<?php
declare(strict_types=1);
// worker_runner.php - kuyruğu dinler (CLI)
// Çalıştır: C:\xampp\php\php.exe C:\xampp\htdocs\6-a\worker_runner.php

require __DIR__ . '/db.php';
require __DIR__ . '/worker_a3.php';

echo "worker_runner started...\n";

while (true) {
  $job = $pdo->query("SELECT * FROM a3_jobs WHERE status='pending' ORDER BY id ASC LIMIT 1")
             ->fetch(PDO::FETCH_ASSOC);

  if (!$job) { sleep(2); continue; }

  try {
    run_job($job);
  } catch (Throwable $e) {
    $jid = (int)$job['id'];
    $pdo->prepare("UPDATE a3_jobs SET status='error', finished_at=NOW(), last_error=? WHERE id=?")
        ->execute([$e->getMessage(), $jid]);
    fwrite(STDERR, $e->getMessage()."\n");
  }
}
