Understanding postgres.js connection management in Trigger.dev v3 #2218
Replies: 1 comment
-
|
@leedia-tech each task run gets its own child process via 1. task isolation by default, every run forks a fresh process. after the task completes, the process gets SIGTERM then SIGKILL. with 2. connection lifecycle without with your pattern matches the official drizzle guide which does the same (db instance at module level), so you're good. 3. cleanup you don't need to manually call one edge case: if you use waitpoints ( for explicit control, you can use the middleware pattern: // init.ts (auto-loaded by trigger.dev)
import { tasks } from "@trigger.dev/sdk/v3";
tasks.middleware("db", async ({ next }) => {
await next();
});
tasks.onWait("db", async () => {
await sql.end({ timeout: 5 }); // free connections during checkpoint
});
tasks.onResume("db", async () => {
// postgres.js reconnects lazily, nothing needed here
});but honestly for postgres.js this is optional since it handles reconnection automatically. ref: how it works | tasks overview (lifecycle hooks) | processKeepAlive changelog | postgres.js lazy connections |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using postgres.js and noticed your Drizzle example creates the db instance outside the task.
I'm wondering:
Task isolation: Do tasks run in separate workers/processes, or share the same Node process?
Connection lifecycle: If I create a postgres instance outside the task:
Is this instance shared across all executions, or does each worker get its own?
Connection cleanup: postgres.js requires
sql.end()to properly close connections. Should I handle this cleanup or does the worker lifecycle manage it?Beta Was this translation helpful? Give feedback.
All reactions