Best practices for writing well-behaving Jobs in your codebase
TriggerClient.defineJob
function
id
and name
. The id
is used to identify the Job in the database, and the name
is used to identify the Job in the UI.
id
property through a slugifier because we use it in URLs in our Dashboard. This means you can use any characters you want, but we recommend using only lowercase letters, numbers and dashes.
version
property is used to track changes to your Job. It’s required to be a semantic version string. You can track changes to your Job by incrementing the version number, and we will display in the Dashboard which version each Job Run was created with.
payload
argument (more in this below)
integrations
option when defining your Job.
run
function implements your custom code that will be executed when your Job is run. It’s an async function that takes three arguments:
payload
- The type of the payload
argument is determined by the Trigger you choose.IO
, which exposes built-in tasks and allows you to create your own, as well as interact with integrations.context
object, which contains information about the current run, such as the run ID, the Job ID, and the Job version. Context referencerun
function you define is like a normal JavaScript function in all respects except one: it will be called one or more times to complete a single Job Run.
This means that you can’t rely on any state that is not persisted between runs, and you must create tasks to ensure that work is not repeated.
Why does the run function work like this?
Task | Description | Task code |
---|---|---|
Delay | Wait for a period of time | await io.wait("wait", 60); |
Log | Log a message | await io.logger.log("Hello"); |
Send Event | Send an event (for eventTrigger) | await io.sendEvent("my-event", { name: "my.event", payload: { hello: "world" } }); |
Background fetch | Fetch data from a URL that can take longer that the serverless timeout. | await io.backgroundFetch("fetch-some-data", { url: "https://example.com" }); |
retry
option.
try/catch
block in your run code and catch these errors, your job will not work correctly. You can check if an error is an internal error using isTriggerError():
for const ... of
to ensure that the tasks are created in the correct order.
Promise.all
will not work correctly.
This is something on our roadmap that we hope to support soon.run
function will be automatically set as the run output and displayed in the Dashboard.