chore: clean up dead code, use shared loadConfig, cache queue I/O

- Remove unused readFileSync import from job_applier.mjs
- Remove unused makeJobId (dead code, nothing imports it)
- setup.mjs: use shared loadConfig instead of inline cfg()
- queue.mjs: add in-memory cache for queue and log to avoid
  redundant disk reads during a single run

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-05 16:06:33 -08:00
parent d61ca4f54f
commit a244a5fddf
3 changed files with 25 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
/**
* queue.mjs — Job queue management
* Handles jobs_queue.json read/write/update
* Uses in-memory cache to avoid redundant disk I/O within a run.
*/
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { dirname, resolve } from 'path';
@@ -36,25 +37,39 @@ function ensureDir(path) {
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
}
// --- In-memory caches (populated on first read, invalidated on write) ---
let _queueCache = null;
let _logCache = null;
export function loadQueue() {
if (_queueCache) return _queueCache;
ensureDir(QUEUE_PATH);
return existsSync(QUEUE_PATH) ? JSON.parse(readFileSync(QUEUE_PATH, 'utf8')) : [];
_queueCache = existsSync(QUEUE_PATH) ? JSON.parse(readFileSync(QUEUE_PATH, 'utf8')) : [];
return _queueCache;
}
export function saveQueue(jobs) {
ensureDir(QUEUE_PATH);
writeFileSync(QUEUE_PATH, JSON.stringify(jobs, null, 2));
_queueCache = jobs;
}
export function loadLog() {
function loadLog() {
if (_logCache) return _logCache;
ensureDir(LOG_PATH);
return existsSync(LOG_PATH) ? JSON.parse(readFileSync(LOG_PATH, 'utf8')) : [];
_logCache = existsSync(LOG_PATH) ? JSON.parse(readFileSync(LOG_PATH, 'utf8')) : [];
return _logCache;
}
function saveLog(log) {
writeFileSync(LOG_PATH, JSON.stringify(log, null, 2));
_logCache = log;
}
export function appendLog(entry) {
const log = loadLog();
log.push({ ...entry, logged_at: new Date().toISOString() });
writeFileSync(LOG_PATH, JSON.stringify(log, null, 2));
saveLog(log);
}
export function getJobsByStatus(status) {
@@ -100,9 +115,3 @@ export function addJobs(newJobs) {
saveQueue(queue);
return added;
}
export function makeJobId(platform, url) {
const match = url.match(/\/(\d{8,})/);
const id = match ? match[1] : Math.random().toString(36).slice(2, 10);
return `${platform}_${id}`;
}