1
+ import { hostname } from 'os' ;
1
2
import { RedisClient } from 'redis' ;
2
3
import { Context } from 'vm' ;
3
4
import { promisify } from 'util' ;
4
5
5
- import execCtx from '../exec-ctx/ctx' ;
6
6
import { execZmonScript } from './exec' ;
7
- import { hostname } from "os" ;
7
+ import execCtx from '../exec-ctx/ctx' ;
8
8
import { RedisService } from '../services/redis-service' ;
9
9
10
10
11
11
export default class ZmonWorker {
12
+ executedChecks = 0 ;
13
+
12
14
private execCtx : Context ;
13
15
private workerName : string ;
14
16
@@ -27,27 +29,32 @@ export default class ZmonWorker {
27
29
}
28
30
29
31
private registerWorker ( ) {
30
- this . redisService . registerWorker ( this . workerName )
32
+ this . redisService . registerWorker ( this . workerName ) ;
31
33
}
32
34
33
35
async startWorker ( ) {
34
36
console . log ( 'worker started' ) ;
35
37
let i = 0 ;
36
38
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
+ }
42
43
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 ] ;
44
49
45
- this . storeCheckResult ( check_id , entity . id , JSON . stringify ( checkResult ) ) ;
50
+ const checkResult = this . executeZmonTask ( command , check_id , entity ) ;
51
+ this . executedChecks ++ ;
46
52
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
+ } ) ;
51
58
}
52
59
53
60
private handleAlerts ( alert : any , checkResult : any , entity : any ) {
@@ -71,8 +78,8 @@ export default class ZmonWorker {
71
78
}
72
79
}
73
80
74
- private async consumeQueue ( ) {
75
- return await this . redisService . getTask ( this . queueName ) ;
81
+ private consumeQueue ( ) {
82
+ return this . redisService . getTask ( this . queueName ) ;
76
83
}
77
84
78
85
private executeZmonTask ( checkScript : string , checkId : string , entity : any ) : any {
0 commit comments