|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Michalsn\CodeIgniterQueue\Handlers; |
| 4 | + |
| 5 | +use CodeIgniter\Exceptions\CriticalError; |
| 6 | +use CodeIgniter\I18n\Time; |
| 7 | +use Exception; |
| 8 | +use Michalsn\CodeIgniterQueue\Config\Queue as QueueConfig; |
| 9 | +use Michalsn\CodeIgniterQueue\Entities\QueueJob; |
| 10 | +use Michalsn\CodeIgniterQueue\Enums\Status; |
| 11 | +use Michalsn\CodeIgniterQueue\Interfaces\QueueInterface; |
| 12 | +use Michalsn\CodeIgniterQueue\Payload; |
| 13 | +use Predis\Client; |
| 14 | +use Throwable; |
| 15 | + |
| 16 | +class PredisHandler extends BaseHandler implements QueueInterface |
| 17 | +{ |
| 18 | + private readonly Client $predis; |
| 19 | + |
| 20 | + public function __construct(protected QueueConfig $config) |
| 21 | + { |
| 22 | + try { |
| 23 | + $this->predis = new Client($config->predis, ['prefix' => $config->predis['prefix']]); |
| 24 | + $this->predis->time(); |
| 25 | + } catch (Exception $e) { |
| 26 | + throw new CriticalError('Queue: Predis connection refused (' . $e->getMessage() . ').'); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Add job to the queue. |
| 32 | + */ |
| 33 | + public function push(string $queue, string $job, array $data): bool |
| 34 | + { |
| 35 | + $this->validateJobAndPriority($queue, $job); |
| 36 | + |
| 37 | + helper('text'); |
| 38 | + |
| 39 | + $queueJob = new QueueJob([ |
| 40 | + 'id' => random_string('numeric', 16), |
| 41 | + 'queue' => $queue, |
| 42 | + 'payload' => new Payload($job, $data), |
| 43 | + 'priority' => $this->priority, |
| 44 | + 'status' => Status::PENDING->value, |
| 45 | + 'attempts' => 0, |
| 46 | + 'available_at' => Time::now()->timestamp, |
| 47 | + ]); |
| 48 | + |
| 49 | + $result = $this->predis->zadd("queues:{$queue}:{$this->priority}", [json_encode($queueJob) => Time::now()->timestamp]); |
| 50 | + |
| 51 | + $this->priority = null; |
| 52 | + |
| 53 | + return $result > 0; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get job from the queue. |
| 58 | + */ |
| 59 | + public function pop(string $queue, array $priorities): ?QueueJob |
| 60 | + { |
| 61 | + $tasks = []; |
| 62 | + $now = Time::now()->timestamp; |
| 63 | + |
| 64 | + foreach ($priorities as $priority) { |
| 65 | + if ($tasks = $this->predis->zrangebyscore("queues:{$queue}:{$priority}", '-inf', $now, ['LIMIT' => [0, 1]])) { |
| 66 | + if ($this->predis->zrem("queues:{$queue}:{$priority}", ...$tasks)) { |
| 67 | + break; |
| 68 | + } |
| 69 | + $tasks = []; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (empty($tasks[0])) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + $queueJob = new QueueJob(json_decode((string) $tasks[0], true)); |
| 78 | + |
| 79 | + // Set the actual status as in DB. |
| 80 | + $queueJob->status = Status::RESERVED->value; |
| 81 | + $queueJob->syncOriginal(); |
| 82 | + |
| 83 | + $this->predis->hset("queues:{$queue}::reserved", $queueJob->id, json_encode($queueJob)); |
| 84 | + |
| 85 | + return $queueJob; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Schedule job for later |
| 90 | + */ |
| 91 | + public function later(QueueJob $queueJob, int $seconds): bool |
| 92 | + { |
| 93 | + $queueJob->status = Status::PENDING->value; |
| 94 | + $queueJob->available_at = Time::now()->addSeconds($seconds)->timestamp; |
| 95 | + |
| 96 | + if ($result = $this->predis->zadd("queues:{$queueJob->queue}:{$queueJob->priority}", [json_encode($queueJob) => $queueJob->available_at->timestamp])) { |
| 97 | + $this->predis->hdel("queues:{$queueJob->queue}::reserved", $queueJob->id); |
| 98 | + } |
| 99 | + |
| 100 | + return $result > 0; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Move job to failed table or move and delete. |
| 105 | + */ |
| 106 | + public function failed(QueueJob $queueJob, Throwable $err, bool $keepJob): bool |
| 107 | + { |
| 108 | + if ($keepJob) { |
| 109 | + $this->logFailed($queueJob, $err); |
| 110 | + } |
| 111 | + |
| 112 | + return (bool) $this->predis->hdel("queues:{$queueJob->queue}::reserved", $queueJob->id); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Change job status to DONE or delete it. |
| 117 | + */ |
| 118 | + public function done(QueueJob $queueJob, bool $keepJob): bool |
| 119 | + { |
| 120 | + if ($keepJob) { |
| 121 | + $queueJob->status = Status::DONE->value; |
| 122 | + $this->predis->lpush("queues:{$queueJob->queue}::done", [json_encode($queueJob)]); |
| 123 | + } |
| 124 | + |
| 125 | + return (bool) $this->predis->hdel("queues:{$queueJob->queue}::reserved", $queueJob->id); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Delete queue jobs |
| 130 | + */ |
| 131 | + public function clear(?string $queue = null): bool |
| 132 | + { |
| 133 | + if ($queue !== null) { |
| 134 | + if ($keys = $this->predis->keys("queues:{$queue}:*")) { |
| 135 | + return $this->predis->del($keys) > 0; |
| 136 | + } |
| 137 | + |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + if ($keys = $this->predis->keys('queues:*')) { |
| 142 | + return $this->predis->del($keys) > 0; |
| 143 | + } |
| 144 | + |
| 145 | + return true; |
| 146 | + } |
| 147 | +} |
0 commit comments