-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathQueuedJobsCollection.php
More file actions
112 lines (99 loc) · 2.85 KB
/
Copy pathQueuedJobsCollection.php
File metadata and controls
112 lines (99 loc) · 2.85 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
<?php
declare(strict_types=1);
namespace Queue\Model\Filter;
use Cake\Http\Exception\NotImplementedException;
use Cake\I18n\DateTime;
use Cake\ORM\Query\SelectQuery;
use Queue\Model\Table\QueuedJobsTable;
use Search\Model\Filter\FilterCollection;
class QueuedJobsCollection extends FilterCollection {
/**
* @return void
*/
public function initialize(): void {
$this
->value('job_task')
->like('search', [
'before' => true,
'after' => true,
'fields' => ['job_group', 'reference', 'status'],
])
->add('status', 'Search.Callback', [
'callback' => function (SelectQuery $query, array $args, $filter) {
$status = $args['status'];
if ($status === 'completed') {
$query->where(['completed IS NOT' => null]);
return true;
}
if ($status === 'in_progress') {
$query->where([
'completed IS' => null,
// Exclude terminally-failed (aborted) jobs: they are
// done, not in progress, even without a completed stamp.
'OR' => [
'status IS' => null,
'status !=' => QueuedJobsTable::STATUS_ABORTED,
],
'AND' => [
'OR' => [
'notbefore <=' => new DateTime(),
'notbefore IS' => null,
],
],
]);
return true;
}
if ($status === 'scheduled') {
$query->where(['completed IS' => null, 'notbefore >' => new DateTime()]);
return true;
}
if ($status === 'pending') {
// Waiting to be picked up: never fetched, no failure, due,
// and not aborted. Mirrors the dashboard "Pending" card
// (totalPending minus running and retriable-failed).
$query->where([
'completed IS' => null,
'fetched IS' => null,
'failure_message IS' => null,
'AND' => [
[
'OR' => [
'notbefore <=' => new DateTime(),
'notbefore IS' => null,
],
],
[
'OR' => [
'status IS' => null,
'status !=' => QueuedJobsTable::STATUS_ABORTED,
],
],
],
]);
return true;
}
if ($status === 'running') {
// Picked up by a worker and not yet completed or failed.
$query->where([
'completed IS' => null,
'fetched IS NOT' => null,
'failure_message IS' => null,
]);
return true;
}
if ($status === 'failed') {
// Unfinished jobs with a recorded failure: still-retrying
// ones and terminally aborted ones alike.
$query->where(['completed IS' => null, 'failure_message IS NOT' => null]);
return true;
}
if ($status === 'aborted') {
// Terminally failed: retries exhausted, will never run again.
$query->where(['completed IS' => null, 'status' => QueuedJobsTable::STATUS_ABORTED]);
return true;
}
throw new NotImplementedException('Invalid status type');
},
]);
}
}