1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Tasks\Engines; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Director; |
6
|
|
|
use SilverStripe\Core\Convert; |
7
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
8
|
|
|
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
9
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class BaseRunner |
13
|
|
|
* |
14
|
|
|
* @author dmooyman |
15
|
|
|
*/ |
16
|
|
|
class BaseRunner |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Returns an instance of the QueuedJobService. |
20
|
|
|
* |
21
|
|
|
* @return QueuedJobService |
22
|
|
|
*/ |
23
|
|
|
public function getService() |
24
|
|
|
{ |
25
|
|
|
return QueuedJobService::singleton(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Write in a format expected by the output medium (CLI/HTML). |
30
|
|
|
* |
31
|
|
|
* @param string $line Line to be written out, without the newline character. |
32
|
|
|
* @param null|string $prefix |
33
|
|
|
*/ |
34
|
|
|
protected function writeLogLine($line, $prefix = null) |
35
|
|
|
{ |
36
|
|
|
if (!$prefix) { |
|
|
|
|
37
|
|
|
$prefix = '[' . DBDatetime::now()->Rfc2822() . '] '; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (Director::is_cli()) { |
41
|
|
|
echo $prefix . $line . "\n"; |
42
|
|
|
} else { |
43
|
|
|
echo Convert::raw2xml($prefix . $line) . "<br>"; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Logs the status of the queued job descriptor. |
49
|
|
|
* |
50
|
|
|
* @param bool|null|QueuedJobDescriptor $descriptor |
51
|
|
|
* @param string $queue |
52
|
|
|
*/ |
53
|
|
|
protected function logDescriptorStatus($descriptor, $queue) |
54
|
|
|
{ |
55
|
|
|
if (is_null($descriptor)) { |
56
|
|
|
$this->writeLogLine('No new jobs on queue ' . $queue); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($descriptor === false) { |
60
|
|
|
$this->writeLogLine('Job is still running on queue ' . $queue); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($descriptor instanceof QueuedJobDescriptor) { |
64
|
|
|
$this->writeLogLine('Running ' . $descriptor->JobTitle . ' and others from queue ' . $queue . '.'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Logs the number of current jobs per queue |
70
|
|
|
*/ |
71
|
|
|
public function listJobs() |
72
|
|
|
{ |
73
|
|
|
$service = $this->getService(); |
74
|
|
|
for ($i = 1; $i <= 3; $i++) { |
75
|
|
|
$jobs = $service->getJobList($i); |
76
|
|
|
$num = $jobs ? $jobs->Count() : 0; |
77
|
|
|
$this->writeLogLine('Found ' . $num . ' jobs for mode ' . $i . '.'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: