Skip to content

Commit

Permalink
Fix: close cache redis connection on alarm job
Browse files Browse the repository at this point in the history
  • Loading branch information
pustovitDmytro committed Aug 9, 2022
1 parent 0407988 commit 4fd15b0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ SEND_ALARM_QUEUE_TTL='15s'
SEND_ALARM_QUEUE_ATTEMPTS=2
SEND_ALARM_QUEUE_BACKOFF_TYPE=exponential
SEND_ALARM_QUEUE_BACKOFF_DELAY='5s'
SEND_ALARM_QUEUE_CONCURRENCY=2
SEND_ALARM_QUEUE_CONCURRENCY=1
SEND_ALARM_QUEUE_LOG_LEVEL=info
SEND_ALARM_QUEUE_PROCESS=true
SEND_ALARM_QUEUE_KEEP_LAST=0
Expand Down
22 changes: 13 additions & 9 deletions src/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ export default class Cache {

async connect() {
if (this.connected) return;
await this.client.connect();

try {
await this.client.connect();
} catch (error) {
if (error.message !== 'Socket already opened') throw error;
}

this.connected = true;
}

async close() {
if (!this.connected) return;
try {
await this.client.quit();
} catch (error) {
if (![ 'The client is closed', 'This socket has been ended by the other party' ].includes(error.message)) throw error;
}

await this.client.quit();
this.connected = false;
}

Expand All @@ -48,11 +58,7 @@ export default class Cache {
chain = chain.SETEX(`${this.prefix}${key}`, this.ttl, cachedValue);
}

const res = await chain.exec();

await this.close();

return res;
return chain.exec();
}

async areAllSaved(keys) {
Expand All @@ -66,8 +72,6 @@ export default class Cache {

const res = await chain.exec();

await this.close();

return res.every(r => r === cachedValue);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/workers/sendEarnAlarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ export default async function (job) {
await cache.saveAll(hashes);
pn.progress(1, 'Saved in cache');

await cache.close();

return { status: 'NOTIFIED' };
}
1 change: 1 addition & 0 deletions src/workers/sendP2PAlarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default async function (job) {

await cache.saveAll(hashes);
pn.progress(1, 'Saved in cache');
await cache.close();

return { status: 'NOTIFIED' };
}
1 change: 1 addition & 0 deletions src/workers/sendSpotAlarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default async function (job) {

await cache.saveAll(hashes);
pn.progress(1, 'Saved in cache');
await cache.close();

return { status: 'NOTIFIED' };
}

0 comments on commit 4fd15b0

Please sign in to comment.