8단계 관리자: 노래 추가·초성 검색·일괄 작업·휴지통·가져오기/내보내기·중복 병합·하이라이트 지정, 가수 별칭, 장르, 통계와 난이도 제안, 방 강제 종료, 수집 실행/멈추기, 새 방 기본 설정·잠금 + 출제 기록(song_plays)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -85,6 +85,9 @@ export class Catalog {
|
||||
}
|
||||
}
|
||||
|
||||
/** The job of this process, for a clean stop on SIGTERM (admin 수집 → 멈추기). */
|
||||
export let currentJob: Job | null = null;
|
||||
|
||||
export class Job {
|
||||
readonly id: number;
|
||||
constructor(
|
||||
@@ -94,7 +97,8 @@ export class Job {
|
||||
) {
|
||||
// Only one runner per kind: a row left 'running' by a killed process is stale.
|
||||
this.db.query("UPDATE collect_jobs SET status = 'stopped', finished_at = ? WHERE kind = ? AND status = 'running'").run(Date.now(), kind);
|
||||
this.id = Number(this.db.query("INSERT INTO collect_jobs (kind, status, total, started_at) VALUES (?, 'running', ?, ?)").run(kind, total, Date.now()).lastInsertRowid);
|
||||
this.id = Number(this.db.query("INSERT INTO collect_jobs (kind, status, total, started_at, pid) VALUES (?, 'running', ?, ?, ?)").run(kind, total, Date.now(), process.pid).lastInsertRowid);
|
||||
currentJob = this;
|
||||
}
|
||||
progress(done: number, total?: number): void {
|
||||
this.db.query('UPDATE collect_jobs SET done = ?, total = COALESCE(?, total) WHERE id = ?').run(done, total ?? null, this.id);
|
||||
|
||||
@@ -13,6 +13,7 @@ import { importSta } from './sta';
|
||||
import { matchYoutube } from './match-youtube';
|
||||
import { pickSearcher } from './ytmusic';
|
||||
import { catalogStats } from '../songs/store';
|
||||
import { currentJob } from './catalog';
|
||||
|
||||
const [cmd, ...rest] = process.argv.slice(2);
|
||||
const flag = (name: string) => {
|
||||
@@ -22,6 +23,13 @@ const flag = (name: string) => {
|
||||
const dbPath = process.env.DB_PATH ?? './data/app.db';
|
||||
const rawDir = process.env.RAW_DIR ?? join(dbPath, '..', 'raw');
|
||||
const db = openDb(dbPath);
|
||||
// 멈추기 from the admin page (or docker stop): record it and leave.
|
||||
process.on('SIGTERM', () => {
|
||||
currentJob?.log('stopped by admin');
|
||||
currentJob?.finish('stopped');
|
||||
db.close();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
switch (cmd) {
|
||||
case 'import-norabose':
|
||||
|
||||
52
apps/server/src/collector/control.ts
Normal file
52
apps/server/src/collector/control.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 관리자 → 수집: starts the collector CLI as its own process (same container, same DB in WAL mode) and stops it with SIGTERM.
|
||||
* One run per kind; the job row carries the process id, so runs started by cron can be stopped too.
|
||||
*/
|
||||
import type { Database } from 'bun:sqlite';
|
||||
import { join } from 'node:path';
|
||||
import type { CollectControl } from '../http/admin-catalog';
|
||||
import type { LogFn } from '../server';
|
||||
|
||||
export const COLLECT_KINDS: CollectControl['kinds'] = [{ id: 'match-youtube', label: '유튜브 원곡 찾기', limit: true }];
|
||||
|
||||
const CLI = join(import.meta.dir, 'cli.ts');
|
||||
|
||||
function alive(pid: number): boolean {
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function collectorControl(db: Database, log: LogFn, kinds = COLLECT_KINDS, args: (kind: string, limit?: number) => string[] = defaultArgs): CollectControl {
|
||||
const running = (kind: string) =>
|
||||
db.query<{ id: number; pid: number | null }, [string]>("SELECT id, pid FROM collect_jobs WHERE kind = ? AND status = 'running' ORDER BY id DESC LIMIT 1").get(kind);
|
||||
return {
|
||||
kinds,
|
||||
run(kind, limit) {
|
||||
const k = kinds.find((x) => x.id === kind);
|
||||
if (!k) return '없는 작업이에요.';
|
||||
const r = running(kind);
|
||||
if (r && r.pid && alive(r.pid)) return '이미 돌고 있는 작업이에요. 끝나거나 멈춘 뒤에 다시 실행해 주세요.';
|
||||
const proc = Bun.spawn([process.execPath, CLI, ...args(kind, k.limit ? limit : undefined)], { stdout: 'ignore', stderr: 'ignore', stdin: 'ignore', env: process.env });
|
||||
proc.unref();
|
||||
log('info', 'collector started', { kind, limit, pid: proc.pid });
|
||||
return null;
|
||||
},
|
||||
stop(jobId) {
|
||||
const j = db.query<{ status: string; pid: number | null }, [number]>('SELECT status, pid FROM collect_jobs WHERE id = ?').get(jobId);
|
||||
if (!j) return '작업을 찾을 수 없어요.';
|
||||
if (j.status !== 'running') return '돌고 있는 작업이 아니에요.';
|
||||
if (j.pid && alive(j.pid)) process.kill(j.pid, 'SIGTERM');
|
||||
else db.query("UPDATE collect_jobs SET status = 'stopped', finished_at = ? WHERE id = ?").run(Date.now(), jobId);
|
||||
return null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function defaultArgs(kind: string, limit?: number): string[] {
|
||||
if (kind === 'match-youtube') return ['match-youtube', '--limit', String(limit ?? 300), '--require-cookie'];
|
||||
return [kind, ...(limit ? ['--limit', String(limit)] : [])];
|
||||
}
|
||||
Reference in New Issue
Block a user