Event-Driven Workflows and Background Jobs
The@repo/trigger
package provides a seamless integration with Trigger.dev, a powerful platform for running background jobs and event-driven workflows in your Next.js application.
Features
- Pre-configured Trigger.dev client with proper environment variable handling
- Type-safe event sending with error handling
- Organized job structure for better maintainability
- Seamless integration with the API app
Installation
This package is part of thezopio
monorepo and is available to all applications in the workspace.
Environment Variables
The package requires the following environment variables:Variable | Description | Required |
---|---|---|
TRIGGER_API_KEY | Your Trigger.dev API key | Yes |
TRIGGER_API_URL | The Trigger.dev API URL | No (defaults to “https://api.trigger.dev”) |
Basic Usage
Importing the Client
Defining a Job
Jobs are defined using the Trigger.dev client. Here’s an example of a simple job:Sending Events
To trigger a job, you need to send an event with the corresponding name:Integration with API
The trigger package is integrated with the API app to provide a webhook endpoint for Trigger.dev.API Route Handler
The API app includes a route handler atapp/trigger/route.ts
that processes incoming webhook requests:
Job Organization
Jobs are organized in a dedicated directory structure:Advanced Usage
Custom Job Triggers
Besides event triggers, Trigger.dev supports various other trigger types:Job Dependencies
You can define jobs that depend on other services:Error Handling
ThesendEvent
function includes built-in error handling:
Troubleshooting
Missing API Key
If you see a warning about a missing API key, make sure to set theTRIGGER_API_KEY
environment variable:
TRIGGER_API_KEY=missing-api-key environment variable is not set. Trigger.dev functionality will not work properly.
Job Not Running
If your job isn’t running when you send an event, check the following:- Make sure the event name matches exactly
- Verify that your job is properly imported and registered
- Check the Trigger.dev dashboard for any errors