From b645d7575d179664cce7b1d28d8ceda3c64261d6 Mon Sep 17 00:00:00 2001 From: EJClaw Date: Wed, 4 Nov 2026 09:58:26 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B2=BD=EA=B8=B0=20=EA=B8=B0=EB=A1=9D=20?= =?UTF-8?q?=EA=B0=80=EC=A0=B8=EC=98=A4=EA=B8=B0=20=EC=B7=A8=EC=86=8C=20?= =?UTF-8?q?=C2=B7=20=EC=9E=AC=EA=B0=9C=20=EB=B3=B4=EC=99=84:=20=EB=B0=9B?= =?UTF-8?q?=EB=8B=A4=EA=B0=80=20=EB=A9=88=EC=B6=98=20=EC=8B=A4=ED=96=89(?= =?UTF-8?q?=EC=B7=A8=EC=86=8C=20=C2=B7=20=EA=B2=8C=EC=9E=84=20=EC=8B=9C?= =?UTF-8?q?=EC=9E=91=20=C2=B7=20=EC=98=A4=EB=A5=98)=EC=9D=80=20history=5Fp?= =?UTF-8?q?ending=20=EC=9C=BC=EB=A1=9C=20=EB=82=A8=EA=B2=A8=20=EB=8B=A4?= =?UTF-8?q?=EC=9D=8C=20=EC=8B=A4=ED=96=89=EC=9D=B4=20=EC=95=84=EB=8A=94=20?= =?UTF-8?q?=EA=B2=BD=EA=B8=B0=20=EC=AA=BD=EC=97=90=EC=84=9C=20=EB=A9=88?= =?UTF-8?q?=EC=B6=94=EC=A7=80=20=EC=95=8A=EA=B3=A0=20=EB=81=9D=EA=B9=8C?= =?UTF-8?q?=EC=A7=80=20=ED=9B=91=EA=B2=8C=20(=EB=81=9D=EA=B9=8C=EC=A7=80?= =?UTF-8?q?=20=ED=9B=91=EC=9D=80=20=EA=B3=84=EC=A0=95=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=83=88=20=EA=B2=BD=EA=B8=B0=20=EC=9D=BC=EB=B6=80=EB=A7=8C=20?= =?UTF-8?q?=EB=B0=9B=EA=B3=A0=20=EC=B7=A8=EC=86=8C=ED=95=98=EB=A9=B4=20?= =?UTF-8?q?=EB=82=98=EB=A8=B8=EC=A7=80=EB=A5=BC=20=EC=98=81=EC=98=81=20?= =?UTF-8?q?=EB=AA=BB=20=EB=B0=9B=EB=8D=98=20=EA=B2=83)=20=C2=B7=20?= =?UTF-8?q?=EC=B7=A8=EC=86=8C=ED=95=98=EB=A9=B4=20=EB=8B=A4=EC=9D=8C=20?= =?UTF-8?q?=EC=8B=A4=ED=96=89=EC=9D=80=20=EC=B7=A8=EC=86=8C=ED=95=9C=20?= =?UTF-8?q?=EB=95=8C=EB=B6=80=ED=84=B0=20=ED=95=9C=20=EC=A3=BC=EA=B8=B0=20?= =?UTF-8?q?=EB=92=A4=20(=EC=98=A4=EB=9E=98=20=EB=8F=88=20=EC=8B=A4?= =?UTF-8?q?=ED=96=89=EC=9D=84=20=EC=B7=A8=EC=86=8C=ED=95=98=EB=A9=B4=20?= =?UTF-8?q?=EB=B0=94=EB=A1=9C=20=EB=8B=A4=EC=8B=9C=20=EC=8B=9C=EC=9E=91?= =?UTF-8?q?=ED=95=98=EB=8D=98=20=EA=B2=83)=20=C2=B7=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- app/src/core/games/history.ts | 11 ++++- app/test/history.test.ts | 80 +++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/app/src/core/games/history.ts b/app/src/core/games/history.ts index 06e469d..8e1d4b4 100644 --- a/app/src/core/games/history.ts +++ b/app/src/core/games/history.ts @@ -72,6 +72,8 @@ export class HistorySync { if (!this.running || this.cancelled) return { cancelled: false, resumeAt: null }; this.cancelled = true; this.prog = null; + // 다음 실행은 취소한 때부터 한 주기 뒤 (오래 돈 실행을 취소해도 바로 다시 시작하지 않게) + this.lastAt = Date.now(); log.info(`${this.src.game} 경기 기록 가져오기: 사용자가 취소함`); return { cancelled: true, resumeAt: this.everyMs ? this.lastAt + this.everyMs : null }; } @@ -101,7 +103,10 @@ export class HistorySync { this.cancelled = false; try { const fullKey = `history_full:${this.src.game}:${acct}`; + // 받다가 멈춘 실행 (취소 · 게임 시작 · 오류): 받은 경기 뒤쪽에 못 받은 경기가 남아 있으니 이번에는 끝까지 훑는다 + const pendingKey = `history_pending:${this.src.game}:${acct}`; const full = !!this.store.meta(fullKey); + const pending = !!this.store.meta(pendingKey); const skip = this.skipped(); const todo: string[] = []; this.prog = { phase: 'scan', first: !full, scanned: 0, done: 0, total: 0, startedAt: Date.now(), etaMs: null }; @@ -113,13 +118,14 @@ export class HistorySync { todo.push(...fresh); this.prog = { ...this.prog, scanned: this.prog.scanned + p.ids.length, total: todo.length }; if (!p.ids.length || p.ids.length < this.opts.pageSize || (p.total !== null && start + this.opts.pageSize >= p.total)) break; - // 한 번 끝까지 훑은 뒤에는 아는 경기만 나오는 쪽에서 멈춘다 - if (full && !fresh.length) break; + // 한 번 끝까지 훑은 뒤에는 아는 경기만 나오는 쪽에서 멈춘다 (지난 실행이 받다가 멈췄으면 끝까지) + if (full && !pending && !fresh.length) break; } if (todo.length) log.info(`${this.src.game} 경기 기록: 새 경기 ${todo.length}개 가져오는 중`); let added = 0; this.fetchStartedAt = Date.now(); if (this.prog) this.prog = { ...this.prog, phase: 'fetch', done: 0, total: todo.length }; + if (todo.length && !pending) this.store.setMeta(pendingKey, '1'); for (const [i, id] of todo.entries()) { if (this.halted()) { log.info(`${this.src.game} 경기 기록: ${this.cancelled ? '취소해서' : '게임이 시작돼'} 멈춤 (${added}/${todo.length})`); @@ -138,6 +144,7 @@ export class HistorySync { if (this.cancelled) return added; // 끝(또는 max)까지 다 받았다: 다음부터는 새 경기만 본다 this.store.setMeta(fullKey, '1'); + if (this.store.meta(pendingKey)) this.store.setMeta(pendingKey, ''); if (added) log.info(`${this.src.game} 경기 기록: ${added}개 저장`); return added; } finally { diff --git a/app/test/history.test.ts b/app/test/history.test.ts index 68d14ac..b81d4c7 100644 --- a/app/test/history.test.ts +++ b/app/test/history.test.ts @@ -249,3 +249,83 @@ test('경기 기록: 진행 상황은 지금 게임 상태(games.state)로 나 store.close(); } }); + +test('경기 기록: 끝까지 훑은 계정에서 새 경기를 받다가 취소해도, 다음 실행이 남은 경기를 모두 받는다 · 취소 뒤 한 주기 동안은 다시 시작하지 않는다', async () => { + const dir = tmp(); + const { Store } = await import('../src/core/db/db.ts'); + const { HistorySync } = await import('../src/core/games/history.ts'); + const store = new Store(path.join(dir, 'db.sqlite')); + // 예전 경기 20개는 이미 받았고 끝까지 훑은 계정 + const old = Array.from({ length: 20 }, (_, i) => `val-old-${i}`); + for (const id of old) store.upsertMatch({ id, game: 'valorant', started_at: 1, status: 'complete' }); + store.setMeta('history_full:valorant:me', '1'); + // 새 경기 30개 (새것부터 = 목록 앞쪽) + const fresh = Array.from({ length: 30 }, (_, i) => `val-new-${i}`); + const all = [...fresh, ...old]; + const fetched: string[] = []; + let pages = 0; + let h!: InstanceType; + const src = { + game: 'valorant' as const, + idle: () => true, + account: () => 'me', + page: async (start: number, n: number) => { + pages++; + return { ids: all.slice(start, start + n), total: all.length }; + }, + fetch: async (id: string) => { + await sleep(5); + store.upsertMatch({ id, game: 'valorant', started_at: 2, status: 'complete' }); + fetched.push(id); + // 첫 쪽 10경기를 받은 뒤 취소 + if (fetched.length === 10) h.cancel(); + return 'ok' as const; + }, + }; + h = new HistorySync(store, src, { pageSize: 10, max: 500, gapMs: 1 }); + try { + assert.equal(await h.run(), 10); + assert.equal(fetched.length, 10); + assert.equal(store.meta('history_pending:valorant:me'), '1', '받다가 멈춤'); + + // 다음 실행: 첫 쪽은 다 아는 경기지만 멈추지 않고 끝까지 훑어 남은 20경기를 받는다 + assert.equal(await h.run(), 20, '남은 경기 모두'); + assert.deepEqual([...fetched].sort(), [...fresh].sort()); + assert.ok(!store.meta('history_pending:valorant:me'), '다 받았으면 표시를 지운다'); + + // 그다음부터는 다시 첫 쪽만 + pages = 0; + assert.equal(await h.run(), 0); + assert.equal(pages, 1, '아는 경기만 나오는 첫 쪽에서 멈춤'); + + // 취소한 때부터 한 주기 뒤에 이어서: 오래(주기보다 길게) 돈 실행을 취소해도 바로 다시 시작하지 않는다 + const every = 60000; + fetched.length = 0; + all.unshift(...Array.from({ length: 15 }, (_, i) => `val-more-${i}`)); + let gate!: () => void; + const slowFetch = src.fetch; + src.fetch = async (id: string) => { + await new Promise((r) => (gate = r)); + return slowFetch(id); + }; + // 위에서 취소한 때부터 한 주기가 지나지 않았으니 maybeRun 은 아직 돌지 않는다 + h.maybeRun(every, 0); + assert.equal((h as any).running, false, '취소 뒤 한 주기 안에는 다시 시작하지 않음'); + (h as any).lastAt = 0; + h.maybeRun(every, 0); + await until(() => h.progress()?.phase === 'fetch', 2000, 'fetch start'); + (h as any).lastAt -= 2 * every; // 두 주기 넘게 돈 것처럼 + const c = h.cancel(); + assert.equal(c.cancelled, true); + assert.ok(c.resumeAt! >= Date.now() + every - 1000, `취소한 때부터 한 주기 뒤 (${c.resumeAt! - Date.now()}ms)`); + gate(); + await until(() => !(h as any).running, 2000, 'stopped'); + pages = 0; + h.maybeRun(every, 0); + await sleep(50); + assert.equal(pages, 0, '다음 확인에서 바로 다시 시작하지 않음'); + assert.equal(h.progress(), null); + } finally { + store.close(); + } +});