@@ -348,6 +348,7 @@ export class BackupOrchestrator {
348348 this . publishTransition ( runId , "preparing" ) ;
349349
350350 let policy = null as Awaited < ReturnType < typeof repos . backupPolicy . findById > > | null ;
351+ let ctx : RunContext | null = null ;
351352 let executor : BackupExecutor | null = null ;
352353 let serviceHandle : ServiceHandle | null = null ;
353354 // The runtime the BackupExecutor wraps. Held for the whole run (it shells into
@@ -396,7 +397,6 @@ export class BackupOrchestrator {
396397 // 3. Materialize the SOURCE — a deployed project service, or a bare
397398 // mail server. Both yield an opaque ServiceHandle + an executor +
398399 // the key/manifest metadata the shared pipeline below needs.
399- let ctx : RunContext ;
400400 if ( policy . sourceKind === "mail_server" ) {
401401 if ( ! policy . mailServerId ) throw new Error ( "mail_server policy has no mailServerId" ) ;
402402 const built = await this . buildMailSource (
@@ -654,6 +654,8 @@ export class BackupOrchestrator {
654654 payload : {
655655 projectName : ctx . projectName ,
656656 serviceName : ctx . serviceName ,
657+ policyId : policy . id ,
658+ destinationId : destinationRow . id ,
657659 destinationName : destinationRow . name ,
658660 bytesTransferred : totalBytes ,
659661 artifactCount : artifactsRecorded . length ,
@@ -739,25 +741,27 @@ export class BackupOrchestrator {
739741 bytesTransferred : 0 ,
740742 } ) ;
741743
742- // Fan-out to subscribers. We re-fetch destination if needed —
743- // the catch block may have lost the closure depending on where
744- // we threw, so look it up by policy.
745- if ( policy ?. destinationId ) {
746- const destForNotify = await repos . backupDestination
747- . findById ( policy . destinationId )
748- . catch ( ( ) => null ) ;
749- if ( destForNotify ) {
750- notification . emit ( {
751- organizationId : destForNotify . organizationId ,
752- eventType : "backup_run.failed" ,
753- resourceType : "backup_run" ,
754- resourceId : runId ,
755- payload : {
756- destinationName : destForNotify . name ,
757- errorMessage : summary ,
758- } ,
759- } ) ;
760- }
744+ // Fan-out to subscribers. Look up destination by policy or run.
745+ const destId = policy ?. destinationId ?? run . destinationId ;
746+ const destForNotify = destId
747+ ? await repos . backupDestination . findById ( destId ) . catch ( ( ) => null )
748+ : null ;
749+ const organizationId = run . organizationId ?? destForNotify ?. organizationId ;
750+ if ( organizationId ) {
751+ notification . emit ( {
752+ organizationId,
753+ eventType : "backup_run.failed" ,
754+ resourceType : "backup_run" ,
755+ resourceId : runId ,
756+ payload : {
757+ destinationName : destForNotify ?. name ?? null ,
758+ destinationId : destId ?? null ,
759+ policyId : policy ?. id ?? run . policyId ?? null ,
760+ projectName : ctx ?. projectName ?? null ,
761+ serviceName : ctx ?. serviceName ?? null ,
762+ errorMessage : summary ,
763+ } ,
764+ } ) ;
761765 }
762766 } finally {
763767 disposeRuntime ( sourceRuntime ) ;
0 commit comments