fade22015f
* Adds new features table to top of v4 upgrade guide * Adds wait idempotency to wait-until, wait-for, and wait-for-token pages * Adds new priority docs page and updates the v4 upgrade guide * Adds new task lifecycle hooks * Removes the message about requiring tasks to be exported * Adds new global lifecycle hooks section * Moves sections from upgrade guide into the table * Adds hidden task page * Improves the global lifecycle hooks section * Updates middleware and locals section * Adds new useWaitToken page to the react hooks section * Adds a new ai.tool section * Moves Docker (legacy) page into self-hosting section * Removes known issues from v4 upgrade guide * Replace “toolTask” with “ai.tool” in the Streams page example * Renames guide to “Migrating from v3” and adds redirect * Remove references to v4 * Removes changelog from migration guide * The installation guide now references `@latest update` * Changes all references from `/sdk/v3` to `/sdk` * Updates @v4-beta to @latest * Fixed broken link * Fixes broken link * Adds an upgrade to v4 using AI section * Fixes 2 broken links * Adds an entry for targetting preview branches * Updates the run statuses * Adds boolean helpers section to the runs and realtime pages * Updates the concurrency page * Updates the test page to include the new options * Adds SDK and curl options for the preview branch targeting * Updates new bulk actions page * Remove the releasing concurrency section * Got rid of some more @v4-beta mentions * Improved rate limit docs * Improved migrating docs * Removed commented sections of the docs * useWaitToken hook * Fixed the description * Fix for missing test image --------- Co-authored-by: Matt Aitken <matt@mattaitken.com> Co-authored-by: Dan <8297864+D-K-P@users.noreply.github.com>
442 lines
14 KiB
Plaintext
442 lines
14 KiB
Plaintext
---
|
|
title: "The trigger.config.ts file"
|
|
sidebarTitle: "trigger.config.ts"
|
|
description: "This file is used to configure your project and how it's built."
|
|
---
|
|
|
|
import ScrapingWarning from "/snippets/web-scraping-warning.mdx";
|
|
import BundlePackages from "/snippets/bundle-packages.mdx";
|
|
import NodeVersions from "/snippets/node-versions.mdx";
|
|
|
|
The `trigger.config.ts` file is used to configure your Trigger.dev project. It is a TypeScript file at the root of your project that exports a default configuration object. Here's an example:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
// Your project ref (you can see it on the Project settings page in the dashboard)
|
|
project: "<project ref>",
|
|
//The paths for your trigger folders
|
|
dirs: ["./trigger"],
|
|
retries: {
|
|
//If you want to retry a task in dev mode (when using the CLI)
|
|
enabledInDev: false,
|
|
//the default retry settings. Used if you don't specify on a task.
|
|
default: {
|
|
maxAttempts: 3,
|
|
minTimeoutInMs: 1000,
|
|
maxTimeoutInMs: 10000,
|
|
factor: 2,
|
|
randomize: true,
|
|
},
|
|
},
|
|
});
|
|
```
|
|
|
|
The config file handles a lot of things, like:
|
|
|
|
- Specifying where your trigger tasks are located using the `dirs` option.
|
|
- Setting the default retry settings.
|
|
- Configuring OpenTelemetry instrumentations.
|
|
- Customizing the build process.
|
|
- Adding global task lifecycle functions.
|
|
|
|
<Note>
|
|
The config file is bundled with your project, so code imported in the config file is also bundled,
|
|
which can have an effect on build times and cold start duration. One important qualification is
|
|
anything defined in the `build` config is automatically stripped out of the config file, and
|
|
imports used inside build config with be tree-shaken out.
|
|
</Note>
|
|
|
|
## Dirs
|
|
|
|
You can specify the directories where your tasks are located using the `dirs` option:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
dirs: ["./trigger"],
|
|
});
|
|
```
|
|
|
|
If you omit the `dirs` option, we will automatically detect directories that are named `trigger` in your project, but we recommend specifying the directories explicitly. The `dirs` option is an array of strings, so you can specify multiple directories if you have tasks in multiple locations.
|
|
|
|
We will search for TypeScript and JavaScript files in the specified directories and include them in the build process. We automatically exclude files that have `.test` or `.spec` in the name, but you can customize this by specifying glob patterns in the `ignorePatterns` option:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
dirs: ["./trigger"],
|
|
ignorePatterns: ["**/*.my-test.ts"],
|
|
});
|
|
```
|
|
|
|
## Lifecycle functions
|
|
|
|
You can add lifecycle functions to get notified when any task starts, succeeds, or fails using `onStart`, `onSuccess` and `onFailure`:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
onSuccess: async (payload, output, { ctx }) => {
|
|
console.log("Task succeeded", ctx.task.id);
|
|
},
|
|
onFailure: async (payload, error, { ctx }) => {
|
|
console.log("Task failed", ctx.task.id);
|
|
},
|
|
onStart: async (payload, { ctx }) => {
|
|
console.log("Task started", ctx.task.id);
|
|
},
|
|
init: async (payload, { ctx }) => {
|
|
console.log("I run before any task is run");
|
|
},
|
|
});
|
|
```
|
|
|
|
Read more about task lifecycle functions in the [tasks overview](/tasks/overview).
|
|
|
|
## Instrumentations
|
|
|
|
We use OpenTelemetry (OTEL) for our run logs. This means you get a lot of information about your tasks with no effort. But you probably want to add more information to your logs. For example, here's all the Prisma calls automatically logged:
|
|
|
|

