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(); + } +});