SPB Git forge

spb/rareindex

Public
54commits 1branches 0releases
7.1 MBsize
maindefault branch
10 days agolast push
TypeScript 61.9% HTML 37.2% SQL 0.7%
10.6 KB · 182 lines typescript
Raw Blame History
1import { loadDotenv } from './lib/env.ts';2loadDotenv();3process.env.RI_SERVICE ??= 'rareindex-worker';45import { env, logger } from '@rareindex/shared';6import { createQueue, JOBS } from './lib/queue.ts';7import { closeDb, db } from './lib/db.ts';8import { flushCosts } from './lib/costs.ts';9import { runCrawl } from './crawler/run.ts';10import { scheduleDueCrawls } from './crawler/scheduler.ts';11import { normalizeBatch } from './normalizer/index.ts';12import { sql } from 'drizzle-orm';13import { pendingCount, resolveBatch } from './entity-resolution/index.ts';14import { assetsNeedingValuation, computePremiums, valueMany } from './valuation/run.ts';15import { runCategorySnapshots, runIndices, runRadar } from './indices/run.ts';16import { syncFx } from './fx.ts';17import { syncBenchmarks } from './benchmarks.ts';18import { computeHealth } from './health.ts';19import { expireListings } from './listings-expire.ts';20import { processImages } from './image-processing/index.ts';21import { verifyCertificates } from './certs-verify.ts';22import { assessLots } from './auctions/assess.ts';2324/**25 * RareIndex worker process (PM2: rareindex-worker). One process runs the queue handlers and the26 * scheduler; scale out by starting more processes against the same database.27 */28export async function startWorker(): Promise<() => Promise<void>> {29  const log = logger.child({ component: 'worker' });30  const queue = createQueue();31  await queue.start();32  // Single-worker deployment: jobs left 'active' by a previous process (restart/crash) would wait for33  // their expiry (hours). Re-queue them so crawls resume immediately.34  await db().execute(sql`update pgboss.job set state = 'retry', started_on = null where state = 'active' and started_on < now() - interval '90 seconds'`);35  // Runs left 'running' by a dead process can never finish: close them so the per-connector guard below works.36  await db().execute(sql`update connector_runs set status = 'failed', finished_at = now(), error = 'worker restarted' where status = 'running'`);37  // Collapse duplicate queued crawls per connector (keep the oldest).38  await db().execute(sql`delete from pgboss.job j using pgboss.job k where j.name = 'crawl.run' and k.name = 'crawl.run' and j.state in ('created','retry') and k.state in ('created','retry') and j.data->>'connectorId' = k.data->>'connectorId' and j.created_on > k.created_on`);39  const concurrency = env().WORKER_CONCURRENCY;4041  // ---- handlers ----42  await queue.work<{ connectorId: string; mode?: 'incremental' | 'backfill' | 'probe'; limit?: number; trigger?: string }>(JOBS.crawlRun, { concurrency: Math.max(1, Math.floor(concurrency / 2)), pollingIntervalSeconds: 5 }, async (data) => {43    // One crawl per connector at a time (duplicates waste engine credits and crawl slots).44    const [running] = (await db().execute(sql`select 1 from connector_runs where connector_id = ${data.connectorId} and status = 'running' and started_at > now() - interval '6 hours' limit 1`)) as unknown as unknown[];45    if (running) {46      log.info({ connector: data.connectorId }, 'crawl already running; skipping duplicate job');47      return;48    }49    const res = await runCrawl(data.connectorId, { mode: data.mode ?? 'incremental', limit: data.limit, trigger: data.trigger ?? 'schedule' });50    if (res.recordsRaw > 0) await queue.send(JOBS.normalizeBatch, { connectorId: data.connectorId }, { singletonKey: `normalize:${data.connectorId}`, singletonSeconds: 30 });51  });52  await queue.work<{ connectorId?: string }>(JOBS.normalizeBatch, { concurrency: 2, pollingIntervalSeconds: 5 }, async (data) => {53    let total = 0;54    let more = false;55    for (let i = 0; i < 40; i++) {56      const r = await normalizeBatch({ connectorId: data.connectorId, limit: 500 });57      total += r.processed;58      more = r.processed >= 500;59      if (!more) break;60    }61    // Large crawls (100k+ raw rows) exceed one job's budget: chain another job instead of leaving rows unprocessed.62    if (more) await queue.send(JOBS.normalizeBatch, { connectorId: data.connectorId }, { singletonKey: `normalize:${data.connectorId ?? 'all'}:${Date.now()}`, startAfterSeconds: 1 });63    if (total > 0) for (let k = 0; k < Number(process.env.RESOLVE_CONCURRENCY ?? 3); k++) await queue.send(JOBS.resolveBatch, {}, { singletonKey: `resolve:${k}`, singletonSeconds: 30 });64  });65  const RESOLVE_WORKERS = Number(process.env.RESOLVE_CONCURRENCY ?? 3);66  // Release rows claimed by a previous worker process that died mid-batch.67  await db().execute(sql`update normalized_records set status = 'pending' where status = 'processing'`);68  await queue.work<Record<string, never>>(JOBS.resolveBatch, { concurrency: RESOLVE_WORKERS, pollingIntervalSeconds: 5 }, async () => {69    const touched = new Set<string>();70    for (let i = 0; i < 40; i++) {71      const r = await resolveBatch({ limit: 500 });72      for (const a of r.touchedAssets) touched.add(a);73      if (r.processed < 500) break;74    }75    if ((await pendingCount()) > 0) for (let k = 0; k < RESOLVE_WORKERS; k++) await queue.send(JOBS.resolveBatch, {}, { singletonKey: `resolve:${k}`, singletonSeconds: 30, startAfterSeconds: 5 });76    if (touched.size) await queue.send(JOBS.valuationAsset, { assetIds: [...touched].slice(0, 5000) }, { singletonKey: `value:${Date.now()}` });77    if (touched.size) await queue.send(JOBS.imagesProcess, { assetIds: [...touched].slice(0, 5000), limit: 5000 }, { singletonKey: `images:${Date.now()}` });78  });79  await queue.work<{ assetIds: string[] }>(JOBS.valuationAsset, { concurrency: 1, pollingIntervalSeconds: 5 }, async (data) => {80    await valueMany(data.assetIds, { concurrency: 4 });81  });82  await queue.work<{ all?: boolean; rebuildHistory?: boolean }>(JOBS.valuationRebuild, { concurrency: 1, pollingIntervalSeconds: 30 }, async (data) => {83    await computePremiums();84    const ids = await assetsNeedingValuation({ all: data.all, limit: 200_000 });85    await valueMany(ids, { rebuildHistory: data.rebuildHistory, concurrency: 4 });86  });87  await queue.work(JOBS.indicesDaily, { concurrency: 1, pollingIntervalSeconds: 30 }, async () => {88    await runIndices();89  });90  await queue.work(JOBS.snapshotsDaily, { concurrency: 1, pollingIntervalSeconds: 30 }, async () => {91    await runCategorySnapshots();92  });93  await queue.work(JOBS.radarScan, { concurrency: 1, pollingIntervalSeconds: 30 }, async () => {94    await runRadar();95  });96  await queue.work(JOBS.healthCompute, { concurrency: 1, pollingIntervalSeconds: 30 }, async () => {97    await computeHealth();98  });99  await queue.work<{ backfill?: boolean }>(JOBS.fxSync, { concurrency: 1, pollingIntervalSeconds: 30 }, async (data) => {100    await syncFx({ backfill: data.backfill });101  });102  await queue.work(JOBS.benchmarksSync, { concurrency: 1, pollingIntervalSeconds: 60 }, async () => {103    await syncBenchmarks();104  });105  await queue.work<{ assetIds?: string[]; limit?: number; recheck?: boolean }>(JOBS.imagesProcess, { concurrency: 1, pollingIntervalSeconds: 30 }, async (data) => {106    await processImages({ assetIds: data.assetIds, limit: data.limit ?? 2000, recheck: data.recheck ?? false });107  });108  await queue.work(JOBS.listingsExpire, { concurrency: 1, pollingIntervalSeconds: 60 }, async () => {109    await expireListings();110  });111  await queue.work<{ limit?: number; grader?: string }>(JOBS.certsVerify, { concurrency: 1, pollingIntervalSeconds: 60 }, async (data) => {112    await verifyCertificates({ limit: data.limit ?? 100, grader: data.grader });113  });114  await queue.work<{ limit?: number; all?: boolean }>(JOBS.auctionsAssess, { concurrency: 1, pollingIntervalSeconds: 60 }, async (data) => {115    await assessLots({ limit: data.limit ?? 20_000, all: data.all ?? false });116  });117  await queue.work(JOBS.accountJobs, { concurrency: 1, pollingIntervalSeconds: 60 }, async () => {118    try {119      const accountModule = './account/index.ts'; // provided by the account module when present120      const mod = (await import(accountModule)) as { runAccountJobs?: (d: ReturnType<typeof db>) => Promise<unknown> };121      if (mod.runAccountJobs) await mod.runAccountJobs(db());122    } catch (err) {123      if (!/Cannot find module|ERR_MODULE_NOT_FOUND/.test(String(err))) log.error({ err }, 'account jobs failed');124    }125  });126127  // ---- schedules (UTC) ----128  await queue.schedule(JOBS.fxSync, '15 16 * * 1-5', {}); // after ECB 16:00 CET publication129  await queue.schedule(JOBS.benchmarksSync, '30 22 * * *', {});130  await queue.schedule(JOBS.valuationRebuild, '0 3 * * *', { all: false });131  await queue.schedule(JOBS.snapshotsDaily, '20 4 * * *', {});132  await queue.schedule(JOBS.indicesDaily, '40 4 * * *', {});133  await queue.schedule(JOBS.radarScan, '0 5 * * *', {});134  await queue.schedule(JOBS.healthCompute, '*/30 * * * *', {});135  await queue.schedule(JOBS.listingsExpire, '10 * * * *', {});136  await queue.schedule(JOBS.certsVerify, '25 * * * *', { limit: 100 }); // SPEC §22: verify cert numbers seen on marketplaces against public grader lookups137  await queue.schedule(JOBS.imagesProcess, '50 5 * * *', { limit: 20000, recheck: true });138  await queue.schedule(JOBS.imagesProcess, '*/15 * * * *', { limit: 3000 });139  await queue.schedule(JOBS.accountJobs, '*/10 * * * *', {});140  await queue.schedule(JOBS.auctionsAssess, '*/20 * * * *', { limit: 20000 }); // §33–§35: all-in bid vs RIV on live lots141142  // ---- crawl scheduler loop ----143  const tick = async () => {144    try {145      await scheduleDueCrawls(queue);146      // Normalize whatever raw rows exist even while long crawls are still running (sales show up progressively).147      await queue.send(JOBS.normalizeBatch, {}, { singletonKey: 'normalize:periodic', singletonSeconds: 120 });148    } catch (err) {149      log.error({ err }, 'scheduler tick failed');150    }151  };152  await tick();153  if ((await pendingCount()) > 0) for (let k = 0; k < RESOLVE_WORKERS; k++) await queue.send(JOBS.resolveBatch, {}, { singletonKey: `resolve:${k}`, singletonSeconds: 30 });154  const timer = setInterval(tick, 60_000);155  // hourly incremental valuation for assets with fresh evidence156  const hourly = setInterval(() => void queue.send(JOBS.valuationRebuild, { all: false }, { singletonKey: 'valuation:hourly', singletonSeconds: 3000 }), 3600_000);157  log.info({ concurrency }, 'worker started');158159  return async () => {160    clearInterval(timer);161    clearInterval(hourly);162    await queue.stop();163    await flushCosts();164    await closeDb();165    log.info('worker stopped');166  };167}168169const isMain = process.argv[1] && /workers\/main\.ts$/.test(process.argv[1]);170if (isMain) {171  startWorker()172    .then((stop) => {173      const shutdown = () => void stop().then(() => process.exit(0));174      process.on('SIGINT', shutdown);175      process.on('SIGTERM', shutdown);176    })177    .catch((err) => {178      logger.error({ err }, 'worker failed to start');179      process.exit(1);180    });181}182