Scheduling functions
You can schedule public functions and internal functions from mutations and actions via the scheduler provided in the respective function context.- runAfter schedules a function to run after a delay (measured in milliseconds).
- runAt schedules a function run at a date or timestamp (measured in milliseconds elapsed since the epoch).
Scheduling from mutations
Scheduling functions from mutations is atomic with the rest of the mutation. This means that if the mutation succeeds, the scheduled function is guaranteed to be scheduled. On the other hand, if the mutations fails, no function will be scheduled, even if the function fails after the scheduling call.Scheduling from actions
Unlike mutations, actions don’t execute as a single database transaction and can have side effects. Thus, scheduling from actions does not depend on the outcome of the function. This means that an action might succeed to schedule some functions and later fail due to transient error or a timeout. The scheduled functions will still be executed.Scheduling immediately
UsingrunAfter() with delay set to 0 is used to immediately add a function to
the event queue. This usage may be familiar to you if you’re used to calling
setTimeout(fn, 0).
As noted above, actions are not atomic and are meant to cause side effects.
Scheduling immediately becomes useful when you specifically want to trigger an
action from a mutation that is conditional on the mutation succeeding.
This post
goes over a direct example of this in action, where the application depends on
an external service to fill in information to the database.
Retrieving scheduled function status
Every scheduled function is reflected as a document in the"_scheduled_functions" system table. runAfter() and runAt() return the id
of scheduled function. You can read data from system tables using the
db.system.get and db.system.query methods, which work the same as the
standard db.get and db.query methods.
name: the path of the scheduled functionargs: the arguments passed to the scheduled functionscheduledTime: the timestamp of when the function is scheduled to run (measured in milliseconds elapsed since the epoch)completedTime: the timestamp of when the function finished running, if it has completed (measured in milliseconds elapsed since the epoch)state: the status of the scheduled function. Here are the possible states a scheduled function can be in:Pending: the function has not been started yetInProgress: the function has started running is not completed yet (only applies to actions)Success: the function finished running successfully with no errorsFailed: the function hit an error while running, which can either be a user error or an internal server errorCanceled: the function was canceled via the console,ctx.scheduler.cancel, or recursively by a parent scheduled function that was canceled while in progress
Canceling scheduled functions
You can cancel a previously scheduled function withcancel via the
scheduler provided in the respective
function context.
cancel does depends on the state of the scheduled function:
- If it hasn’t started running, it won’t run.
- If it already started, it will continue to run, but any functions it schedules will not run.