refactor: add recovery mode + shared JSON/error/fetch utilities
- GroupQueue recovery mode: limit to 3 concurrent agents for 60s after restart to prevent API rate-limit storms (configurable via env vars) - getErrorMessage: replace 14 occurrences of instanceof Error pattern - readJsonFile/writeJsonFile: replace 19 occurrences of JSON+fs pattern - fetchWithTimeout: replace 3 occurrences of AbortController pattern 354/354 tests passing
This commit is contained in:
19
src/ipc.ts
19
src/ipc.ts
@@ -80,8 +80,13 @@ export function startIpcWatcher(deps: IpcDeps): void {
|
||||
const filePath = path.join(messagesDir, file);
|
||||
try {
|
||||
const data = readJsonFile(filePath);
|
||||
if (!data || typeof data !== 'object') throw new Error('Invalid JSON');
|
||||
const msg = data as { type?: string; chatJid?: string; text?: string };
|
||||
if (!data || typeof data !== 'object')
|
||||
throw new Error('Invalid JSON');
|
||||
const msg = data as {
|
||||
type?: string;
|
||||
chatJid?: string;
|
||||
text?: string;
|
||||
};
|
||||
if (msg.type === 'message' && msg.chatJid && msg.text) {
|
||||
// Authorization: verify this group can send to this chatJid
|
||||
const targetGroup = registeredGroups[msg.chatJid];
|
||||
@@ -133,9 +138,15 @@ export function startIpcWatcher(deps: IpcDeps): void {
|
||||
const filePath = path.join(tasksDir, file);
|
||||
try {
|
||||
const data = readJsonFile(filePath);
|
||||
if (!data || typeof data !== 'object') throw new Error('Invalid JSON');
|
||||
if (!data || typeof data !== 'object')
|
||||
throw new Error('Invalid JSON');
|
||||
// Pass source group identity to processTaskIpc for authorization
|
||||
await processTaskIpc(data as Parameters<typeof processTaskIpc>[0], sourceGroup, isMain, deps);
|
||||
await processTaskIpc(
|
||||
data as Parameters<typeof processTaskIpc>[0],
|
||||
sourceGroup,
|
||||
isMain,
|
||||
deps,
|
||||
);
|
||||
fs.unlinkSync(filePath);
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
|
||||
Reference in New Issue
Block a user