File tree 1 file changed +22
-10
lines changed 1 file changed +22
-10
lines changed Original file line number Diff line number Diff line change @@ -7,43 +7,55 @@ class CheckCompletion {
7
7
host : configRedis . auxRedisHost ,
8
8
port : configRedis . auxRedisPort ,
9
9
} ;
10
+
11
+ this . redis = null ;
10
12
}
11
13
12
14
async setInitialJobCounter (
13
15
key ,
14
16
value ,
15
17
) {
16
- const redis = new Redis (
17
- this . options ,
18
- ) ;
19
-
18
+ const redis = this . newRedis ( ) ;
20
19
const result = await redis . set ( key , value ) ;
20
+
21
21
console . log ( `Job counter key: ${ key } , increased, total jobs: ${ value } ` ) ;
22
22
23
- await redis . disconnect ( ) ;
24
23
return result ;
25
24
}
26
25
27
26
async decrement (
28
27
key ,
29
28
) {
30
- const redis = new Redis (
31
- this . options ,
32
- ) ;
29
+ const redis = this . newRedis ( ) ;
33
30
const luaScript = `
34
31
local count = redis.call('DECR', KEYS[1])
35
32
return count
36
33
` ;
37
34
38
35
const result = await redis . eval ( luaScript , 1 , key ) ;
36
+
39
37
if ( result === 0 ) {
40
38
console . log ( 'Last job completed.' ) ;
41
39
await redis . del ( key ) ;
42
- } else {
43
- console . log ( `Jobs remaining: ${ result } ` ) ;
40
+
41
+ return result ;
44
42
}
43
+
44
+ console . log ( `Jobs remaining: ${ result } ` ) ;
45
45
return result ;
46
46
}
47
+
48
+ newRedis ( ) {
49
+ if ( this . redis ) {
50
+ return this . redis ;
51
+ }
52
+
53
+ this . redis = new Redis (
54
+ this . options ,
55
+ ) ;
56
+
57
+ return this . redis ;
58
+ }
47
59
}
48
60
49
61
module . exports = CheckCompletion ;
You can’t perform that action at this time.
0 commit comments