|
|
|
|
Here we add Prisma and OpenAI instrumentations to your `trigger.config.ts` file.
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
import { PrismaInstrumentation } from "@prisma/instrumentation";
|
|
import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
telemetry: {
|
|
instrumentations: [new PrismaInstrumentation(), new OpenAIInstrumentation()],
|
|
},
|
|
});
|
|
```
|
|
|
|
There is a [huge library of instrumentations](https://opentelemetry.io/ecosystem/registry/?language=js) you can easily add to your project like this.
|
|
|
|
Some ones we recommend:
|
|
|
|
| Package | Description |
|
|
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
|
| `@opentelemetry/instrumentation-http` | Logs all HTTP calls |
|
|
| `@prisma/instrumentation` | Logs all Prisma calls, you need to [enable tracing](https://github.com/prisma/prisma/tree/main/packages/instrumentation) |
|
|
| `@traceloop/instrumentation-openai` | Logs all OpenAI calls |
|
|
|
|
<Note>
|
|
`@opentelemetry/instrumentation-fs` which logs all file system calls is currently not supported.
|
|
</Note>
|
|
|
|
### Telemetry Exporters
|
|
|
|
You can also configure custom telemetry exporters to send your traces and logs to other external services. For example, you can send your logs to [Axiom](https://axiom.co/docs/guides/opentelemetry-nodejs#exporter-instrumentation-ts). First, add the opentelemetry exporter packages to your package.json file:
|
|
|
|
```json package.json
|
|
"dependencies": {
|
|
"@opentelemetry/exporter-logs-otlp-http": "0.52.1",
|
|
"@opentelemetry/exporter-trace-otlp-http": "0.52.1"
|
|
}
|
|
```
|
|
|
|
Then, configure the exporters in your `trigger.config.ts` file:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
|
|
// Initialize OTLP trace exporter with the endpoint URL and headers;
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
telemetry: {
|
|
instrumentations: [
|
|
// Your instrumentations here
|
|
],
|
|
logExporters: [
|
|
new OTLPLogExporter({
|
|
url: "https://api.axiom.co/v1/logs",
|
|
headers: {
|
|
Authorization: `Bearer ${process.env.AXIOM_API_TOKEN}`,
|
|
"X-Axiom-Dataset": process.env.AXIOM_DATASET,
|
|
},
|
|
}),
|
|
],
|
|
exporters: [
|
|
new OTLPTraceExporter({
|
|
url: "https://api.axiom.co/v1/traces",
|
|
headers: {
|
|
Authorization: `Bearer ${process.env.AXIOM_API_TOKEN}`,
|
|
"X-Axiom-Dataset": process.env.AXIOM_DATASET,
|
|
},
|
|
}),
|
|
],
|
|
},
|
|
});
|
|
```
|
|
|
|
Make sure to set the `AXIOM_API_TOKEN` and `AXIOM_DATASET` environment variables in your project.
|
|
|
|
It's important to note that you cannot configure exporters using `OTEL_*` environment variables, as they would conflict with our internal telemetry. Instead you should configure the exporters via passing in arguments to the `OTLPTraceExporter` and `OTLPLogExporter` constructors. For example, here is how you can configure exporting to Honeycomb:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
|
|
// Initialize OTLP trace exporter with the endpoint URL and headers;
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
telemetry: {
|
|
instrumentations: [
|
|
// Your instrumentations here
|
|
],
|
|
logExporters: [
|
|
new OTLPLogExporter({
|
|
url: "https://api.honeycomb.io/v1/logs",
|
|
headers: {
|
|
"x-honeycomb-team": process.env.HONEYCOMB_API_KEY,
|
|
"x-honeycomb-dataset": process.env.HONEYCOMB_DATASET,
|
|
},
|
|
}),
|
|
],
|
|
exporters: [
|
|
new OTLPTraceExporter({
|
|
url: "https://api.honeycomb.io/v1/traces",
|
|
headers: {
|
|
"x-honeycomb-team": process.env.HONEYCOMB_API_KEY,
|
|
"x-honeycomb-dataset": process.env.HONEYCOMB_DATASET,
|
|
},
|
|
}),
|
|
],
|
|
},
|
|
});
|
|
```
|
|
|
|
## Runtime
|
|
|
|
We currently only officially support the `node` runtime, but you can try our experimental `bun` runtime by setting the `runtime` option in your config file:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
runtime: "bun",
|
|
});
|
|
```
|
|
|
|
See our [Bun guide](/guides/frameworks/bun) for more information.
|
|
|
|
### Node.js versions
|
|
|
|
<NodeVersions />
|
|
|
|
## Default machine
|
|
|
|
You can specify the default machine for all tasks in your project:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
defaultMachine: "large-1x",
|
|
});
|
|
```
|
|
|
|
See our [machines documentation](/machines) for more information.
|
|
|
|
## Log level
|
|
|
|
You can set the log level for your project:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
logLevel: "debug",
|
|
});
|
|
```
|
|
|
|
The `logLevel` only determines which logs are sent to the Trigger.dev instance when using the `logger` API. All `console` based logs are always sent.
|
|
|
|
## Max duration
|
|
|
|
You can set the default `maxDuration` for all tasks in your project:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
maxDuration: 60, // 60 seconds
|
|
});
|
|
```
|
|
|
|
See our [maxDuration guide](/runs/max-duration) for more information.
|
|
|
|
## Build configuration
|
|
|
|
You can customize the build process using the `build` option:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
build: {
|
|
// Don't bundle these packages
|
|
external: ["header-generator"],
|
|
},
|
|
});
|
|
```
|
|
|
|
<Note>
|
|
The `trigger.config.ts` file is included in the bundle, but with the `build` configuration
|
|
stripped out. These means any imports only used inside the `build` configuration are also removed
|
|
from the final bundle.
|
|
</Note>
|
|
|
|
### External
|
|
|
|
All code is bundled by default, but you can exclude some packages from the bundle using the `external` option:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
build: {
|
|
external: ["header-generator"],
|
|
},
|
|
});
|
|
```
|
|
|
|
When a package is excluded from the bundle, it will be added to a dynamically generated package.json file in the build directory. The version of the package will be the same as the version found in your `node_modules` directory.
|
|
|
|
Each entry in the external should be a package name, not necessarily the import path. For example, if you want to exclude the `ai` package, but you are importing `ai/rsc`, you should just include `ai` in the `external` array:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
build: {
|
|
external: ["ai"],
|
|
},
|
|
});
|
|
```
|
|
|
|
<Note>
|
|
Any packages that install or build a native binary should be added to external, as native binaries
|
|
cannot be bundled. For example, `re2`, `sharp`, and `sqlite3` should be added to external.
|
|
</Note>
|
|
|
|
### JSX
|
|
|
|
You can customize the `jsx` options that are passed to `esbuild` using the `jsx` option:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
build: {
|
|
jsx: {
|
|
// Use the Fragment component instead of React.Fragment
|
|
fragment: "Fragment",
|
|
// Use the h function instead of React.createElement
|
|
factory: "h",
|
|
// Turn off automatic runtime
|
|
automatic: false,
|
|
},
|
|
},
|
|
});
|
|
```
|
|
|
|
By default we enabled [esbuild's automatic JSX runtime](https://esbuild.github.io/content-types/#auto-import-for-jsx) which means you don't need to import `React` in your JSX files. You can disable this by setting `automatic` to `false`.
|
|
|
|
See the [esbuild JSX documentation](https://esbuild.github.io/content-types/#jsx) for more information.
|
|
|
|
### Conditions
|
|
|
|
You can add custom [import conditions](https://esbuild.github.io/api/#conditions) to your build using the `conditions` option:
|
|
|
|
```ts trigger.config.ts
|
|
import { defineConfig } from "@trigger.dev/sdk";
|
|
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
build: {
|
|
conditions: ["react-server"],
|
|
},
|
|
});
|
|
```
|
|
|
|
These conditions effect how imports are resolved during the build process. For example, the `react-server` condition will resolve `ai/rsc` to the server version of the `ai/rsc` export.
|
|
|
|
Custom conditions will also be passed to the `node` runtime when running your tasks.
|
|
|
|
### Extensions
|
|
|
|
Build extension allow you to hook into the build system and customize the build process or the resulting bundle and container image (in the case of deploying). You can use pre-built extensions by installing the `@trigger.dev/build` package into your `devDependencies`, or you can create your own.
|
|
|
|
#### additionalFiles
|
|
|
|
See the [additionalFiles documentation](/config/extensions/additionalFiles) for more information.
|
|
|
|
#### `additionalPackages`
|
|
|
|
See the [additionalPackages documentation](/config/extensions/additionalPackages) for more information.
|
|
|
|
#### `emitDecoratorMetadata`
|
|
|
|
See the [emitDecoratorMetadata documentation](/config/extensions/emitDecoratorMetadata) for more information.
|
|
|
|
#### Prisma
|
|
|
|
See the [prismaExtension documentation](/config/extensions/prismaExtension) for more information.
|
|
|
|
#### syncEnvVars
|
|
|
|
See the [syncEnvVars documentation](/config/extensions/syncEnvVars) for more information.
|
|
|
|
#### puppeteer
|
|
|
|
See the [puppeteer documentation](/config/extensions/puppeteer) for more information.
|
|
|
|
#### ffmpeg
|
|
|
|
See the [ffmpeg documentation](/config/extensions/ffmpeg) for more information.
|
|
|
|
#### esbuild plugins
|
|
|
|
See the [esbuild plugins documentation](/config/extensions/esbuildPlugin) for more information.
|
|
|
|
#### aptGet
|
|
|
|
See the [aptGet documentation](/config/extensions/aptGet) for more information.
|