1
1
import type { Tracer } from "@opentelemetry/api" ;
2
2
import type { Logger } from "@opentelemetry/api-logs" ;
3
3
import {
4
+ AnyOnCatchErrorHookFunction ,
5
+ AnyOnFailureHookFunction ,
6
+ AnyOnInitHookFunction ,
7
+ AnyOnStartHookFunction ,
8
+ AnyOnSuccessHookFunction ,
4
9
apiClientManager ,
5
10
clock ,
6
11
ExecutorToWorkerMessageCatalog ,
7
12
type HandleErrorFunction ,
13
+ lifecycleHooks ,
14
+ localsAPI ,
8
15
logger ,
9
16
LogLevel ,
17
+ resourceCatalog ,
10
18
runMetadata ,
11
19
runtime ,
12
- resourceCatalog ,
20
+ runTimelineMetrics ,
13
21
TaskRunErrorCodes ,
14
22
TaskRunExecution ,
15
23
timeout ,
16
24
TriggerConfig ,
17
25
waitUntil ,
18
26
WorkerManifest ,
19
27
WorkerToExecutorMessageCatalog ,
20
- runTimelineMetrics ,
21
28
} from "@trigger.dev/core/v3" ;
22
29
import { TriggerTracer } from "@trigger.dev/core/v3/tracer" ;
23
30
import {
@@ -29,15 +36,17 @@ import {
29
36
logLevels ,
30
37
ManagedRuntimeManager ,
31
38
OtelTaskLogger ,
39
+ StandardLifecycleHooksManager ,
40
+ StandardLocalsManager ,
32
41
StandardMetadataManager ,
33
42
StandardResourceCatalog ,
43
+ StandardRunTimelineMetricsManager ,
34
44
StandardWaitUntilManager ,
35
45
TaskExecutor ,
36
46
TracingDiagnosticLogLevel ,
37
47
TracingSDK ,
38
48
usage ,
39
49
UsageTimeoutManager ,
40
- StandardRunTimelineMetricsManager ,
41
50
} from "@trigger.dev/core/v3/workers" ;
42
51
import { ZodIpcConnection } from "@trigger.dev/core/v3/zodIpc" ;
43
52
import { readFile } from "node:fs/promises" ;
@@ -89,10 +98,16 @@ process.on("uncaughtException", function (error, origin) {
89
98
90
99
const heartbeatIntervalMs = getEnvVar ( "HEARTBEAT_INTERVAL_MS" ) ;
91
100
101
+ const standardLocalsManager = new StandardLocalsManager ( ) ;
102
+ localsAPI . setGlobalLocalsManager ( standardLocalsManager ) ;
103
+
92
104
const standardRunTimelineMetricsManager = new StandardRunTimelineMetricsManager ( ) ;
93
105
runTimelineMetrics . setGlobalManager ( standardRunTimelineMetricsManager ) ;
94
106
standardRunTimelineMetricsManager . seedMetricsFromEnvironment ( ) ;
95
107
108
+ const standardLifecycleHooksManager = new StandardLifecycleHooksManager ( ) ;
109
+ lifecycleHooks . setGlobalLifecycleHooksManager ( standardLifecycleHooksManager ) ;
110
+
96
111
const devUsageManager = new DevUsageManager ( ) ;
97
112
usage . setGlobalUsageManager ( devUsageManager ) ;
98
113
timeout . setGlobalManager ( new UsageTimeoutManager ( devUsageManager ) ) ;
@@ -170,12 +185,46 @@ async function bootstrap() {
170
185
171
186
logger . setGlobalTaskLogger ( otelTaskLogger ) ;
172
187
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
+
173
223
return {
174
224
tracer,
175
225
tracingSDK,
176
226
consoleInterceptor,
177
227
config,
178
- handleErrorFn : handleError ,
179
228
workerManifest,
180
229
} ;
181
230
}
@@ -217,7 +266,7 @@ const zodIpc = new ZodIpcConnection({
217
266
}
218
267
219
268
try {
220
- const { tracer, tracingSDK, consoleInterceptor, config, handleErrorFn , workerManifest } =
269
+ const { tracer, tracingSDK, consoleInterceptor, config, workerManifest } =
221
270
await bootstrap ( ) ;
222
271
223
272
_tracingSDK = tracingSDK ;
@@ -257,6 +306,18 @@ const zodIpc = new ZodIpcConnection({
257
306
async ( ) => {
258
307
const beforeImport = performance . now ( ) ;
259
308
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
+
260
321
await import ( normalizeImportPath ( taskManifest . entryPoint ) ) ;
261
322
resourceCatalog . clearCurrentFileContext ( ) ;
262
323
const durationMs = performance . now ( ) - beforeImport ;
@@ -321,8 +382,7 @@ const zodIpc = new ZodIpcConnection({
321
382
tracer,
322
383
tracingSDK,
323
384
consoleInterceptor,
324
- config,
325
- handleErrorFn,
385
+ retries : config . retries ,
326
386
} ) ;
327
387
328
388
try {
@@ -340,42 +400,7 @@ const zodIpc = new ZodIpcConnection({
340
400
? timeout . abortAfterTimeout ( execution . run . maxDuration )
341
401
: undefined ;
342
402
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 ) ;
379
404
380
405
const usageSample = usage . stop ( measurement ) ;
381
406
0 commit comments