Transcribe only at night when TRANSCRIBE_HOURS is set

Queued jobs wait until the window opens (local time, so set TZ); a started job finishes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nmemmertandClaude Sonnet 5.5 committed 2026-10-27 08:55:49 -04:00
1 parent b0d60d0a5b
commit 41c48b10e8
1 file changed
+5
+5
View File
@@ -110,9 +110,14 @@ async function fetchAudio(src, cookie, tmp) {
let n = 0; const body = Readable.fromWeb(r.body); body.on('data', c => { n += c.length; if (n > MAX_AUDIO) body.destroy(new Error('That audio file is too large to transcribe')); });
await pipeline(body, fs.createWriteStream(tmp, { mode: 0o600 }));
}
// TRANSCRIBE_HOURS=22-6 keeps the (slow, CPU-hungry) speech-to-text to the night: a job waits in the queue until that window opens (local time, so set TZ). A job that has started finishes even if the window closes. Empty = any time.
const NIGHT = (process.env.TRANSCRIBE_HOURS || '').match(/^(\d{1,2})\s*-\s*(\d{1,2})$/);
const inWindow = () => { if (!NIGHT) return true; const a = +NIGHT[1], b = +NIGHT[2], h = new Date().getHours(); return a === b || (a < b ? h >= a && h < b : h >= a || h < b); };
async function untilWindow() { while (!inWindow()) await new Promise(r => setTimeout(r, 60e3)); }
function startJob(key, src, cookie, getAudio, ep) {
jobs.set(key, { state: 'running', since: Date.now() });
chain = chain.then(async () => {
await untilWindow();
const tmp = path.join(os.tmpdir(), 'nehub-tr-' + key);
try { if (getAudio) await saveAudio(await getAudio(), tmp); else await fetchAudio(src, cookie, tmp); const r = await whisper(tmp, 'audio'); if (!r) throw new Error('No speech was found in that audio'); atomicWrite(fileOf(key), JSON.stringify(ep ? { ...r, ep } : r)); jobs.delete(key); }
catch (e) { jobs.set(key, { state: 'error', since: Date.now(), error: e.message }); setTimeout(() => jobs.delete(key), 10 * 60e3).unref(); }