🚀 مُصادَق: تحديث برمجي جديد 2026-05-03 15:51

This commit is contained in:
Hamza-Ayed
2026-05-03 15:51:53 +03:00
parent e182faad1d
commit 81a3e5188e
12 changed files with 415 additions and 6060 deletions

View File

@@ -20,47 +20,71 @@ pcntl_signal(SIGTERM, function() use (&$keepRunning) {
$keepRunning = false;
});
while ($keepRunning) {
$jobsProcessed = 0;
$maxJobs = 100; // Prevent memory leaks by restarting after N jobs
while ($keepRunning && $jobsProcessed < $maxJobs) {
$job = QueueService::pop();
if ($job) {
echo "[+] Processing job: {$job['type']} ({$job['id']})\n";
$jobsProcessed++;
echo "[+] Processing job: {$job['type']} ({$job['id']}) - Attempt: " . (($job['attempts'] ?? 0) + 1) . "\n";
$timeout = 120; // 2 minutes max per job
$completed = false;
try {
pcntl_alarm($timeout);
$container = $app->getContainer();
switch($job['type']) {
case 'ExtractInvoiceJob':
case 'invoice_extraction':
$handler = $container->get(\Queue\Jobs\ExtractInvoiceJob::class);
$handler->handle($job['payload']);
$container->get(\queue\Jobs\ExtractInvoiceJob::class)->handle($job['payload']);
break;
case 'SubmitJoFotaraJob':
case 'submit_jofotara':
$handler = $container->get(\Queue\Jobs\SubmitJoFotaraJob::class);
$handler->handle($job['payload']);
$container->get(\queue\Jobs\SubmitJoFotaraJob::class)->handle($job['payload']);
break;
case 'RiskAnalysisJob':
case 'risk_analysis':
$handler = $container->get(\Queue\Jobs\RiskAnalysisJob::class);
$handler->handle($job['payload']);
$container->get(\queue\Jobs\RiskAnalysisJob::class)->handle($job['payload']);
break;
case 'SendNotificationJob':
case 'send_notification':
$handler = $container->get(\Queue\Jobs\SendNotificationJob::class);
$handler->handle($job['payload']);
break;
default:
echo "[!] Unknown job type: {$job['type']}\n";
}
pcntl_alarm(0); // Cancel alarm
$completed = true;
echo "[✓] Job completed: {$job['id']}\n";
// If fallback DB is used, mark done
$db = \App\Core\Database::getInstance();
$db->prepare("UPDATE queue_jobs SET status = 'completed', completed_at = NOW() WHERE id = ?")->execute([$job['id']]);
} catch (\Throwable $e) {
pcntl_alarm(0); // Cancel alarm
echo "[✗] Job failed: {$job['id']} - {$e->getMessage()}\n";
// In a real app, you'd handle retries or move to a failed_jobs table
$attempts = ($job['attempts'] ?? 0) + 1;
if ($attempts < 3) {
// Exponential backoff: 2^attempts * 10 seconds (20s, 40s)
$delay = pow(2, $attempts) * 10;
$job['attempts'] = $attempts;
echo "[!] Retrying job in {$delay} seconds...\n";
// Note: Delay logic needs to be handled by a sorted set in Redis or a cron,
// but for simplicity we push back immediately for now, or you'd use a delayed queue.
QueueService::push($job['type'], $job['payload'], 0, $delay);
} else {
echo "[!] Job permanently failed (DLQ): {$job['id']}\n";
try {
$db = \App\Core\Database::getInstance();
// Just set status to failed if error_payload doesn't exist
$db->prepare("UPDATE queue_jobs SET status = 'failed' WHERE id = ?")->execute([$job['id']]);
} catch (\Throwable $err) {
echo "[!] Failed to update DB status: " . $err->getMessage() . "\n";
}
}
}
} else {
usleep(500000); // 0.5s