Skip to content

Commit a414e65

Browse files
committed
handle env vars that have been removed between executions
1 parent 38fe854 commit a414e65

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

packages/cli-v3/src/entryPoints/dev-run-worker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ let _cancelController = new AbortController();
252252
let _lastFlushPromise: Promise<void> | undefined;
253253
let _sharedWorkerRuntime: SharedRuntimeManager | undefined;
254254

255+
let _lastEnv: Record<string, string> | undefined;
256+
255257
function resetExecutionEnvironment() {
256258
_execution = undefined;
257259
_isRunning = false;
@@ -285,7 +287,10 @@ const zodIpc = new ZodIpcConnection({
285287
if (env) {
286288
populateEnv(env, {
287289
override: true,
290+
previousEnv: _lastEnv,
288291
});
292+
293+
_lastEnv = env;
289294
}
290295

291296
log(`[${new Date().toISOString()}] Received EXECUTE_TASK_RUN`, execution);

packages/cli-v3/src/entryPoints/managed-run-worker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ function resetExecutionEnvironment() {
266266
console.log(`[${new Date().toISOString()}] Reset execution environment`);
267267
}
268268

269+
let _lastEnv: Record<string, string> | undefined;
270+
269271
const zodIpc = new ZodIpcConnection({
270272
listenSchema: WorkerToExecutorMessageCatalog,
271273
emitSchema: ExecutorToWorkerMessageCatalog,
@@ -278,7 +280,10 @@ const zodIpc = new ZodIpcConnection({
278280
if (env) {
279281
populateEnv(env, {
280282
override: true,
283+
previousEnv: _lastEnv,
281284
});
285+
286+
_lastEnv = env;
282287
}
283288

284289
console.log(

packages/core/src/v3/workers/populateEnv.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ interface PopulateEnvOptions {
1313
* @default false
1414
*/
1515
debug?: boolean;
16+
17+
/**
18+
* The previous environment variables
19+
* @default undefined
20+
*/
21+
previousEnv?: Record<string, string>;
1622
}
1723

1824
/**
@@ -25,7 +31,7 @@ export function populateEnv(
2531
envObject: Record<string, string>,
2632
options: PopulateEnvOptions = {}
2733
): void {
28-
const { override = false, debug = false } = options;
34+
const { override = false, debug = false, previousEnv } = options;
2935

3036
if (!envObject || typeof envObject !== "object") {
3137
return;
@@ -47,4 +53,13 @@ export function populateEnv(
4753
process.env[key] = envObject[key];
4854
}
4955
}
56+
57+
if (previousEnv) {
58+
// if there are any keys in previousEnv that are not in envObject, remove them from process.env
59+
for (const key of Object.keys(previousEnv)) {
60+
if (!envObject[key]) {
61+
delete process.env[key];
62+
}
63+
}
64+
}
5065
}

references/hello-world/src/trigger/example.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export const helloWorldTask = task({
66
id: "hello-world",
77
run: async (payload: any, { ctx }) => {
88
logger.info("Hello, world from the init", { ctx, payload });
9+
logger.info("env vars", {
10+
env: process.env,
11+
});
912

1013
logger.debug("debug: Hello, world!", { payload });
1114
logger.info("info: Hello, world!", { payload });
@@ -17,6 +20,8 @@ export const helloWorldTask = task({
1720
logger.debug("some log", { span });
1821
});
1922

23+
await setTimeout(payload.sleepFor ?? 180_000);
24+
2025
logger.trace(
2126
"my trace",
2227
async (span) => {

references/hello-world/trigger.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default defineConfig({
66
project: "proj_rrkpdguyagvsoktglnod",
77
experimental_processKeepAlive: {
88
enabled: true,
9-
maxExecutionsPerProcess: 3,
9+
maxExecutionsPerProcess: 20,
1010
},
1111
logLevel: "log",
1212
maxDuration: 3600,

0 commit comments

Comments
 (0)