Reliable scheduling with Trigger.dev¶
Trigger.dev is an open source background job platform. With Trigger.dev you can easily create, schedule, and manage background jobs using code.
Read on to learn how to create complex, reliable scheduling with Trigger.dev.
Before you start¶
Before you start, ensure:
- You have a Trigger.dev account.
- You have a Tinybird workspace.
Create your first trigger task¶
The tinybird-trigger-tasks package implements tasks for Tinybird copy pipes and the Query API. You can find the source code in the @sdairs/tinybird-trigger repo.
Create a working directory, and run
npx trigger.dev@latest initto connect the project to Trigger.dev.Inside the
triggerdirectory, install the npm package withnpm install @sdairs/tinybird-trigger-tasks.Create a new file called
myTask.tsand add the following code:
import { task } from "@trigger.dev/sdk/v3";
import { tinybirdCopyTask } from "@sdairs/tinybird-trigger-tasks";
export const exampleExecutor = task({
id: "example-executor",
run: async (payload, { ctx }) => {
console.log("Example executor task is running");
// Run a copy job
const copyResult = await tinybirdCopyTask.triggerAndWait({ pipeId: <COPY_PIPE_ID> });
console.log(copyResult);
},
});
- Create a new pipe using the following SQL:
SELECT number + 1 AS value FROM numbers(100)
Name the pipe
my_copyand deploy the changes.Update
myTask.ts, replacing<COPY_PIPE_ID>with the name of your pipe,my_copyin this case.Create a
.envfile in your directory root.Go to your Tinybird workspace and copy the Admin Token, then add it to the
.envfile as follows:
TINYBIRD_TOKEN=p.eyJ...
Run
npx trigger.dev@latest devto push the task to Trigger.dev.Go to your Trigger.dev dashboard, and perform a test run to trigger the task and the copy pipe.
Go to your Tinybird workspace and check the copy pipe results.