Skip to content

Commit 61bf3d9

Browse files
committed
Improve wss initalization #99 && Improve closing application in docker #100
1 parent de1d857 commit 61bf3d9

7 files changed

Lines changed: 42 additions & 29 deletions

File tree

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 5.12.0
2+
3+
### Changes
4+
* Improve wss initalization #99
5+
* Improve closing application in docker #100
6+
* Update npm packages
7+
8+
### Breaking changes
9+
10+
* Change logs format from short to structured #100
11+
112
## 5.11.0
213

314
### Changes

Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ WORKDIR /opt/elastalert-server
6666
COPY package*.json ./
6767
RUN npm ci --omit=dev
6868

69-
COPY scripts scripts
70-
7169
COPY config/elastalert.yaml /opt/elastalert/config.yaml
7270
COPY config/elastalert-test.yaml /opt/elastalert/config-test.yaml
7371
COPY config/config.json config/config.json
@@ -82,4 +80,4 @@ RUN mkdir -p /opt/elastalert/rules/ /opt/elastalert/server_data/tests/ \
8280
USER node
8381

8482
EXPOSE 3030
85-
ENTRYPOINT ["npm", "start"]
83+
CMD ["node", "dist/index.js"]

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "elastalert2-server",
3-
"version": "5.11.0",
3+
"version": "5.12.0",
44
"description": "A server that runs ElastAlert2 and exposes REST API's for manipulating rules and alerts.",
55
"license": "MIT",
66
"main": "index.js",
@@ -45,7 +45,7 @@
4545
"scripts": {
4646
"dev": "concurrently \"nodemon\" \"nodemon --config nodemon-spec.json\"",
4747
"build": "tsoa spec-and-routes && tsc",
48-
"start": "sh ./scripts/start.sh",
48+
"start": "node dist/index.js",
4949
"serve": "nodemon --inspect --watch dist/ -d 1 ./dist/index.js",
5050
"tsc:watch": "tsc -w",
5151
"tsc:build": "tsc"

scripts/start.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/common/websocket.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ interface ExtWebSocket extends WebSocket {
44
isAlive: boolean;
55
}
66

7-
export let wss: WebSocket.Server;
8-
97
export function listen(port: number): WebSocket.Server {
10-
wss = new WebSocket.Server({ port, path: '/test' });
8+
let wss = new WebSocket.Server({ port, path: '/test' });
119

1210
wss.on('connection', (ws: ExtWebSocket) => {
1311
ws.isAlive = true;
@@ -16,19 +14,19 @@ export function listen(port: number): WebSocket.Server {
1614
});
1715
});
1816

19-
return wss;
20-
}
17+
// Keepalive in case clients lose connection during a long rule test.
18+
// If client doesn't respond in 10s this will close the socket and
19+
// therefore stop the elastalert test from continuing to run detached.
20+
setInterval(() => {
21+
wss.clients.forEach(ws => {
22+
let extWs = <ExtWebSocket>ws;
2123

22-
// Keepalive in case clients lose connection during a long rule test.
23-
// If client doesn't respond in 10s this will close the socket and
24-
// therefore stop the elastalert test from continuing to run detached.
25-
setInterval(() => {
26-
wss.clients.forEach(ws => {
27-
let extWs = <ExtWebSocket>ws;
24+
if (extWs.isAlive === false) {return ws.terminate();
25+
}
26+
extWs.isAlive = false;
27+
extWs.ping(() => {});
28+
});
29+
}, 10000);
2830

29-
if (extWs.isAlive === false) {return ws.terminate();
30-
}
31-
extWs.isAlive = false;
32-
extWs.ping(() => {});
33-
});
34-
}, 10000);
31+
return wss;
32+
}

src/elastalert_server.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express, { Express, Response as ExResponse,
22
Request as ExRequest,
33
NextFunction,} from 'express';
4+
import WebSocket from 'ws';
45
import bodyParser from 'body-parser';
56
import Logger from './common/logger';
67
import config from './common/config';
@@ -22,6 +23,7 @@ let logger = new Logger('Server');
2223

2324
export 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

Comments
 (0)