#!/usr/bin/env php
<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/bootstrap.php';

$interval = 2;
$maxIterations = null;

foreach ($argv as $arg) {
    if (str_starts_with($arg, '--interval=')) {
        $interval = (int)substr($arg, 11);
    }
    if (str_starts_with($arg, '--iterations=')) {
        $maxIterations = (int)substr($arg, 13);
    }
}

$worker = new \Crawl\Services\CrawlWorkerService();
$iteration = 0;

while (true) {
    $iteration++;
    $processed = $worker->tick();
    $time = date('Y-m-d H:i:s');
    echo "[{$time}] Processed {$processed} queue items (iteration {$iteration})\n";

    if ($maxIterations !== null && $iteration >= $maxIterations) {
        echo "Reached max iterations ({$maxIterations}). Exiting.\n";
        break;
    }

    sleep(max(1, $interval));
}
