fix: address code quality issues in session pool, approval, and main

- session.ts: add onReconnect callback so reconnected socket replaces stale pool entry
- session-pool.ts: pass onReconnect to update pool on reconnect; add closeAll() to gracefully end WebSocket connections on shutdown
- approval.ts: use approved flag so double-approval race (updateMany count=0) returns null instead of sending duplicate forward jobs
- main.ts: wrap pool.add() in try/catch to continue if one account fails; replace silent groupMap fallback with explicit error log and early return; call pool.closeAll() before BullMQ shutdown to prevent hanging sockets
- Tests: add closeAll() test to session-pool.test.ts; add double-approval race test to approval.test.ts (56 tests total, all passing)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 17:35:52 +05:30
parent 9e3ee0cd38
commit 41aabc4c0d
6 changed files with 82 additions and 5 deletions
+10 -1
View File
@@ -48,6 +48,7 @@ async function bootstrap() {
for (const account of accounts) {
groupMaps.set(account.id, new Map());
try {
await pool.add(
account.id,
account.sessionPath,
@@ -55,7 +56,11 @@ async function bootstrap() {
const tags = detectTags(msg.content, msg.senderJid, adminJids);
if (!isFlagged(tags)) return;
const groupMap = groupMaps.get(accountId) ?? new Map();
const groupMap = groupMaps.get(accountId);
if (!groupMap) {
logger.error({ accountId }, 'No group map for account — message dropped');
return;
}
const sourceGroupId = groupMap.get(msg.sourceGroupJid);
if (!sourceGroupId) {
logger.warn({ jid: msg.sourceGroupJid, accountId }, 'Unknown group — skipping message');
@@ -101,12 +106,16 @@ async function bootstrap() {
groupMaps.set(accountId, map);
},
);
} catch (err) {
logger.error({ accountId: account.id, err }, 'Failed to start session — skipping account');
}
}
logger.info({ accountCount: accounts.length }, 'Tower worker ready');
const shutdown = async () => {
logger.info('Shutting down...');
await pool.closeAll();
await ingestWorker.close();
await forwardWorker.close();
await ingestQueue.close();