File tree Expand file tree Collapse file tree 1 file changed +22
-10
lines changed
Expand file tree Collapse file tree 1 file changed +22
-10
lines changed Original file line number Diff line number Diff line change @@ -7,43 +7,55 @@ class CheckCompletion {
77 host : configRedis . auxRedisHost ,
88 port : configRedis . auxRedisPort ,
99 } ;
10+
11+ this . redis = null ;
1012 }
1113
1214 async setInitialJobCounter (
1315 key ,
1416 value ,
1517 ) {
16- const redis = new Redis (
17- this . options ,
18- ) ;
19-
18+ const redis = this . newRedis ( ) ;
2019 const result = await redis . set ( key , value ) ;
20+
2121 console . log ( `Job counter key: ${ key } , increased, total jobs: ${ value } ` ) ;
2222
23- await redis . disconnect ( ) ;
2423 return result ;
2524 }
2625
2726 async decrement (
2827 key ,
2928 ) {
30- const redis = new Redis (
31- this . options ,
32- ) ;
29+ const redis = this . newRedis ( ) ;
3330 const luaScript = `
3431 local count = redis.call('DECR', KEYS[1])
3532 return count
3633 ` ;
3734
3835 const result = await redis . eval ( luaScript , 1 , key ) ;
36+
3937 if ( result === 0 ) {
4038 console . log ( 'Last job completed.' ) ;
4139 await redis . del ( key ) ;
42- } else {
43- console . log ( `Jobs remaining: ${ result } ` ) ;
40+
41+ return result ;
4442 }
43+
44+ console . log ( `Jobs remaining: ${ result } ` ) ;
4545 return result ;
4646 }
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+ }
4759}
4860
4961module . exports = CheckCompletion ;
You can’t perform that action at this time.
0 commit comments