@@ -18,6 +18,8 @@ import {
1818 waitUntil ,
1919 apiClientManager ,
2020 runTimelineMetrics ,
21+ lifecycleHooks ,
22+ lifecycleHooksAdapters ,
2123 localsAPI ,
2224} from "@trigger.dev/core/v3" ;
2325import { TriggerTracer } from "@trigger.dev/core/v3/tracer" ;
@@ -40,6 +42,7 @@ import {
4042 StandardWaitUntilManager ,
4143 ManagedRuntimeManager ,
4244 StandardRunTimelineMetricsManager ,
45+ StandardLifecycleHooksManager ,
4346 StandardLocalsManager ,
4447} from "@trigger.dev/core/v3/workers" ;
4548import { ZodIpcConnection } from "@trigger.dev/core/v3/zodIpc" ;
@@ -98,6 +101,9 @@ const heartbeatIntervalMs = getEnvVar("HEARTBEAT_INTERVAL_MS");
98101const standardLocalsManager = new StandardLocalsManager ( ) ;
99102localsAPI . setGlobalLocalsManager ( standardLocalsManager ) ;
100103
104+ const standardLifecycleHooksManager = new StandardLifecycleHooksManager ( ) ;
105+ lifecycleHooks . setGlobalLifecycleHooksManager ( standardLifecycleHooksManager ) ;
106+
101107const standardRunTimelineMetricsManager = new StandardRunTimelineMetricsManager ( ) ;
102108runTimelineMetrics . setGlobalManager ( standardRunTimelineMetricsManager ) ;
103109standardRunTimelineMetricsManager . seedMetricsFromEnvironment ( ) ;
@@ -185,12 +191,46 @@ async function bootstrap() {
185191
186192 logger . setGlobalTaskLogger ( otelTaskLogger ) ;
187193
194+ if ( config . init ) {
195+ lifecycleHooks . registerGlobalInitHook ( {
196+ id : "config" ,
197+ fn : lifecycleHooksAdapters . createInitHookAdapter ( config . init ) ,
198+ } ) ;
199+ }
200+
201+ if ( config . onStart ) {
202+ lifecycleHooks . registerGlobalStartHook ( {
203+ id : "config" ,
204+ fn : lifecycleHooksAdapters . createStartHookAdapter ( config . onStart ) ,
205+ } ) ;
206+ }
207+
208+ if ( config . onSuccess ) {
209+ lifecycleHooks . registerGlobalSuccessHook ( {
210+ id : "config" ,
211+ fn : lifecycleHooksAdapters . createSuccessHookAdapter ( config . onSuccess ) ,
212+ } ) ;
213+ }
214+
215+ if ( config . onFailure ) {
216+ lifecycleHooks . registerGlobalFailureHook ( {
217+ id : "config" ,
218+ fn : lifecycleHooksAdapters . createFailureHookAdapter ( config . onFailure ) ,
219+ } ) ;
220+ }
221+
222+ if ( handleError ) {
223+ lifecycleHooks . registerGlobalCatchErrorHook ( {
224+ id : "config" ,
225+ fn : lifecycleHooksAdapters . createHandleErrorHookAdapter ( handleError ) ,
226+ } ) ;
227+ }
228+
188229 return {
189230 tracer,
190231 tracingSDK,
191232 consoleInterceptor,
192233 config,
193- handleErrorFn : handleError ,
194234 workerManifest,
195235 } ;
196236}
@@ -232,7 +272,7 @@ const zodIpc = new ZodIpcConnection({
232272 }
233273
234274 try {
235- const { tracer, tracingSDK, consoleInterceptor, config, handleErrorFn , workerManifest } =
275+ const { tracer, tracingSDK, consoleInterceptor, config, workerManifest } =
236276 await bootstrap ( ) ;
237277
238278 _tracingSDK = tracingSDK ;
@@ -336,8 +376,7 @@ const zodIpc = new ZodIpcConnection({
336376 tracer,
337377 tracingSDK,
338378 consoleInterceptor,
339- config,
340- handleErrorFn,
379+ retries : config . retries ,
341380 } ) ;
342381
343382 try {
@@ -355,42 +394,7 @@ const zodIpc = new ZodIpcConnection({
355394 ? timeout . abortAfterTimeout ( execution . run . maxDuration )
356395 : undefined ;
357396
358- signal ?. addEventListener ( "abort" , async ( e ) => {
359- if ( _isRunning ) {
360- _isRunning = false ;
361- _execution = undefined ;
362-
363- const usageSample = usage . stop ( measurement ) ;
364-
365- await sender . send ( "TASK_RUN_COMPLETED" , {
366- execution,
367- result : {
368- ok : false ,
369- id : execution . run . id ,
370- error : {
371- type : "INTERNAL_ERROR" ,
372- code : TaskRunErrorCodes . MAX_DURATION_EXCEEDED ,
373- message :
374- signal . reason instanceof Error
375- ? signal . reason . message
376- : String ( signal . reason ) ,
377- } ,
378- usage : {
379- durationMs : usageSample . cpuTime ,
380- } ,
381- metadata : runMetadataManager . stopAndReturnLastFlush ( ) ,
382- } ,
383- } ) ;
384- }
385- } ) ;
386-
387- const { result } = await executor . execute (
388- execution ,
389- metadata ,
390- traceContext ,
391- measurement ,
392- signal
393- ) ;
397+ const { result } = await executor . execute ( execution , metadata , traceContext , signal ) ;
394398
395399 const usageSample = usage . stop ( measurement ) ;
396400
0 commit comments