@@ -164,8 +164,10 @@ function buildRunner(input: {
164
164
} ) ;
165
165
166
166
let running = true ;
167
- const stop = async ( ) => {
168
- compiledOptions . logger . debug ( "Runner stopping" ) ;
167
+ const stop = async ( reason : string | null , itsFine = reason === null ) => {
168
+ compiledOptions . logger [ itsFine ? "debug" : "warn" ] (
169
+ `Runner stopping${ reason ? ` (reason: ${ reason } )` : "" } ` ,
170
+ ) ;
169
171
if ( running ) {
170
172
running = false ;
171
173
events . emit ( "stop" , { ctx } ) ;
@@ -190,25 +192,19 @@ function buildRunner(input: {
190
192
throw new Error ( "Runner is already stopped" ) ;
191
193
}
192
194
} ;
193
- const kill = async ( ) => {
194
- if ( running ) {
195
- stop ( ) . catch ( ( ) => { } ) ;
196
- }
197
- if ( workerPool . _active ) {
198
- await workerPool . forcefulShutdown ( `Terminated through .kill() command` ) ;
199
- }
200
- } ;
201
195
202
- const wp = workerPool . promise . finally ( ( ) => {
203
- if ( running ) {
204
- stop ( ) ;
205
- }
206
- } ) ;
207
- const cp = cron . promise . finally ( ( ) => {
208
- if ( running ) {
209
- stop ( ) ;
210
- }
211
- } ) ;
196
+ const wp = workerPool . promise
197
+ . then (
198
+ ( ) => ( running ? stop ( "worker pool exited cleanly" , true ) : void 0 ) ,
199
+ ( e ) => ( running ? stop ( `worker pool exited with error: ${ e } ` ) : void 0 ) ,
200
+ )
201
+ . catch ( noop ) ;
202
+ const cp = cron . promise
203
+ . then (
204
+ ( ) => ( running ? stop ( "cron exited cleanly" , true ) : void 0 ) ,
205
+ ( e ) => ( running ? stop ( `cron exited with error: ${ e } ` ) : void 0 ) ,
206
+ )
207
+ . catch ( noop ) ;
212
208
213
209
const promise = Promise . all ( [ cp , wp ] ) . then (
214
210
( ) => {
@@ -217,7 +213,7 @@ function buildRunner(input: {
217
213
async ( error ) => {
218
214
if ( running ) {
219
215
logger . error ( `Stopping worker due to an error: ${ error } ` , { error } ) ;
220
- await stop ( ) ;
216
+ await stop ( String ( error ) ) ;
221
217
} else {
222
218
logger . error (
223
219
`Error occurred, but worker is already stopping: ${ error } ` ,
@@ -229,10 +225,22 @@ function buildRunner(input: {
229
225
) ;
230
226
231
227
return {
232
- stop,
233
- kill,
228
+ // It's fine - the user told us to exit
229
+ stop : ( reason ) => stop ( reason ?? null , true ) ,
230
+ async kill ( reason ?: string ) {
231
+ if ( running ) {
232
+ stop ( `runner.kill() called${ reason ? `: ${ reason } ` : "" } ` ) . catch ( noop ) ;
233
+ }
234
+ if ( workerPool . _active ) {
235
+ await workerPool . forcefulShutdown ( `Terminated through .kill() command` ) ;
236
+ }
237
+ } ,
234
238
addJob,
235
239
promise,
236
240
events,
237
241
} ;
238
242
}
243
+
244
+ function noop ( ) {
245
+ /* NOOP */
246
+ }
0 commit comments