DoormanRunner::getNextJobDescriptorWithoutMutex()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 1
1
<?php
2
3
namespace Symbiote\QueuedJobs\Tasks\Engines;
4
5
use AsyncPHP\Doorman\Rule;
6
use SilverStripe\ORM\FieldType\DBDatetime;
7
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
8
use Symbiote\QueuedJobs\Jobs\DoormanQueuedJobTask;
9
use Symbiote\QueuedJobs\Services\QueuedJob;
10
11
/**
12
 * Runs all jobs through the doorman engine
13
 */
14
class DoormanRunner extends BaseRunner implements TaskRunnerEngine
15
{
16
17
    /**
18
     * @var string
19
     */
20
    protected $defaultRules = array();
21
22
    /**
23
     * Assign default rules for this task
24
     *
25
     * @param array $rules
26
     */
27
    public function setDefaultRules($rules)
28
    {
29
        $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...
30
    }
31
32
    /**
33
     * @return array List of rules
34
     */
35
    public function getDefaultRules()
36
    {
37
        return $this->defaultRules;
38
    }
39
40
    /**
41
     * Run tasks on the given queue
42
     *
43
     * @param string $queue
44
     */
45
    public function runQueue($queue)
46
    {
47
48
        // split jobs out into multiple tasks...
49
50
        $manager = new ProcessManager();
51
        $manager->setWorker(BASE_PATH . "/framework/cli-script.php dev/tasks/ProcessJobQueueChildTask");
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, Symbiote\QueuedJobs\Task...::logDescriptorStatus() does only seem to accept boolean|null|object<Symb...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