@@ -19,6 +19,7 @@ import {
1919 runMetadata ,
2020 runtime ,
2121 runTimelineMetrics ,
22+ taskContext ,
2223 TaskRunErrorCodes ,
2324 TaskRunExecution ,
2425 timeout ,
@@ -58,6 +59,7 @@ import sourceMapSupport from "source-map-support";
5859import { env } from "std-env" ;
5960import { normalizeImportPath } from "../utilities/normalizeImportPath.js" ;
6061import { VERSION } from "../version.js" ;
62+ import { promiseWithResolvers } from "@trigger.dev/core/utils" ;
6163
6264sourceMapSupport . install ( {
6365 handleUncaughtExceptions : false ,
@@ -112,8 +114,12 @@ runTimelineMetrics.setGlobalManager(standardRunTimelineMetricsManager);
112114
113115const devUsageManager = new DevUsageManager ( ) ;
114116usage . setGlobalUsageManager ( devUsageManager ) ;
115- timeout . setGlobalManager ( new UsageTimeoutManager ( devUsageManager ) ) ;
116- resourceCatalog . setGlobalResourceCatalog ( new StandardResourceCatalog ( ) ) ;
117+
118+ const usageTimeoutManager = new UsageTimeoutManager ( devUsageManager ) ;
119+ timeout . setGlobalManager ( usageTimeoutManager ) ;
120+
121+ const standardResourceCatalog = new StandardResourceCatalog ( ) ;
122+ resourceCatalog . setGlobalResourceCatalog ( standardResourceCatalog ) ;
117123
118124const durableClock = new DurableClock ( ) ;
119125clock . setGlobalClock ( durableClock ) ;
@@ -238,7 +244,29 @@ let _isRunning = false;
238244let _isCancelled = false ;
239245let _tracingSDK : TracingSDK | undefined ;
240246let _executionMeasurement : UsageMeasurement | undefined ;
241- const cancelController = new AbortController ( ) ;
247+ let _cancelController = new AbortController ( ) ;
248+ let _lastFlushPromise : Promise < void > | undefined ;
249+
250+ function resetExecutionEnvironment ( ) {
251+ _execution = undefined ;
252+ _isRunning = false ;
253+ _isCancelled = false ;
254+ _executionMeasurement = undefined ;
255+ _cancelController = new AbortController ( ) ;
256+
257+ standardLocalsManager . reset ( ) ;
258+ standardLifecycleHooksManager . reset ( ) ;
259+ standardRunTimelineMetricsManager . reset ( ) ;
260+ devUsageManager . reset ( ) ;
261+ usageTimeoutManager . reset ( ) ;
262+ runMetadataManager . reset ( ) ;
263+ waitUntilManager . reset ( ) ;
264+ sharedWorkerRuntime . reset ( ) ;
265+ durableClock . reset ( ) ;
266+ taskContext . disable ( ) ;
267+
268+ log ( `[${ new Date ( ) . toISOString ( ) } ] Reset execution environment` ) ;
269+ }
242270
243271const zodIpc = new ZodIpcConnection ( {
244272 listenSchema : WorkerToExecutorMessageCatalog ,
@@ -254,6 +282,18 @@ const zodIpc = new ZodIpcConnection({
254282
255283 log ( `[${ new Date ( ) . toISOString ( ) } ] Received EXECUTE_TASK_RUN` , execution ) ;
256284
285+ if ( _lastFlushPromise ) {
286+ const now = performance . now ( ) ;
287+
288+ await _lastFlushPromise ;
289+
290+ const duration = performance . now ( ) - now ;
291+
292+ log ( `[${ new Date ( ) . toISOString ( ) } ] Awaited last flush in ${ duration } ms` ) ;
293+ }
294+
295+ resetExecutionEnvironment ( ) ;
296+
257297 standardRunTimelineMetricsManager . registerMetricsFromExecution ( metrics ) ;
258298
259299 if ( _isRunning ) {
@@ -365,8 +405,6 @@ const zodIpc = new ZodIpcConnection({
365405 return ;
366406 }
367407
368- process . title = `trigger-dev-worker: ${ execution . task . id } ${ execution . run . id } ` ;
369-
370408 // Import the task module
371409 const task = resourceCatalog . getTask ( execution . task . id ) ;
372410
@@ -413,7 +451,7 @@ const zodIpc = new ZodIpcConnection({
413451
414452 const timeoutController = timeout . abortAfterTimeout ( execution . run . maxDuration ) ;
415453
416- const signal = AbortSignal . any ( [ cancelController . signal , timeoutController . signal ] ) ;
454+ const signal = AbortSignal . any ( [ _cancelController . signal , timeoutController . signal ] ) ;
417455
418456 const { result } = await executor . execute ( execution , metadata , traceContext , signal ) ;
419457
@@ -432,8 +470,7 @@ const zodIpc = new ZodIpcConnection({
432470 } ) ;
433471 }
434472 } finally {
435- _execution = undefined ;
436- _isRunning = false ;
473+ log ( `[${ new Date ( ) . toISOString ( ) } ] Task run completed` ) ;
437474 }
438475 } catch ( err ) {
439476 logError ( "Failed to execute task" , err ) ;
@@ -459,7 +496,7 @@ const zodIpc = new ZodIpcConnection({
459496 } ,
460497 CANCEL : async ( { timeoutInMs } ) => {
461498 _isCancelled = true ;
462- cancelController . abort ( "run cancelled" ) ;
499+ _cancelController . abort ( "run cancelled" ) ;
463500 await callCancelHooks ( timeoutInMs ) ;
464501 if ( _executionMeasurement ) {
465502 usage . stop ( _executionMeasurement ) ;
@@ -490,6 +527,10 @@ async function callCancelHooks(timeoutInMs: number = 10_000) {
490527async function flushAll ( timeoutInMs : number = 10_000 ) {
491528 const now = performance . now ( ) ;
492529
530+ const { promise, resolve } = promiseWithResolvers < void > ( ) ;
531+
532+ _lastFlushPromise = promise ;
533+
493534 const results = await Promise . allSettled ( [
494535 flushTracingSDK ( timeoutInMs ) ,
495536 flushMetadata ( timeoutInMs ) ,
@@ -522,6 +563,9 @@ async function flushAll(timeoutInMs: number = 10_000) {
522563 const duration = performance . now ( ) - now ;
523564
524565 log ( `Flushed all in ${ duration } ms` ) ;
566+
567+ // Resolve the last flush promise
568+ resolve ( ) ;
525569}
526570
527571async function flushTracingSDK ( timeoutInMs : number = 10_000 ) {
0 commit comments