Invoke triggers
Invoke Jobs manually using the invoke Trigger
Sometimes it makes sense to be able to invoke a Job manually, without having to specify an event, especially for cases where you want to get notified when the invoked Job Run is complete.
To specify that a job is manually invokable, you can use the invokeTrigger()
function when defining a job:
And then you can invoke the job using the Job.invoke()
method:
Payload Schema
You can specify the type of the expected payload by passing a Zod schema to invokeTrigger()
:
Now when you invoke the job, you will get a type error if the payload does not match the schema:
Invoking a Job
As you can see in the example above, invoking a job is as simple as calling the Job.invoke()
method. This method returns a JobRun
object that you can use to track the progress of the job run, especially in conjunction with our React hooks, like useRunDetails()
There are many variations for how you could get the job run information passed from your backend to your frontend, depending on the framework you are using and which rendering model. We’ll leave that as an exercise for the reader
Deduplicate Invocations
You can pass an optional idempotencyKey
to the invoke()
method to deduplicate invocations. This is useful when you want to make sure that a job is only invoked once for a given payload.
Callback URL
You can also pass an optional callbackUrl
to the invoke()
method to get notified when the job run is complete, either successfully or with an error.
When the run is complete, we will issue a POST
request to the URL with the RunNotification payload.
Verifying the callback
You should make sure to verify the payload in your callback route to make sure that the request is coming from Trigger. You can do this by checking the X-Trigger-Signature-256
header, which contains a HMAC signature of the payload using your secret API Key.
Additional context
You can pass an optional context
object to the invoke()
method, which will be available in the job run context. This is useful for passing additional information to the job run that doesn’t make sense in the payload.
And then you can access the context in the job run:
Invoking a job from a job
You can also invoke a job from another job:
Notice how the invoke()
method takes a string as the first argument. This is because under the hood invoke()
is automatically creating a Task and the "⚡"
string is the cacheKey
for the created task. You can easily see the run created via the Run Dashboard:
If Job.invoke()
is called within another job, and you don’t include the cacheKey
we’ll
throw an error.
Wait for completion
You can also invoke a job and wait for it to complete before continuing execution of the current job:
By default, invokeAndWaitForCompletion()
will wait for the job run to complete for up to 60 minutes. You can change this by passing a timeoutInSeconds
option:
If the invoked job run doesn’t complete in the given time, the underlying task will fail, along with the run
The return value of invokeAndWaitForCompletion
is a RunNotification object.
Batch invoke and wait for completion
You can also batch invoke jobs and wait for them all to complete before continuing execution of the current job:
You can batch up to 25 invocations at once, and we will run them in parallel and wait for all of them to complete before continuing execution of the current job.
You can in an optional options
object to each invocation, if you want to pass context
and accountId
to each invocation:
Was this page helpful?