11import * as Sentry from "@sentry/node" ;
22import log from "./utils/log" ;
3+ import { client } from "./client" ;
34
4- process . on ( "SIGHUP" , function ( ) {
5- process . exit ( 0 ) ;
6- } ) ;
5+ async function gracefulShutdown ( signal : string ) {
6+ log . info ( "Process" , `Received ${ signal } , shutting down...` ) ;
77
8- process . on ( "SIGTERM" , function ( ) {
9- process . exit ( 0 ) ;
10- } ) ;
8+ try {
9+ if ( client . pupBrowser ) {
10+ await client . pupBrowser . close ( ) ;
11+ log . info ( "Browser" , "Puppeteer browser closed successfully." ) ;
12+ }
13+ } catch ( err ) {
14+ log . error ( "Browser" , `Error closing browser: ${ ( err as Error ) . message } ` ) ;
15+ }
1116
12- process . on ( "SIGINT" , function ( ) {
13- process . kill ( process . pid ) ;
1417 process . exit ( 0 ) ;
18+ }
19+
20+ [ "SIGINT" , "SIGTERM" , "SIGHUP" ] . forEach ( ( signal ) => {
21+ process . on ( signal , ( ) => gracefulShutdown ( signal ) ) ;
1522} ) ;
1623
1724process . on ( "uncaughtException" , ( err , origin ) => {
@@ -26,14 +33,6 @@ process.on("unhandledRejection", (reason, promise) => {
2633 log . error ( "UnhandledRejection" , `Reason: ${ reason } \nPromise: ${ promise } ` ) ;
2734} ) ;
2835
29- process . on ( "beforeExit" , ( code ) => {
30- log . info ( "BeforeExit" , `Process is about to exit with code: ${ code } ` ) ;
31- } ) ;
32-
33- process . on ( "exit" , ( code ) => {
34- console . log ( "" ) ;
35- } ) ;
36-
3736log . info ( "Bot" , `Initiating ${ process . env . PROJECT_CANIS_ALIAS || "Canis" } ...` ) ;
3837log . info ( "Bot" , `prefix: ${ process . env . COMMAND_PREFIX || "!" } ` ) ;
3938log . info ( "Process" , "Event listeners for process signals have been set up." ) ;
0 commit comments