Trigger CC from Another Task
A CloudCode task can be queued for execution from itself, or from another task. This can be used to:
Split a long-running task into smaller pieces to avoid running into time limits.
Prevent multiple instances of a task running concurrently.
Re-use logic by keeping it in a shared task (e.g. sending an email).
Run multiple instances of a task in parallel (although most use cases can be covered by doing parallel execution in a single task).
From the CloudCode task's JavaScript we call othertask
(the name of the other CloudCode task) like this:
In othertask
we have the following:
Options for CloudCode.scheduleTask
:
task
(string): The name of the task to execute. The task will be executed in the same environment.parameters
(object, optional): Parameters to pass to the task. Must be an object. The object will be converted to JSON and back.at
(Date, optional): Time to run the task.after
(number, optional): Milliseconds delay before executing the task.queue
(string, optional): Name of a queue to run the task on. No two tasks in the same queue will ever run concurrently. If no queue name is passed, multiple instances of the task may run concurrently. The enqueued task is scheduled to run in a completely separate process. No memory is shared between the two.
Because of the JSON conversion step, database objects cannot be passed through directly in the parameters. It is recommended to instead pass the object ID, and do a database lookup in the enqueued task.
Retries
When a task is placed on a queue, CloudCode ensures that it is run at least once. A task's execution could fail in some scenarios, in which case the task will be retried. A task is considered to have run successfully and will not be retried if a response (either error or success) is received from the task.
The following scenarios will not generate a response, and will cause the task to be retried:
Connection failure between the queue runner and the task
Task timeout
Task crash due to for example out-of-memory error or
process.exit()
Due to these cases, all tasks placed on a queue must be designed to handle multiple executions with the same parameters. If it is important that an action is performed only once, a flag can be stored in the database to indicate that the task has already started or been completed, and should not be executed again.
Last updated