Completed
Pull Request — master (#109)
by Robbie
01:51
created

DoormanRunner   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 92
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaultRules() 0 4 1
A getDefaultRules() 0 4 1
B runQueue() 0 33 6
A getNextJobDescriptorWithoutMutex() 0 22 2
1
<?php
2
3
namespace SilverStripe\QueuedJobs\Tasks\Engines;
4
5
use AsyncPHP\Doorman\Rule;
6
use SilverStripe\ORM\FieldType\DBDatetime;
7
use SilverStripe\QueuedJobs\DataObjects\QueuedJobDescriptor;
8
use SilverStripe\QueuedJobs\Jobs\DoormanProcessManager;
9
use SilverStripe\QueuedJobs\Jobs\DoormanQueuedJobTask;
10
use SilverStripe\QueuedJobs\Services\QueuedJob;
11
12
/**
13
 * Runs all jobs through the doorman engine
14
 */
15
class DoormanRunner extends BaseRunner implements TaskRunnerEngine
16
{
17
18
    /**
19
     * @var string
20
     */
21
    protected $defaultRules = array();
22
23
    /**
24
     * Assign default rules for this task
25
     *
26
     * @param array $rules
27
     */
28
    public function setDefaultRules($rules)
29
    {
30
        $this->defaultRules = $rules;
0 ignored issues
show
Documentation Bug introduced by
It seems like $rules of type array is incompatible with the declared type string of property $defaultRules.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
    }
32
33
    /**
34
     * @return array List of rules
35
     */
36
    public function getDefaultRules()
37
    {
38
        return $this->defaultRules;
39
    }
40
41
    /**
42
     * Run tasks on the given queue
43
     *
44
     * @param string $queue
45
     */
46
    public function runQueue($queue)
47
    {
48
49
        // split jobs out into multiple tasks...
50
51
        $manager = new DoormanProcessManager();
52
        // $manager->setLogPath(__DIR__);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
54
        // Assign default rules
55
        $defaultRules = $this->getDefaultRules();
56
        if ($defaultRules) {
57
            foreach ($defaultRules as $rule) {
0 ignored issues
show
Bug introduced by
The expression $defaultRules of type string is not traversable.
Loading history...
58
                $manager->addRule($rule);
59
            }
60
        }
61
62
        $descriptor = $this->getNextJobDescriptorWithoutMutex($queue);
63
64
        while ($manager->tick() || $descriptor) {
65
            $this->logDescriptorStatus($descriptor, $queue);
0 ignored issues
show
Bug introduced by
It seems like $descriptor can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\QueuedJobs\...::logDescriptorStatus() does only seem to accept boolean|null|object<Silv...ts\QueuedJobDescriptor>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
66
67
            if ($descriptor instanceof QueuedJobDescriptor) {
68
                $descriptor->JobStatus = QueuedJob::STATUS_INIT;
69
                $descriptor->write();
70
71
                $manager->addTask(new DoormanQueuedJobTask($descriptor));
72
            }
73
74
            sleep(1);
75
76
            $descriptor = $this->getNextJobDescriptorWithoutMutex($queue);
77
        }
78
    }
79
80
    /**
81
     * @param string $queue
82
     * @return null|QueuedJobDescriptor
83
     */
84
    protected function getNextJobDescriptorWithoutMutex($queue)
85
    {
86
        $list = QueuedJobDescriptor::get()
87
            ->filter('JobType', $queue)
88
            ->sort('ID', 'ASC');
89
90
        $descriptor = $list
91
            ->filter('JobStatus', QueuedJob::STATUS_WAIT)
92
            ->first();
93
94
        if ($descriptor) {
95
            return $descriptor;
96
        }
97
98
        return $list
99
            ->filter('JobStatus', QueuedJob::STATUS_NEW)
100
            ->where(sprintf(
101
                '"StartAfter" < \'%s\' OR "StartAfter" IS NULL',
102
                DBDatetime::now()->getValue()
103
            ))
104
            ->first();
105
    }
106
}
107