From 8a52fd3b68a69a8ece5cded879a1270a462fe1f1 Mon Sep 17 00:00:00 2001 From: EJClaw Date: Tue, 3 Nov 2026 22:43:05 +0900 Subject: [PATCH] =?UTF-8?q?=EC=97=B0=EB=A0=B9=EC=A0=9C=ED=95=9C=20?= =?UTF-8?q?=EC=9D=8C=EC=9B=90=20=EB=B0=9B=EA=B8=B0:=20429=20=EC=89=BC?= =?UTF-8?q?=EC=9D=84=20=EC=8B=9C=EC=9E=91=ED=95=A0=20=EB=95=8C=EB=A7=8C=20?= =?UTF-8?q?=EB=B3=B4=EB=8D=98=20=EA=B2=83=20=E2=80=94=20=EA=B3=A1=EB=A7=88?= =?UTF-8?q?=EB=8B=A4=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=80=20=EC=9A=94=EC=B2=AD=EA=B3=BC=20=EB=8B=A4=EC=9A=B4?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=20=EC=A7=81=EC=A0=84=EC=97=90=EB=8F=84=20`.cooldown`=EC=9D=84=20?= =?UTF-8?q?=EB=B3=B4=EA=B3=A0,=20=EA=B1=B8=EB=A6=AC=EB=A9=B4(RateLimited)?= =?UTF-8?q?=20=EC=8B=A4=ED=8C=A8=20=EA=B3=A1=EC=9C=BC=EB=A1=9C=20=EC=84=B8?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EA=B3=A0=20=EC=9E=91=EC=97=85=EC=9D=84=20?= =?UTF-8?q?=EB=B0=94=EB=A1=9C=20=EB=81=9D=EB=83=84.=20=EC=84=9C=EB=B2=84?= =?UTF-8?q?=EB=82=98=20=EB=8B=A4=EB=A5=B8=20=EC=88=98=EC=A7=91=20=EC=9E=91?= =?UTF-8?q?=EC=97=85=EC=9D=B4=20=EB=8F=84=EC=A4=91=EC=97=90=20429=EB=A5=BC?= =?UTF-8?q?=20=EB=B0=9B=EC=95=84=EB=8F=84=20=EC=9A=94=EC=B2=AD=EC=9D=B4=20?= =?UTF-8?q?=EA=B3=84=EC=86=8D=20=EB=82=98=EA=B0=80=EB=8D=98=20=EA=B2=83.?= =?UTF-8?q?=20=ED=9A=8C=EA=B7=80=20=ED=85=8C=EC=8A=A4=ED=8A=B8(=EB=8F=84?= =?UTF-8?q?=EC=A4=91=20=EC=89=BC=20=E2=86=92=20=EB=8B=A4=EC=9A=B4=EB=A1=9C?= =?UTF-8?q?=EB=93=9C=C2=B7=EB=8B=A4=EC=9D=8C=20=EA=B3=A1=20=EC=97=86?= =?UTF-8?q?=EC=9D=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- apps/server/src/collector/age-audio.test.ts | 21 ++++++++++++++++++++- apps/server/src/collector/age-audio.ts | 10 ++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/apps/server/src/collector/age-audio.test.ts b/apps/server/src/collector/age-audio.test.ts index e0b3ab7..3ba4f43 100644 --- a/apps/server/src/collector/age-audio.test.ts +++ b/apps/server/src/collector/age-audio.test.ts @@ -10,7 +10,7 @@ import { loudnessGain, quizSong } from '../songs/quiz'; import { tempDbPath } from '../test-utils'; import { cookieForms, collectAgeAudio } from './age-audio'; import { Catalog } from './catalog'; -import { cooldownUntil } from './cookie-jar'; +import { cooldownUntil, startCooldown } from './cookie-jar'; import { Blocked } from './charts'; import { collectLoudness, isAgeGated } from './loudness'; @@ -138,4 +138,23 @@ describe('연령제한 곡', () => { expect(downloads).toBe(1); expect(db.query<{ status: string; log: string }, []>("SELECT status, log FROM collect_jobs WHERE kind = 'age-audio' ORDER BY id DESC").get()).toMatchObject({ status: 'failed', log: expect.stringContaining('429') }); }); + + test('a pause started elsewhere while it runs stops it before the next signed-in request or download', async () => { + const { db, dir } = setup(3); + db.query('UPDATE songs SET age_video = youtube_id').run(); + const cookiePath = join(mkdtempSync(join(tmpdir(), 'mq-age-')), 'ytm_cookie.txt'); + writeFileSync(cookiePath, 'SAPISID=a; __Secure-1PSID=b'); + let pages = 0; + let downloads = 0; + // the first page is read, then the server gets a 429: neither its download nor the next songs go out + const r = await collectAgeAudio(db, dir, { + delayMs: 0, + cookiePath, + fetchSignedIn: async () => (pages++, startCooldown(cookiePath), ok(-9)), + download: async (_, out) => (downloads++, writeFileSync(out, 'x')), + }); + expect([pages, downloads, r.downloaded, r.failed]).toEqual([1, 0, 0, 0]); + expect(db.query<{ n: number }, []>("SELECT COUNT(*) n FROM signals WHERE kind = 'yt_audio_tried'").get()!.n).toBe(0); + expect(db.query<{ status: string; log: string }, []>("SELECT status, log FROM collect_jobs WHERE kind = 'age-audio' ORDER BY id DESC").get()).toMatchObject({ status: 'failed', log: expect.stringContaining('429') }); + }); }); diff --git a/apps/server/src/collector/age-audio.ts b/apps/server/src/collector/age-audio.ts index 57944b0..8c007d0 100644 --- a/apps/server/src/collector/age-audio.ts +++ b/apps/server/src/collector/age-audio.ts @@ -127,7 +127,11 @@ export async function collectAgeAudio(db: Database, dir: string, opts: AgeAudioO // 2. download with the adult-verified account (not during a 429 pause; a block here starts one — cookie-jar.ts) // (tests that inject both without a cookie path touch no cookie file) const path = opts.fetchSignedIn && opts.download ? opts.cookiePath : (opts.cookiePath ?? process.env.YT_COOKIE_PATH ?? '/home/claude/.config/music_quiz/ytm_cookie.txt'); - if (path && cooldownUntil(path) > Date.now()) throw new RateLimited(cooldownUntil(path)); + // checked before every signed-in request: the server or another collector may get a 429 while this runs + const waitOut = () => { + if (path && cooldownUntil(path) > Date.now()) throw new RateLimited(cooldownUntil(path)); + }; + waitOut(); const todo = pending(); job.progress(done, recheck.length + todo.length); let fetchSignedIn = opts.fetchSignedIn; @@ -142,6 +146,7 @@ export async function collectAgeAudio(db: Database, dir: string, opts: AgeAudioO } for (const s of todo) { try { + waitOut(); const page = await fetchSignedIn!(s.youtube_id); if (isAgeGated(page)) throw new CookieProblem('쿠키 계정으로도 성인 인증 페이지가 나와요(쿠키가 만료됐거나 성인 인증이 안 된 계정)'); // the anonymous page of an age-restricted video has no loudness / heatmap: take them from this one @@ -149,13 +154,14 @@ export async function collectAgeAudio(db: Database, dir: string, opts: AgeAudioO const sec = s.highlight_source === 'admin' || s.highlight_source === 'heatmap' ? null : pickHighlight(parseHeatmap(page)); if (sec !== null) db.query("UPDATE songs SET highlight_sec = ?, highlight_source = 'heatmap', updated_at = ? WHERE id = ? AND COALESCE(highlight_source, '') <> 'admin'").run(sec, Date.now(), s.id); + waitOut(); await download!(s.youtube_id, audioFile(dir, s.youtube_id)); db.query('UPDATE songs SET audio_video = ?, updated_at = ? WHERE id = ? AND youtube_id = ?').run(s.youtube_id, Date.now(), s.id, s.youtube_id); stats.downloaded++; job.log(`${s.youtube_id} downloaded`); } catch (e) { if (e instanceof Blocked && path) startCooldown(path); - if (e instanceof Blocked || e instanceof CookieProblem) throw e; + if (e instanceof Blocked || e instanceof CookieProblem || e instanceof RateLimited) throw e; stats.failed++; cat.signal(s.id, 'yt_audio_tried', Date.now()); job.log(`${s.youtube_id} download failed: ${(e as Error).message}`);