@@ -460,57 +460,78 @@ export class TaskExecutor {
460460 initOutput : any ,
461461 signal ?: AbortSignal
462462 ) {
463- await this . #callOnFailureFunction(
464- this . task . fns . onFailure ,
465- "task.onFailure" ,
466- payload ,
467- error ,
468- ctx ,
469- initOutput ,
470- signal
471- ) ;
463+ const globalFailureHooks = lifecycleHooks . getGlobalFailureHooks ( ) ;
464+ const taskFailureHook = lifecycleHooks . getTaskFailureHook ( this . task . id ) ;
472465
473- await this . #callOnFailureFunction(
474- this . _importedConfig ?. onFailure ,
475- "config.onFailure" ,
476- payload ,
477- error ,
478- ctx ,
479- initOutput ,
480- signal
481- ) ;
482- }
483-
484- async #callOnFailureFunction(
485- onFailureFn : TaskMetadataWithFunctions [ "fns" ] [ "onFailure" ] ,
486- name : string ,
487- payload : unknown ,
488- error : unknown ,
489- ctx : TaskRunContext ,
490- initOutput : any ,
491- signal ?: AbortSignal
492- ) {
493- if ( ! onFailureFn ) {
466+ if ( globalFailureHooks . length === 0 && ! taskFailureHook ) {
494467 return ;
495468 }
496469
497- try {
498- return await this . _tracer . startActiveSpan (
499- name ,
500- async ( span ) => {
501- return await runTimelineMetrics . measureMetric ( "trigger.dev/execution" , name , ( ) =>
502- onFailureFn ( payload , error , { ctx, init : initOutput , signal } )
503- ) ;
470+ return this . _tracer . startActiveSpan (
471+ "hooks.failure" ,
472+ async ( span ) => {
473+ return await runTimelineMetrics . measureMetric (
474+ "trigger.dev/execution" ,
475+ "failure" ,
476+ async ( ) => {
477+ for ( const hook of globalFailureHooks ) {
478+ try {
479+ await this . _tracer . startActiveSpan (
480+ hook . name ?? "global" ,
481+ async ( span ) => {
482+ await hook . fn ( {
483+ payload,
484+ error,
485+ ctx,
486+ signal,
487+ task : this . task . id ,
488+ init : initOutput ,
489+ } ) ;
490+ } ,
491+ {
492+ attributes : {
493+ [ SemanticInternalAttributes . STYLE_ICON ] : "tabler-function" ,
494+ } ,
495+ }
496+ ) ;
497+ } catch {
498+ // Ignore errors from onFailure functions
499+ }
500+ }
501+
502+ if ( taskFailureHook ) {
503+ try {
504+ await this . _tracer . startActiveSpan (
505+ "task" ,
506+ async ( span ) => {
507+ await taskFailureHook ( {
508+ payload,
509+ error,
510+ ctx,
511+ signal,
512+ task : this . task . id ,
513+ init : initOutput ,
514+ } ) ;
515+ } ,
516+ {
517+ attributes : {
518+ [ SemanticInternalAttributes . STYLE_ICON ] : "tabler-function" ,
519+ } ,
520+ }
521+ ) ;
522+ } catch {
523+ // Ignore errors from onFailure functions
524+ }
525+ }
526+ }
527+ ) ;
528+ } ,
529+ {
530+ attributes : {
531+ [ SemanticInternalAttributes . STYLE_ICON ] : "tabler-function" ,
504532 } ,
505- {
506- attributes : {
507- [ SemanticInternalAttributes . STYLE_ICON ] : "function" ,
508- } ,
509- }
510- ) ;
511- } catch ( e ) {
512- // Ignore errors from onFailure functions
513- }
533+ }
534+ ) ;
514535 }
515536
516537 async #parsePayload( payload : unknown ) {
0 commit comments