Skip to content

Commit bc77e47

Browse files
authored
Merge pull request #12 from not-empty/bugfix/redis-connection
Fix to connect only one time
2 parents 279635f + a3ef5c3 commit bc77e47

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

core/check-completion.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff 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

4961
module.exports = CheckCompletion;

0 commit comments

Comments
 (0)