@@ -762,6 +762,15 @@ export const layer = Layer.effect(
762762 ) ,
763763 ) ;
764764
765+ const releaseRun = ( run : Pick < SettledRun , "taskId" | "startedAtIso" > , message : string ) =>
766+ Effect . result ( findTask ( run . taskId ) ) . pipe (
767+ Effect . flatMap ( ( found ) =>
768+ Result . isSuccess ( found ) && found . success !== null
769+ ? releaseStuckRun ( found . success , message , run . startedAtIso , undefined )
770+ : Effect . void ,
771+ ) ,
772+ ) ;
773+
765774 // Each outcome stands alone: one that still cannot be saved after retries is
766775 // released as failed so its row does not stay `running` and block the task.
767776 const settleOutcome = Effect . fn ( "ScheduledTaskService.settleOutcome" ) ( function * (
@@ -787,17 +796,7 @@ export const layer = Layer.effect(
787796 Effect . retry ( { times : 2 } ) ,
788797 Effect . catchCause ( ( cause ) =>
789798 Effect . logWarning ( "Could not record scheduled task run outcome" , { cause } ) . pipe (
790- Effect . andThen ( Effect . result ( findTask ( run . taskId ) ) ) ,
791- Effect . flatMap ( ( found ) =>
792- Result . isSuccess ( found ) && found . success !== null
793- ? releaseStuckRun (
794- found . success ,
795- "Could not record how the run ended." ,
796- run . startedAtIso ,
797- undefined ,
798- )
799- : Effect . void ,
800- ) ,
799+ Effect . andThen ( releaseRun ( run , "Could not record how the run ended." ) ) ,
801800 ) ,
802801 ) ,
803802 ) ;
@@ -1147,11 +1146,28 @@ export const layer = Layer.effect(
11471146 const catchUpRuns = Effect . suspend ( ( ) =>
11481147 Effect . forEach (
11491148 trackedRuns . pending ( ) ,
1150- ( { messageId , threadId } ) =>
1149+ ( pending ) =>
11511150 Effect . gen ( function * ( ) {
1152- const { messages } = yield * threadManagement . getThreadRecords ( threadId , [ "messages" ] , {
1153- messageIds : [ messageId ] ,
1154- } ) ;
1151+ const { messageId, threadId } = pending ;
1152+ const { thread, messages } = yield * threadManagement . getThreadRecords (
1153+ threadId ,
1154+ [ "messages" ] ,
1155+ { messageIds : [ messageId ] } ,
1156+ ) ;
1157+ const occurredAt = yield * DateTime . now ;
1158+ // Deleting a thread keeps its projection, flagged, so a delete missed
1159+ // with the stream shows up here rather than as a read failure.
1160+ if ( thread . deletedAt !== null ) {
1161+ const ended = trackedRuns . observe ( {
1162+ id : EventId . make ( `scheduled-task-catch-up:${ threadId } :deleted` ) ,
1163+ type : "thread.deleted" ,
1164+ threadId,
1165+ occurredAt,
1166+ payload : undefined as never ,
1167+ } ) ;
1168+ yield * Effect . forEach ( ended , settleOutcome , { discard : true } ) ;
1169+ return ;
1170+ }
11551171 const runId = messages [ 0 ] ?. runId ?? null ;
11561172 if ( runId === null ) return ;
11571173 const { runs } = yield * threadManagement . getThreadRecords ( threadId , [ "runs" ] , {
@@ -1160,7 +1176,6 @@ export const layer = Layer.effect(
11601176 const run = runs [ 0 ] ;
11611177 if ( run === undefined ) return ;
11621178 trackedRuns . bindRun ( messageId , runId ) ;
1163- const occurredAt = yield * DateTime . now ;
11641179 const ended = trackedRuns . observe ( {
11651180 id : EventId . make ( `scheduled-task-catch-up:${ runId } ` ) ,
11661181 type : "run.updated" ,
@@ -1171,8 +1186,21 @@ export const layer = Layer.effect(
11711186 } ) ;
11721187 yield * Effect . forEach ( ended , settleOutcome , { discard : true } ) ;
11731188 } ) . pipe (
1189+ // A thread that cannot be read (deleted, records gone) would otherwise
1190+ // be retried by every resubscribe and nothing else would settle its
1191+ // row, so after a few tries the run is released as failed.
1192+ Effect . retry ( { times : 3 , schedule : Schedule . exponential ( "500 millis" ) } ) ,
11741193 Effect . catchCause ( ( cause ) =>
1175- Effect . logWarning ( "Could not catch up a scheduled task run" , { messageId, cause } ) ,
1194+ Effect . logWarning ( "Could not catch up a scheduled task run" , {
1195+ messageId : pending . messageId ,
1196+ cause,
1197+ } ) . pipe (
1198+ Effect . andThen (
1199+ Effect . sync ( ( ) => trackedRuns . forgetTask ( pending . taskId ) ) . pipe (
1200+ Effect . andThen ( releaseRun ( pending , "Could not read how the run ended." ) ) ,
1201+ ) ,
1202+ ) ,
1203+ ) ,
11761204 ) ,
11771205 ) ,
11781206 { discard : true } ,
0 commit comments