<?php
declare(strict_types=1); use App\{Bootstrap,Queue,Utils,Logger};
require __DIR__.'/../src/Env.php';
require __DIR__.'/../src/Logger.php';
require __DIR__.'/../src/DB.php';
require __DIR__.'/../src/Queue.php';
require __DIR__.'/../src/Utils.php';
require __DIR__.'/../src/Bootstrap.php';


$boot = new Bootstrap(); $log=$boot->log;
$poll = (int) (App\Env::get('QUEUE_POLL_INTERVAL_MS','250'));
while (true) {
$job = Queue::reserveNext($poll);
if (!$job) { usleep($poll*1000); continue; }
$payload = json_decode($job['payload'], true) ?: [];
try {
switch ($job['type']) {
case 'send_document':
$params = ['chat_id'=>$payload['chat_id'], 'caption'=>$payload['caption'] ?? null];
if (is_file($payload['file_path'])) {
$params['document'] = new CURLFile($payload['file_path']);
} else {
throw new RuntimeException('file not found');
}
App\Utils::tgRequest('sendDocument', $params);
break;
default:
throw new RuntimeException('unknown job type');
}
Queue::done((int)$job['id']);
} catch (Throwable $e) {
$log->error('job failed', ['id'=>$job['id'], 'e'=>$e->getMessage()]);
Queue::fail((int)$job['id']);
}
}