11import express , { Express , Response as ExResponse ,
22 Request as ExRequest ,
33 NextFunction , } from 'express' ;
4+ import WebSocket from 'ws' ;
45import bodyParser from 'body-parser' ;
56import Logger from './common/logger' ;
67import config from './common/config' ;
@@ -22,6 +23,7 @@ let logger = new Logger('Server');
2223
2324export default class ElastalertServer {
2425 private _express : Express ;
26+ private _wss ?: WebSocket . Server ;
2527 private _runningTimeouts : NodeJS . Timeout [ ] ;
2628
2729 private _processService : ProcessService ;
@@ -48,6 +50,12 @@ export default class ElastalertServer {
4850
4951 // Set listener on process exit (SIGINT == ^C)
5052 process . on ( 'SIGINT' , ( ) => {
53+ logger . info ( 'Received signal: SIGINT' ) ;
54+ process . exit ( 0 ) ;
55+ } ) ;
56+
57+ process . on ( 'SIGTERM' , ( ) => {
58+ logger . info ( 'Received signal: SIGTERM' ) ;
5159 process . exit ( 0 ) ;
5260 } ) ;
5361
@@ -147,9 +155,9 @@ export default class ElastalertServer {
147155
148156 logger . info ( 'Server listening on port ' + config . get ( ) . port ) ;
149157
150- let wss = listen ( config . get ( ) . wsport ) ;
158+ self . _wss = listen ( config . get ( ) . wsport ) ;
151159
152- wss . on ( 'connection' , ( ws ) => {
160+ self . _wss . on ( 'connection' , ( ws ) => {
153161 ws . on ( 'message' , ( message ) => {
154162 try {
155163 let data = JSON . parse ( message . toString ( ) ) ;
@@ -176,6 +184,7 @@ export default class ElastalertServer {
176184 stop ( ) {
177185 this . _processService ?. stop ( ) ;
178186 this . _runningServer ?. close ( ) ;
187+ this . _wss ?. close ( ) ;
179188
180189 this . _runningTimeouts . forEach ( ( timeout ) => clearTimeout ( timeout ) ) ;
181190 }
0 commit comments