11import type { Tracer } from "@opentelemetry/api" ;
22import type { Logger } from "@opentelemetry/api-logs" ;
33import {
4+ AnyOnCatchErrorHookFunction ,
5+ AnyOnFailureHookFunction ,
6+ AnyOnInitHookFunction ,
7+ AnyOnStartHookFunction ,
8+ AnyOnSuccessHookFunction ,
49 apiClientManager ,
510 clock ,
611 ExecutorToWorkerMessageCatalog ,
712 type HandleErrorFunction ,
13+ lifecycleHooks ,
14+ localsAPI ,
815 logger ,
916 LogLevel ,
17+ resourceCatalog ,
1018 runMetadata ,
1119 runtime ,
12- resourceCatalog ,
20+ runTimelineMetrics ,
1321 TaskRunErrorCodes ,
1422 TaskRunExecution ,
1523 timeout ,
1624 TriggerConfig ,
1725 waitUntil ,
1826 WorkerManifest ,
1927 WorkerToExecutorMessageCatalog ,
20- runTimelineMetrics ,
2128} from "@trigger.dev/core/v3" ;
2229import { TriggerTracer } from "@trigger.dev/core/v3/tracer" ;
2330import {
@@ -29,15 +36,17 @@ import {
2936 logLevels ,
3037 ManagedRuntimeManager ,
3138 OtelTaskLogger ,
39+ StandardLifecycleHooksManager ,
40+ StandardLocalsManager ,
3241 StandardMetadataManager ,
3342 StandardResourceCatalog ,
43+ StandardRunTimelineMetricsManager ,
3444 StandardWaitUntilManager ,
3545 TaskExecutor ,
3646 TracingDiagnosticLogLevel ,
3747 TracingSDK ,
3848 usage ,
3949 UsageTimeoutManager ,
40- StandardRunTimelineMetricsManager ,
4150} from "@trigger.dev/core/v3/workers" ;
4251import { ZodIpcConnection } from "@trigger.dev/core/v3/zodIpc" ;
4352import { readFile } from "node:fs/promises" ;
@@ -89,10 +98,16 @@ process.on("uncaughtException", function (error, origin) {
8998
9099const heartbeatIntervalMs = getEnvVar ( "HEARTBEAT_INTERVAL_MS" ) ;
91100
101+ const standardLocalsManager = new StandardLocalsManager ( ) ;
102+ localsAPI . setGlobalLocalsManager ( standardLocalsManager ) ;
103+
92104const standardRunTimelineMetricsManager = new StandardRunTimelineMetricsManager ( ) ;
93105runTimelineMetrics . setGlobalManager ( standardRunTimelineMetricsManager ) ;
94106standardRunTimelineMetricsManager . seedMetricsFromEnvironment ( ) ;
95107
108+ const standardLifecycleHooksManager = new StandardLifecycleHooksManager ( ) ;
109+ lifecycleHooks . setGlobalLifecycleHooksManager ( standardLifecycleHooksManager ) ;
110+
96111const devUsageManager = new DevUsageManager ( ) ;
97112usage . setGlobalUsageManager ( devUsageManager ) ;
98113timeout . setGlobalManager ( new UsageTimeoutManager ( devUsageManager ) ) ;
@@ -170,12 +185,46 @@ async function bootstrap() {
170185
171186 logger . setGlobalTaskLogger ( otelTaskLogger ) ;
172187
188+ if ( config . init ) {
189+ lifecycleHooks . registerGlobalInitHook ( {
190+ id : "config" ,
191+ fn : config . init as AnyOnInitHookFunction ,
192+ } ) ;
193+ }
194+
195+ if ( config . onStart ) {
196+ lifecycleHooks . registerGlobalStartHook ( {
197+ id : "config" ,
198+ fn : config . onStart as AnyOnStartHookFunction ,
199+ } ) ;
200+ }
201+
202+ if ( config . onSuccess ) {
203+ lifecycleHooks . registerGlobalSuccessHook ( {
204+ id : "config" ,
205+ fn : config . onSuccess as AnyOnSuccessHookFunction ,
206+ } ) ;
207+ }
208+
209+ if ( config . onFailure ) {
210+ lifecycleHooks . registerGlobalFailureHook ( {
211+ id : "config" ,
212+ fn : config . onFailure as AnyOnFailureHookFunction ,
213+ } ) ;
214+ }
215+
216+ if ( handleError ) {
217+ lifecycleHooks . registerGlobalCatchErrorHook ( {
218+ id : "config" ,
219+ fn : handleError as AnyOnCatchErrorHookFunction ,
220+ } ) ;
221+ }
222+
173223 return {
174224 tracer,
175225 tracingSDK,
176226 consoleInterceptor,
177227 config,
178- handleErrorFn : handleError ,
179228 workerManifest,
180229 } ;
181230}
@@ -217,7 +266,7 @@ const zodIpc = new ZodIpcConnection({
217266 }
218267
219268 try {
220- const { tracer, tracingSDK, consoleInterceptor, config, handleErrorFn , workerManifest } =
269+ const { tracer, tracingSDK, consoleInterceptor, config, workerManifest } =
221270 await bootstrap ( ) ;
222271
223272 _tracingSDK = tracingSDK ;
@@ -257,6 +306,18 @@ const zodIpc = new ZodIpcConnection({
257306 async ( ) => {
258307 const beforeImport = performance . now ( ) ;
259308 resourceCatalog . setCurrentFileContext ( taskManifest . entryPoint , taskManifest . filePath ) ;
309+
310+ // Load init file if it exists
311+ if ( workerManifest . initEntryPoint ) {
312+ try {
313+ await import ( normalizeImportPath ( workerManifest . initEntryPoint ) ) ;
314+ log ( `Loaded init file from ${ workerManifest . initEntryPoint } ` ) ;
315+ } catch ( err ) {
316+ logError ( `Failed to load init file` , err ) ;
317+ throw err ;
318+ }
319+ }
320+
260321 await import ( normalizeImportPath ( taskManifest . entryPoint ) ) ;
261322 resourceCatalog . clearCurrentFileContext ( ) ;
262323 const durationMs = performance . now ( ) - beforeImport ;
@@ -321,8 +382,7 @@ const zodIpc = new ZodIpcConnection({
321382 tracer,
322383 tracingSDK,
323384 consoleInterceptor,
324- config,
325- handleErrorFn,
385+ retries : config . retries ,
326386 } ) ;
327387
328388 try {
@@ -340,42 +400,7 @@ const zodIpc = new ZodIpcConnection({
340400 ? timeout . abortAfterTimeout ( execution . run . maxDuration )
341401 : undefined ;
342402
343- signal ?. addEventListener ( "abort" , async ( e ) => {
344- if ( _isRunning ) {
345- _isRunning = false ;
346- _execution = undefined ;
347-
348- const usageSample = usage . stop ( measurement ) ;
349-
350- await sender . send ( "TASK_RUN_COMPLETED" , {
351- execution,
352- result : {
353- ok : false ,
354- id : execution . run . id ,
355- error : {
356- type : "INTERNAL_ERROR" ,
357- code : TaskRunErrorCodes . MAX_DURATION_EXCEEDED ,
358- message :
359- signal . reason instanceof Error
360- ? signal . reason . message
361- : String ( signal . reason ) ,
362- } ,
363- usage : {
364- durationMs : usageSample . cpuTime ,
365- } ,
366- metadata : runMetadataManager . stopAndReturnLastFlush ( ) ,
367- } ,
368- } ) ;
369- }
370- } ) ;
371-
372- const { result } = await executor . execute (
373- execution ,
374- metadata ,
375- traceContext ,
376- measurement ,
377- signal
378- ) ;
403+ const { result } = await executor . execute ( execution , metadata , traceContext , signal ) ;
379404
380405 const usageSample = usage . stop ( measurement ) ;
381406
0 commit comments