275 lines
8.8 KiB
Plaintext
275 lines
8.8 KiB
Plaintext
---
|
|
title: "The trigger.config.ts file"
|
|
sidebarTitle: "trigger.config file"
|
|
description: "This file is used to configure your project and how it's bundled."
|
|
---
|
|
|
|
import BundlePackages from '/snippets/bundle-packages.mdx';
|
|
|
|
Let's take a look at a basic `trigger.config.ts` file. This is generated for you when you follow [the quick start guide](/quick-start). This file is used to configure your project and how it's bundled.
|
|
|
|
```ts trigger.config.ts
|
|
import type { TriggerConfig } from "@trigger.dev/sdk/v3";
|
|
|
|
export const config: TriggerConfig = {
|
|
//Your project ref (you can see it on the Project settings page in the dashboard)
|
|
project: "proj_gtcwttqhhtlasxgfuhxs",
|
|
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 paths for your trigger folders
|
|
triggerDirectories: ["./trigger"],
|
|
};
|
|
```
|
|
|
|
Most of the time you don't need to change anything in this file, or if you do then we will tell you when you the run the CLI command.
|
|
|
|
## Global initialization
|
|
|
|
You can run code before any task is run by adding a `init` function to your `trigger.config.ts` file.
|
|
|
|
```ts trigger.config.ts
|
|
import type { TriggerConfig } from "@trigger.dev/sdk/v3";
|
|
|
|
export const config: TriggerConfig = {
|
|
//..other stuff
|
|
init: async (payload, { ctx }) => {
|
|
console.log("I run before any task is run");
|
|
},
|
|
};
|
|
```
|
|
|
|
You'll have access to the run payload and the context object. Currently you cannot return anything from this function.
|
|
|
|
## 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 type { TriggerConfig } from "@trigger.dev/sdk/v3";
|
|
|
|
export const config: TriggerConfig = {
|
|
//..other stuff
|
|
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);
|
|
},
|
|
};
|
|
```
|
|
|
|
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 type { TriggerConfig } from "@trigger.dev/sdk/v3";
|
|
import { PrismaInstrumentation } from "@prisma/instrumentation";
|
|
import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai";
|
|
|
|
export const config: TriggerConfig = {
|
|
//..other stuff
|
|
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-undici` | Logs all fetch calls (inc. Undici fetch) |
|
|
| `@opentelemetry/instrumentation-fs` | Logs all file system calls |
|
|
| `@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 |
|
|
|
|
## Syncing environment variables
|
|
|
|
You can sync environment variables from another service using the `resolveEnvVars` function. [Read the docs](/deploy-environment-variables#sync-env-vars-from-another-service) for more information.
|
|
|
|
## ESM-only packages
|
|
|
|
We'll let you know when running the CLI dev command if this is a problem. Some packages are ESM-only so they don't work directly from CJS when using Node.js. In that case you need to add them to the `dependenciesToBundle` array in your `trigger.config.ts` file.
|
|
|
|
<BundlePackages/>
|
|
|
|
## Prisma (and other generators)
|
|
|
|
<Accordion title="The Prisma error you might see">
|
|
|
|
```bash
|
|
✘ [ERROR] Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
|
|
|
|
In case this error is unexpected for you, please report it in
|
|
https://pris.ly/prisma-prisma-bug-report
|
|
at new PrismaClient (/app/node_modules/.prisma/client/default.js:43:11)
|
|
at Object.<anonymous> (/lib/prisma.ts:7:33)
|
|
at Module.\_compile (node:internal/modules/cjs/loader:1356:14)
|
|
at Object.Module.\_extensions..js (node:internal/modules/cjs/loader:1414:10)
|
|
at Module.load (node:internal/modules/cjs/loader:1197:32)
|
|
at Function.Module.\_load (node:internal/modules/cjs/loader:1013:12)
|
|
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
|
|
at node:internal/main/run_main_module:28:49
|
|
```
|
|
|
|
</Accordion>
|
|
|
|
Prisma works by generating a client from your `prisma.schema` file. This means you need to do a couple of things to get it to work with Trigger:
|
|
|
|
<Steps>
|
|
|
|
<Step title="package.json postinstall `prisma generate`">
|
|
|
|
<CodeGroup>
|
|
|
|
```json default path
|
|
{
|
|
"scripts": {
|
|
"postinstall": "prisma generate"
|
|
}
|
|
}
|
|
```
|
|
|
|
```json custom path
|
|
{
|
|
"scripts": {
|
|
"postinstall": "prisma generate --schema=./custom/path/to/schema.prisma"
|
|
}
|
|
}
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
Anything you put in `postinstall` will be run as part of the install step. This is how Next.js recommends you set up Prisma anyway.
|
|
|
|
</Step>
|
|
|
|
<Step title="Add prisma and the schema to trigger.config.ts">
|
|
|
|
```ts trigger.config.ts
|
|
import type { TriggerConfig } from "@trigger.dev/sdk/v3";
|
|
|
|
export const config: TriggerConfig = {
|
|
//..other stuff
|
|
|
|
// using the default path
|
|
additionalFiles: ["./prisma/schema.prisma"],
|
|
// or a custom path, for example in a monorepo
|
|
additionalFiles: ["../../custom/path/to/schema.prisma"],
|
|
|
|
additionalPackages: ["prisma@5.11.0"],
|
|
};
|
|
```
|
|
|
|
This tells Trigger to bundle the Prisma client and the schema file.
|
|
|
|
</Step>
|
|
|
|
</Steps>
|
|
|
|
## TypeORM support
|
|
|
|
We support using TypeORM with Trigger. You can use decorators in your entities and then use them in your tasks. Here's an example:
|
|
|
|
```ts orm/index.ts
|
|
import "reflect-metadata";
|
|
import { DataSource } from "typeorm";
|
|
import { Entity, Column, PrimaryColumn } from "typeorm";
|
|
|
|
@Entity()
|
|
export class Photo {
|
|
@PrimaryColumn()
|
|
id!: number;
|
|
|
|
@Column()
|
|
name!: string;
|
|
|
|
@Column()
|
|
description!: string;
|
|
|
|
@Column()
|
|
filename!: string;
|
|
|
|
@Column()
|
|
views!: number;
|
|
|
|
@Column()
|
|
isPublished!: boolean;
|
|
}
|
|
|
|
export const AppDataSource = new DataSource({
|
|
type: "postgres",
|
|
host: "localhost",
|
|
port: 5432,
|
|
username: "postgres",
|
|
password: "postgres",
|
|
database: "my-database",
|
|
entities: [Photo],
|
|
synchronize: true,
|
|
logging: false,
|
|
});
|
|
```
|
|
|
|
And then in your trigger.config.ts file you can initialize the datasource using the `onStart` lifecycle function option:
|
|
|
|
```ts trigger.config.ts
|
|
import type { TriggerConfig } from "@trigger.dev/sdk/v3";
|
|
import { AppDataSource } from "@/trigger/orm";
|
|
|
|
export const config: TriggerConfig = {
|
|
// ... other options here
|
|
onStart: async (payload, { ctx }) => {
|
|
await AppDataSource.initialize();
|
|
},
|
|
};
|
|
```
|
|
|
|
Now you are ready to use this in your tasks:
|
|
|
|
```ts
|
|
import { task } from "@trigger.dev/sdk/v3";
|
|
import { AppDataSource, Photo } from "./orm";
|
|
|
|
export const taskThatUsesDecorators = task({
|
|
id: "task-that-uses-decorators",
|
|
run: async (payload: { message: string }) => {
|
|
console.log("Creating a photo...");
|
|
|
|
const photo = new Photo();
|
|
photo.id = 2;
|
|
photo.name = "Me and Bears";
|
|
photo.description = "I am near polar bears";
|
|
photo.filename = "photo-with-bears.jpg";
|
|
photo.views = 1;
|
|
photo.isPublished = true;
|
|
|
|
await AppDataSource.manager.save(photo);
|
|
},
|
|
});
|
|
```
|
|
|
|
## Troubleshooting
|
|
|
|
If you have an issue with bundling checkout our [troubleshooting guide](/troubleshooting).
|