forked from codeigniter4/queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseHandler.php
More file actions
223 lines (185 loc) · 6.09 KB
/
BaseHandler.php
File metadata and controls
223 lines (185 loc) · 6.09 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Queue.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Queue\Handlers;
use Closure;
use CodeIgniter\I18n\Time;
use CodeIgniter\Queue\Config\Queue as QueueConfig;
use CodeIgniter\Queue\Entities\QueueJob;
use CodeIgniter\Queue\Entities\QueueJobFailed;
use CodeIgniter\Queue\Exceptions\QueueException;
use CodeIgniter\Queue\Interfaces\QueueInterface;
use CodeIgniter\Queue\Models\QueueJobFailedModel;
use CodeIgniter\Queue\Payloads\ChainBuilder;
use CodeIgniter\Queue\Payloads\PayloadMetadata;
use CodeIgniter\Queue\QueuePushResult;
use CodeIgniter\Queue\Traits\HasQueueValidation;
use ReflectionException;
use Throwable;
/**
* @property QueueConfig $config
*/
abstract class BaseHandler implements QueueInterface
{
use HasQueueValidation;
protected QueueConfig $config;
protected ?string $priority = null;
protected ?int $delay = null;
abstract public function name(): string;
abstract public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): QueuePushResult;
abstract public function pop(string $queue, array $priorities): ?QueueJob;
abstract public function later(QueueJob $queueJob, int $seconds): bool;
abstract public function failed(QueueJob $queueJob, Throwable $err, bool $keepJob): bool;
abstract public function done(QueueJob $queueJob): bool;
abstract public function clear(?string $queue = null): bool;
/**
* Retry failed job.
*
* @throws ReflectionException
*/
public function retry(?int $id, ?string $queue): int
{
$jobs = model(QueueJobFailedModel::class)
->when(
$id !== null,
static fn ($query) => $query->where('id', $id),
)
->when(
$queue !== null,
static fn ($query) => $query->where('queue', $queue),
)
->findAll();
foreach ($jobs as $job) {
$this->setPriority($job->priority)->push($job->queue, $job->payload['job'], $job->payload['data']);
$this->forget($job->id);
}
return count($jobs);
}
/**
* Delete a failed job by ID.
*/
public function forget(int $id): bool
{
if (model(QueueJobFailedModel::class)->delete($id)) {
return model(QueueJobFailedModel::class)->affectedRows() > 0;
}
return false;
}
/**
* Delete many failed jobs at once.
*/
public function flush(?int $hours, ?string $queue): bool
{
if ($hours === null && $queue === null) {
return model(QueueJobFailedModel::class)->truncate();
}
return model(QueueJobFailedModel::class)
->when(
$hours !== null,
static fn ($query) => $query->where('failed_at <=', Time::now()->subHours($hours)->timestamp),
)
->when(
$queue !== null,
static fn ($query) => $query->where('queue', $queue),
)
->delete();
}
/**
* List failed queue jobs.
*/
public function listFailed(?string $queue): array
{
return model(QueueJobFailedModel::class)
->when(
$queue !== null,
static fn ($query) => $query->where('queue', $queue),
)
->orderBy('failed_at', 'desc')
->findAll();
}
/**
* Set delay for job queue (in seconds).
*/
public function setDelay(int $delay): static
{
$this->validateDelay($delay);
$this->delay = $delay;
return $this;
}
/**
* Set priority for job queue.
*/
public function setPriority(string $priority): static
{
$this->validatePriority($priority);
$this->priority = $priority;
return $this;
}
/**
* Create a job chain on the specified queue
*
* @param Closure $callback Chain definition callback
*/
public function chain(Closure $callback): QueuePushResult
{
$chainBuilder = new ChainBuilder($this);
$callback($chainBuilder);
return $chainBuilder->dispatch();
}
/**
* Log failed job.
*
* @throws ReflectionException
*/
protected function logFailed(QueueJob $queueJob, Throwable $err): bool
{
$exception = "Exception: {$err->getCode()} - {$err->getMessage()}" . PHP_EOL .
"file: {$err->getFile()}:{$err->getLine()}";
$queueJobFailed = new QueueJobFailed([
'connection' => $this->name(),
'queue' => $queueJob->queue,
'payload' => $queueJob->payload,
'priority' => $queueJob->priority,
'exception' => $exception,
]);
return model(QueueJobFailedModel::class)->insert($queueJobFailed, false);
}
/**
* Validate job and priority.
*/
protected function validateJobAndPriority(string $queue, string $job): void
{
// Validate queue
$this->validateQueue($queue);
// Validate jobHandler.
if (! in_array($job, array_keys($this->config->jobHandlers), true)) {
throw QueueException::forIncorrectJobHandler();
}
if ($this->priority === null) {
$this->setPriority($this->config->queueDefaultPriority[$queue] ?? 'default');
}
// Validate non-standard priority.
if (! in_array($this->priority, $this->config->queuePriorities[$queue] ?? ['default'], true)) {
throw QueueException::forIncorrectQueuePriority($this->priority, $queue);
}
}
/**
* Validate queue name.
*/
protected function validateQueue(string $queue): void
{
if (! preg_match('/^[a-z0-9_-]+$/', $queue)) {
throw QueueException::forIncorrectQueueFormat();
}
if (strlen($queue) > 64) {
throw QueueException::forTooLongQueueName();
}
}
}