summaryrefslogtreecommitdiff
path: root/app/lib/worker.server.ts
blob: f71ea4045eb00391c60a2b37a5a5529673f7ee46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { claimNextParseJob, completeParseJob, failParseJob, listBands } from "./db.server";
import { parseLiveInfoBands } from "./claude.server";

const POLL_INTERVAL_MS = 2000;

let started = false;

function startWorker() {
  if (started) return;
  started = true;

  let busy = false;

  setInterval(async () => {
    if (busy) return;
    const job = claimNextParseJob();
    if (!job) return;

    busy = true;
    try {
      const bands = listBands();
      const matches = await parseLiveInfoBands(job.input_text, bands);
      completeParseJob(job.id, JSON.stringify(matches));
    } catch (e) {
      failParseJob(job.id, e instanceof Error ? e.message : "不明なエラー");
    } finally {
      busy = false;
    }
  }, POLL_INTERVAL_MS);
}

startWorker();