import { processNextBackgroundJob } from '../lib/background-jobs';

function runWorker(maxJobs = 50): void {
  let processed = 0;
  while (processed < maxJobs) {
    const job = processNextBackgroundJob();
    if (!job) break;
    processed += 1;
    const statusLine = `[background-job] ${job.id} type=${job.type} status=${job.status}`;
    if (job.status === 'failed') {
      console.error(`${statusLine} error=${job.error || 'unknown'}`);
    } else {
      console.log(statusLine);
    }
  }

  console.log(`[background-job] processed=${processed}`);
}

const arg = Number(process.argv[2] || '50');
runWorker(Number.isFinite(arg) && arg > 0 ? arg : 50);
