trigger.dev
Learn how to set up background jobs, cron jobs and queues with trigger.dev in your supastarter application.
Before we get started, make sure to create a new trigger.dev and project.
You can find a full example of this recipe in the feat/trigger.dev branch of the supastarter-nextjs repository.
1. Create a new package in your repository
Create a new folder tasks
in the /packages
directory and add the following files:
2. Create your first task
Now to create your first task, create a new file test-task.ts
in the packages/tasks/trigger
directory and add the following code:
Read the trigger.dev documentation for more information on how to create tasks or cron jobs.
3. Test your task
You can easily test your task locally by running pnpm --filter tasks dev
from the root of your repository.
This will deploy your task to trigger.dev in the development environment, so you can trigger it from there. When you run the task in the development environment, it will be executed on your local machine.
4. Deploy your task
To deploy your task to trigger.dev, run pnpm --filter tasks deploy
from the root of your repository.
You can also add this command as an automated deployment step in your CI/CD pipeline, by creating a new github action.
You need to add the TRIGGER_ACCESS_TOKEN
secret to your repositories secrets, which you can create in the trigger.dev dashboard.
5. Programmatically trigger a task
If you want to trigger one of your tasks from your application, for example as an action from an API call, you can import the task to your API resolver and trigger it.
Make sure to add the @repo/tasks
package to your api
package as a dependency.