Jake Vanderwerf
2026-01-20 7a9054bb3f033c98067b3196378311dae54c5fbf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace JVBase\managers\queue;
if (!defined('ABSPATH')) {
    exit;
}
 
final class Processor
{
    public function __construct(
        private Storage $storage,
        private Executor $defaultExecutor,
        private TypeRegistry $registry,
        private Locker $locker
    ) {}
 
    public function run(): void
    {
        $this->locker->withLock(function () {
            $ops = $this->storage->fetchRunnable(10);
 
            foreach ($ops as $op) {
                if (!$this->storage->markProcessing($op->id)) {
                    continue;
                }
 
                $this->processOne($op);
            }
 
            $this->storage->invalidateQueueCache();
        });
    }
 
    private function processOne(Operation $op): void
    {
        $progress = new Progress($op);
        $executor = $this->registry->getExecutor($op->type) ?? $this->defaultExecutor;
 
        try {
            $result = $executor->execute($op, $progress);
 
            $op->state = 'completed';
            $op->outcome = $result->outcome;
            $op->result = $result->result;
            $op->completedAt = current_time('mysql');
 
        } catch (\Throwable $e) {
            $this->handleFailure($op, $e);
        }
 
        $this->storage->save($op);
    }
 
    private function handleFailure(Operation $op, \Throwable $e): void
    {
        $hash = md5($e->getMessage());
 
        if ($op->lastErrorHash === $hash) {
            // Same error twice → permanent failure
            $op->state = 'completed';
            $op->outcome = 'failed_permanent';
            $op->completedAt = current_time('mysql');
        } else {
            // New error → schedule retry
            $op->retries++;
            $op->lastErrorHash = $hash;
            $op->state = 'scheduled';
            $op->scheduledAt = $this->calculateBackoff($op->retries);
        }
 
        $op->errorMessage = $e->getMessage();
 
        JVB()->error()->log(
            '[Queue]:processOne',
            $e->getMessage(),
            [
                'operation_id' => $op->id,
                'type'         => $op->type,
                'user_id'      => $op->userId,
                'retries'      => $op->retries,
            ],
            $op->outcome === 'failed_permanent' ? 'critical' : 'warning'
        );
    }
 
    private function calculateBackoff(int $attempt): string
    {
        $delay = min(30 * pow(2, $attempt - 1), 3600);
        $jitter = rand(0, (int)($delay * 0.1));
        return date('Y-m-d H:i:s', time() + $delay + $jitter);
    }
}