GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#178)
by Robbie
01:45
created

DoormanRunner   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
c 3
b 0
f 0
lcom 1
cbo 8
dl 0
loc 105
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaultRules() 0 4 1
A getDefaultRules() 0 4 1
C runQueue() 0 37 7
A getNextJobDescriptorWithoutMutex() 0 22 2
1
<?php
2
3
namespace Symbiote\QueuedJobs\Tasks\Engines;
4
5
use AsyncPHP\Doorman\Manager\ProcessManager;
6
use SilverStripe\Core\Config\Configurable;
7
use SilverStripe\ORM\FieldType\DBDatetime;
8
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
9
use Symbiote\QueuedJobs\Jobs\DoormanQueuedJobTask;
10
use Symbiote\QueuedJobs\Services\QueuedJob;
11
12
/**
13
 * Runs all jobs through the doorman engine
14
 */
15
class DoormanRunner extends BaseRunner implements TaskRunnerEngine
16
{
17
    use Configurable;
18
19
    /**
20
     * Optional: configure a log path for the runner
21
     *
22
     * @config
23
     * @var string|null
24
     */
25
    private static $log_path = null;
0 ignored issues
show
Unused Code introduced by
The property $log_path is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
26
27
    /**
28
     * @var string
29
     */
30
    protected $defaultRules = array();
31
32
    /**
33
     * Assign default rules for this task
34
     *
35
     * @param array $rules
36
     */
37
    public function setDefaultRules($rules)
38
    {
39
        $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...
40
    }
41
42
    /**
43
     * @return array List of rules
44
     */
45
    public function getDefaultRules()
46
    {
47
        return $this->defaultRules;
48
    }
49
50
    /**
51
     * Run tasks on the given queue
52
     *
53
     * @param string $queue
54
     */
55
    public function runQueue($queue)
56
    {
57
58
        // split jobs out into multiple tasks...
59
60
        $manager = new ProcessManager();
61
        $manager->setWorker(BASE_PATH . "/vendor/silverstripe/framework/cli-script.php dev/tasks/ProcessJobQueueChildTask");
62
        $logPath = $this->config()->get('log_path');
63
        if ($logPath) {
64
            $manager->setLogPath($logPath);
65
        }
66
67
        // Assign default rules
68
        $defaultRules = $this->getDefaultRules();
69
        if ($defaultRules) {
70
            foreach ($defaultRules as $rule) {
0 ignored issues
show
Bug introduced by
The expression $defaultRules of type string is not traversable.
Loading history...
71
                $manager->addRule($rule);
72
            }
73
        }
74
75
        $descriptor = $this->getNextJobDescriptorWithoutMutex($queue);
76
77
        while ($manager->tick() || $descriptor) {
78
            $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...
79
80
            if ($descriptor instanceof QueuedJobDescriptor) {
81
                $descriptor->JobStatus = QueuedJob::STATUS_INIT;
82
                $descriptor->write();
83
84
                $manager->addTask(new DoormanQueuedJobTask($descriptor));
85
            }
86
87
            sleep(1);
88
89
            $descriptor = $this->getNextJobDescriptorWithoutMutex($queue);
90
        }
91
    }
92
93
    /**
94
     * @param string $queue
95
     * @return null|QueuedJobDescriptor
96
     */
97
    protected function getNextJobDescriptorWithoutMutex($queue)
98
    {
99
        $list = QueuedJobDescriptor::get()
100
            ->filter('JobType', $queue)
101
            ->sort('ID', 'ASC');
102
103
        $descriptor = $list
104
            ->filter('JobStatus', QueuedJob::STATUS_WAIT)
105
            ->first();
106
107
        if ($descriptor) {
108
            return $descriptor;
109
        }
110
111
        return $list
112
            ->filter('JobStatus', QueuedJob::STATUS_NEW)
113
            ->where(sprintf(
114
                '"StartAfter" < \'%s\' OR "StartAfter" IS NULL',
115
                DBDatetime::now()->getValue()
116
            ))
117
            ->first();
118
    }
119
}
120