@@ -45,7 +45,10 @@ export interface RunInLoopOptions {
45
45
initialDelayMs ?: number ;
46
46
}
47
47
48
- export const runInLoop = async ( fn : ( ) => Promise < void > , options : RunInLoopOptions ) => {
48
+ export const runInLoop = async (
49
+ fn : ( ) => Promise < { shouldContinueRunning : boolean } | void > ,
50
+ options : RunInLoopOptions
51
+ ) => {
49
52
const {
50
53
logger,
51
54
logLabel,
@@ -73,7 +76,7 @@ export const runInLoop = async (fn: () => Promise<void>, options: RunInLoopOptio
73
76
74
77
if ( enabled ) {
75
78
const context = logLabel ? { executionId, label : logLabel } : { executionId } ;
76
- await logger . runWithContext ( context , async ( ) => {
79
+ const shouldContinueRunning = await logger . runWithContext ( context , async ( ) => {
77
80
const goRes = await go ( fn , hardTimeoutMs ? { totalTimeoutMs : hardTimeoutMs } : { } ) ; // NOTE: This is a safety net to prevent the loop from hanging
78
81
if ( ! goRes . success ) {
79
82
logger . error ( `Unexpected runInLoop error` , goRes . error ) ;
@@ -85,7 +88,14 @@ export const runInLoop = async (fn: () => Promise<void>, options: RunInLoopOptio
85
88
} else {
86
89
logger . info ( `Execution finished` , { executionTimeMs } ) ;
87
90
}
91
+
92
+ return goRes . data ?. shouldContinueRunning === false ? false : true ;
88
93
} ) ;
94
+
95
+ // Stop loop execution if the callback return value indicates it should stop
96
+ if ( ! shouldContinueRunning ) {
97
+ break ;
98
+ }
89
99
} else {
90
100
// If the bot is disabled, we still want to run the loop to prevent the process from hanging. We also want to
91
101
// sleep according to the wait time logic.
0 commit comments