Completed
Push — master ( c5ed18...ed0fce )
by Nathan
07:36
created

BaseRunner::listJobs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
namespace Symbiote\QueuedJobs\Tasks\Engines;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Core\Convert;
7
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
8
9
/**
10
 * Class BaseRunner
11
 *
12
 * @author dmooyman
13
 */
14
class BaseRunner
15
{
16
    /**
17
     * Returns an instance of the QueuedJobService.
18
     *
19
     * @return QueuedJobService
20
     */
21
    public function getService()
22
    {
23
        return singleton('Symbiote\\QueuedJobs\\Services\\QueuedJobService');
24
    }
25
26
    /**
27
     * Write in a format expected by the output medium (CLI/HTML).
28
     *
29
     * @param string $line Line to be written out, without the newline character.
30
     * @param null|string $prefix
31
     */
32
    protected function writeLogLine($line, $prefix = null)
33
    {
34
        if (!$prefix) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $prefix of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
35
            $prefix = '[' . date('Y-m-d H:i:s') . '] ';
36
        }
37
38
        if (Director::is_cli()) {
39
            echo $prefix . $line . "\n";
40
        } else {
41
            echo Convert::raw2xml($prefix . $line) . "<br>";
42
        }
43
    }
44
45
    /**
46
     * Logs the status of the queued job descriptor.
47
     *
48
     * @param bool|null|QueuedJobDescriptor $descriptor
49
     * @param string $queue
50
     */
51
    protected function logDescriptorStatus($descriptor, $queue)
52
    {
53
        if (is_null($descriptor)) {
54
            $this->writeLogLine('No new jobs');
55
        }
56
57
        if ($descriptor === false) {
58
            $this->writeLogLine('Job is still running on ' . $queue);
59
        }
60
61
        if ($descriptor instanceof QueuedJobDescriptor) {
62
            $this->writeLogLine('Running ' . $descriptor->JobTitle . ' and others from ' . $queue . '.');
63
        }
64
    }
65
66
    /**
67
     * Logs the number of current jobs per queue
68
     */
69
    public function listJobs()
70
    {
71
        $service = $this->getService();
72
        for ($i = 1; $i <= 3; $i++) {
73
            $jobs = $service->getJobList($i);
74
            $num = $jobs ? $jobs->Count() : 0;
75
            $this->writeLogLine('Found ' . $num . ' jobs for mode ' . $i . '.');
76
        }
77
    }
78
}
79