fix: add config validation and retry logic for failed jobs

- Add loadConfig() helper with clear errors for missing/malformed JSON
- Replace raw JSON.parse(readFileSync(...)) in both entry points
- Track retry_count on jobs; re-queue as 'new' up to max_retries (default 2)
- Add max_retries and DEFAULT_MAX_RETRIES constant

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-05 16:03:33 -08:00
parent 47513e8cec
commit e71f940687
4 changed files with 50 additions and 23 deletions

View File

@@ -53,3 +53,4 @@ export const NOTIFY_RATE_LIMIT_MS = 1500;
// --- Queue ---
export const DEFAULT_REVIEW_WINDOW_MINUTES = 30;
export const DEFAULT_MAX_RETRIES = 2;

View File

@@ -3,13 +3,34 @@
* Handles jobs_queue.json read/write/update
*/
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { dirname } from 'path';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
const __dir = dirname(fileURLToPath(import.meta.url));
const QUEUE_PATH = `${__dir}/../data/jobs_queue.json`;
const LOG_PATH = `${__dir}/../data/applications_log.json`;
/**
* Load and validate a JSON config file. Throws with a clear message on failure.
*/
export function loadConfig(filePath) {
const resolved = resolve(filePath);
if (!existsSync(resolved)) {
throw new Error(`Config file not found: ${resolved}\nCopy the matching .example.json and fill in your values.`);
}
let raw;
try {
raw = readFileSync(resolved, 'utf8');
} catch (e) {
throw new Error(`Cannot read config file ${resolved}: ${e.message}`);
}
try {
return JSON.parse(raw);
} catch (e) {
throw new Error(`Invalid JSON in ${resolved}: ${e.message}`);
}
}
function ensureDir(path) {
const dir = dirname(path);
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });