In Cloudcode, I need to execute cloud code tasks in a particular order and the subsequent task needs to not run until after the prior task is complete. Would this best be done by using the queue string? And if so, can you give an example of queue names?
If you want one task to execute from a queue then you can use the
queue logic or the
Limit to a single instance task setting. But this just works for multiple executions of the same task.
If you want to have multiple different tasks execute in a specific sequence then you have one of two options (broadly speaking)
- Easiest: Initiate the first task in your sequence and let that task, upon a successful execution, schedule the subsequent task in the sequence. Keep following this pattern until all tasks have been completed in sequence. So, the very last line of
await CloudCoude.scheduleTask('task_2')and the very last line of
await CloudCoude.scheduleTask('task_3'), and so on
- Controlled 'State Engine' (more complicated, but more controlled): Have a DB object that is used to keep track of the state of the sequential execution of the CC tasks. Have all the potential steps/sequences that the process can go through as fields on this new
state_object. When the process starts kick off the first task and update the associated
state_objectto indicate that 'task_1' has started and save the object (using something like a timestamp or a boolean flag). When 'task_1' completed update the same
state_objectand indicate that 'task_1' has completed (again using either a timestamp or a boolean flag). And finally, have each DB transaction for the
state_objectfire a webhook, using an 'update' webhook, that gets processed by a another CC task which manages this 'State Engine'. This CC task will receive an updated
state_object, inspect its fields and if it finds that the necessary prerequisite business logic has been met then it can schedule the subsequent CC Task (for example if the object has a boolean flag for task_1 completed but not a flag for task_2 started, then it knows to schedule task_2)
Please note, the above are general guidelines, you would still need to think about wether or not you want to have multiple instances of each task running in parallel and what all the triggers are that could kick off this sequence, etc.
I hope this makes sense.
It does, thank you. I had already pretty much adopted approach 1 after I discovered putting more than 1 await cloud code at the end of the first task kicked off simultaneous tasks which resulted in bad data. I will do some testing on the state engine concept and let you know.
@LaurisaPrince Yes, the await would just await the successful ‘scheduling’ of the task, not the execution of the task