Skip to content

Commit a13cfa7

Browse files
committed
factorise worker
1 parent 1671cda commit a13cfa7

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

src/services/redis-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class RedisService {
5252
}
5353

5454
async getTask(queueName: string) {
55-
return await this.brpopAsync(queueName, 0);
55+
return this.brpopAsync(queueName, 0);
5656
}
5757

5858
deleteAlert(alertKey: string) {

src/worker/exec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import { Context, createContext, CreateContextOptions, runInNewContext, RunningScriptOptions } from 'vm';
22
import { performance } from 'perf_hooks';
33

4+
const opt: CreateContextOptions = {
5+
name: 'checkId: xxx',
6+
codeGeneration: {
7+
strings: false
8+
},
9+
};
10+
411
export function execZmonScript(ctx: Context, script: string, additionalCtx: {value: any} | {entity: Object}) {
512
const scriptToExec = `(${script})();`
6-
const opt: CreateContextOptions = {
7-
name: 'checkId: xxx',
8-
codeGeneration: {
9-
strings: false
10-
},
11-
};
1213

1314
const localCtx = {...ctx, ...additionalCtx};
1415
const newCtx = createContext(localCtx, opt);
1516

1617
const t0 = Date.now();
18+
1719
const result = runInNewContext(scriptToExec, newCtx);
20+
1821
const t1 = Date.now();
1922

2023
return {

src/worker/zmon-worker.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import { hostname } from 'os';
12
import { RedisClient } from 'redis';
23
import { Context } from 'vm';
34
import { promisify } from 'util';
45

5-
import execCtx from '../exec-ctx/ctx';
66
import { execZmonScript } from './exec';
7-
import { hostname } from "os";
7+
import execCtx from '../exec-ctx/ctx';
88
import { RedisService } from '../services/redis-service';
99

1010

1111
export default class ZmonWorker {
12+
executedChecks = 0;
13+
1214
private execCtx: Context;
1315
private workerName: string;
1416

@@ -27,27 +29,32 @@ export default class ZmonWorker {
2729
}
2830

2931
private registerWorker() {
30-
this.redisService.registerWorker(this.workerName)
32+
this.redisService.registerWorker(this.workerName);
3133
}
3234

3335
async startWorker() {
3436
console.log('worker started');
3537
let i = 0;
3638
while (true) {
37-
const res = await this.consumeQueue();
38-
const [ _, msg ] = res;
39-
const parsedMsg = JSON.parse(msg);
40-
const {check_id, entity, command} = parsedMsg.body.args[0];
41-
const alertList = parsedMsg.body.args[1];
39+
const res: [string, string] = await this.consumeQueue();
40+
this.processMessage(res);
41+
}
42+
}
4243

43-
const checkResult = this.executeZmonTask(command, check_id, entity);
44+
private processMessage(res: [string, string]) {
45+
const [_, msg] = res;
46+
const parsedMsg = JSON.parse(msg);
47+
const {check_id, entity, command} = parsedMsg.body.args[0];
48+
const alertList = parsedMsg.body.args[1];
4449

45-
this.storeCheckResult(check_id, entity.id, JSON.stringify(checkResult));
50+
const checkResult = this.executeZmonTask(command, check_id, entity);
51+
this.executedChecks++;
4652

47-
alertList.forEach((alert: any) => {
48-
this.handleAlerts(alert, checkResult, entity);
49-
});
50-
}
53+
this.storeCheckResult(check_id, entity.id, JSON.stringify(checkResult));
54+
55+
alertList.forEach((alert: any) => {
56+
this.handleAlerts(alert, checkResult, entity);
57+
});
5158
}
5259

5360
private handleAlerts(alert: any, checkResult: any, entity: any) {
@@ -71,8 +78,8 @@ export default class ZmonWorker {
7178
}
7279
}
7380

74-
private async consumeQueue() {
75-
return await this.redisService.getTask(this.queueName);
81+
private consumeQueue() {
82+
return this.redisService.getTask(this.queueName);
7683
}
7784

7885
private executeZmonTask(checkScript: string, checkId: string, entity: any): any {

0 commit comments

Comments
 (0)