File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -54,10 +54,14 @@ export async function run() {
54
54
}
55
55
log . error ( `[strategies] failed to load ${ JSON . stringify ( e ) } ` ) ;
56
56
}
57
+
58
+ // if stop() has been called after sleep started,
59
+ // the loop will exit only after the sleep has completed
57
60
await snapshot . utils . sleep ( RUN_INTERVAL ) ;
58
61
}
59
62
}
60
63
61
64
export function stop ( ) {
65
+ log . info ( '[strategies] Stopping strategies refresh' ) ;
62
66
shouldStop = true ;
63
67
}
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ import initMetrics from './helpers/metrics';
8
8
import refreshModeration from './helpers/moderation' ;
9
9
import rateLimit from './helpers/rateLimit' ;
10
10
import shutter from './helpers/shutter' ;
11
- import { run as refreshStrategies } from './helpers/strategies' ;
11
+ import { run as refreshStrategies , stop as stopStrategies } from './helpers/strategies' ;
12
12
import { trackTurboStatuses } from './helpers/turbo' ;
13
13
14
14
const app = express ( ) ;
@@ -31,4 +31,23 @@ app.use('/shutter', shutter);
31
31
fallbackLogger ( app ) ;
32
32
33
33
const PORT = process . env . PORT || 3001 ;
34
- app . listen ( PORT , ( ) => log . info ( `Started on: http://localhost:${ PORT } ` ) ) ;
34
+ const server = app . listen ( PORT , ( ) => log . info ( `Started on: http://localhost:${ PORT } ` ) ) ;
35
+
36
+ const gracefulShutdown = ( signal : string ) => {
37
+ log . info ( `Received ${ signal } , shutting down gracefully...` ) ;
38
+
39
+ stopStrategies ( ) ;
40
+
41
+ server . close ( ( ) => {
42
+ log . info ( 'Server closed' ) ;
43
+ process . exit ( 0 ) ;
44
+ } ) ;
45
+
46
+ setTimeout ( ( ) => {
47
+ log . error ( 'Could not close connections in time, forcefully shutting down' ) ;
48
+ process . exit ( 1 ) ;
49
+ } , 10000 ) ;
50
+ } ;
51
+
52
+ process . on ( 'SIGTERM' , ( ) => gracefulShutdown ( 'SIGTERM' ) ) ;
53
+ process . on ( 'SIGINT' , ( ) => gracefulShutdown ( 'SIGINT' ) ) ;
You can’t perform that action at this time.
0 commit comments