You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c906338
Browse filesBrowse the repository at this point in the historyBrowse files
`metadata.parent` and `metadata.root` now update the parent and root runs when a task file was imported before its run started, as happens when a task is preloaded.
With `processKeepAlive` on, a new process that replaces one that suspended now imports the init file and the last run's task file while the machine waits, so the next run of that task starts without the import. Set `TRIGGER_PRELOAD_DISABLED=1` in your environment to turn it off.
With `processKeepAlive` enabled, a process can't be reused when its run suspends (for example during a long `wait`), crashes, or reaches `maxExecutionsPerProcess`, so it's replaced. In deployed environments, the new process imports your init file and the last run's task file while the machine waits for its next run. If that run is for the same task, it starts without paying for the import. With `processKeepAlive` off (the default), nothing is preloaded.
466
+
467
+
The import happens before the next run exists, using the previous run's environment variables. This affects code at the top level of your task files and init file:
468
+
469
+
- Environment variables read at import time hold the previous run's values. This is already true for a process kept alive between runs.
470
+
- Run credentials such as `TRIGGER_SECRET_KEY` are not set during the import, so code that copies them into a variable at the top level gets `undefined`. Read them inside your task's `run` function, or leave them to the SDK, which reads them when it makes a request.
471
+
- Side effects (opening connections, starting timers, logging) run while no run is active.
472
+
- Some [secret environment variables](/deploy-environment-variables#secret-environment-variables) have a value that is only valid for the run it was given to. A client built at the top level from one of these keeps the previous run's value, so later runs fail with an authentication error from that service (for example a 401).
473
+
474
+
To avoid stale values, build clients that use secrets inside your task's `run` function:
475
+
476
+
```ts trigger/ask-user.ts
477
+
import { task, wait } from"@trigger.dev/sdk";
478
+
importOpenAIfrom"openai";
479
+
480
+
// Before: built at import time, can keep a previous run's API key
481
+
// const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
482
+
483
+
exportconst askUser =task({
484
+
id: "ask-user",
485
+
run: async (payload: { question:string }) => {
486
+
// After: built for each run, from this run's environment variables
To turn preloading off and import task files only when a run starts, add a `TRIGGER_PRELOAD_DISABLED` environment variable with the value `1` to the environment. Set in the dashboard, it applies from the next run without a redeploy. Set with [`syncEnvVars`](/deploy-environment-variables#using-the-syncenvvars-build-extension), it applies from your next deploy.
495
+
463
496
## Development behavior
464
497
465
498
You can control the working directory behavior in development:
0 commit comments