1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Tasks; |
4
|
|
|
|
5
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
6
|
|
|
use SilverStripe\Dev\BuildTask; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Task used to process the job queue |
10
|
|
|
* |
11
|
|
|
* @author Marcus Nyeholt <[email protected]> |
12
|
|
|
* @license BSD http://silverstripe.org/bsd-license/ |
13
|
|
|
*/ |
14
|
|
|
class ProcessJobQueueTask extends BuildTask |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritDoc} |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private static $segment = 'ProcessJobQueueTask'; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public function getDescription() |
26
|
|
|
{ |
27
|
|
|
return _t( |
28
|
|
|
'ProcessJobQueueTask.Description', |
29
|
|
|
'Used via a cron job to execute queued jobs that need to be run.' |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param HTTPRequest $request |
35
|
|
|
*/ |
36
|
|
|
public function run($request) |
37
|
|
|
{ |
38
|
|
|
$service = $this->getService(); |
39
|
|
|
|
40
|
|
|
if ($request->getVar('list')) { |
41
|
|
|
// List helper |
42
|
|
|
$service->queueRunner->listJobs(); |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Check if there is a job to run |
47
|
|
|
if (($job = $request->getVar('job')) && strpos($job, '-')) { |
48
|
|
|
// Run from a isngle job |
49
|
|
|
$parts = explode('-', $job); |
50
|
|
|
$id = $parts[1]; |
51
|
|
|
$service->runJob($id); |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Run the queue |
56
|
|
|
$queue = $this->getQueue($request); |
57
|
|
|
$service->runQueue($queue); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Resolves the queue name to one of a few aliases. |
62
|
|
|
* |
63
|
|
|
* @todo Solve the "Queued"/"queued" mystery! |
64
|
|
|
* |
65
|
|
|
* @param HTTPRequest $request |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
protected function getQueue($request) |
69
|
|
|
{ |
70
|
|
|
$queue = $request->getVar('queue'); |
71
|
|
|
|
72
|
|
|
if (!$queue) { |
73
|
|
|
$queue = 'Queued'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
switch (strtolower($queue)) { |
77
|
|
|
case 'immediate': |
78
|
|
|
$queue = QueuedJob::IMMEDIATE; |
79
|
|
|
break; |
80
|
|
|
case 'queued': |
81
|
|
|
$queue = QueuedJob::QUEUED; |
82
|
|
|
break; |
83
|
|
|
case 'large': |
84
|
|
|
$queue = QueuedJob::LARGE; |
85
|
|
|
break; |
86
|
|
|
default: |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $queue; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns an instance of the QueuedJobService. |
95
|
|
|
* |
96
|
|
|
* @return QueuedJobService |
97
|
|
|
*/ |
98
|
|
|
public function getService() |
99
|
|
|
{ |
100
|
|
|
return singleton('Symbiote\\QueuedJobs\\Services\\QueuedJobService'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|