1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Tasks\Engines; |
4
|
|
|
|
5
|
|
|
use AsyncPHP\Doorman\Manager\ProcessManager; |
6
|
|
|
use SilverStripe\Core\Environment; |
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
|
|
|
use SilverStripe\Core\Injector\Injector; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Runs all jobs through the doorman engine |
15
|
|
|
*/ |
16
|
|
|
class DoormanRunner extends BaseRunner implements TaskRunnerEngine |
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; |
|
|
|
|
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 = Injector::inst()->create(ProcessManager::class); |
52
|
|
|
$manager->setWorker(BASE_PATH . "/vendor/silverstripe/framework/cli-script.php dev/tasks/ProcessJobQueueChildTask"); |
53
|
|
|
$logPath = Environment::getEnv('SS_DOORMAN_LOGPATH'); |
|
|
|
|
54
|
|
|
if ($logPath) { |
55
|
|
|
$manager->setLogPath($logPath); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Assign default rules |
59
|
|
|
$defaultRules = $this->getDefaultRules(); |
60
|
|
|
if ($defaultRules) { |
61
|
|
|
foreach ($defaultRules as $rule) { |
|
|
|
|
62
|
|
|
$manager->addRule($rule); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$descriptor = $this->getNextJobDescriptorWithoutMutex($queue); |
67
|
|
|
|
68
|
|
|
while ($manager->tick() || $descriptor) { |
69
|
|
|
$this->logDescriptorStatus($descriptor, $queue); |
|
|
|
|
70
|
|
|
|
71
|
|
|
if ($descriptor instanceof QueuedJobDescriptor) { |
72
|
|
|
$descriptor->JobStatus = QueuedJob::STATUS_INIT; |
73
|
|
|
$descriptor->write(); |
74
|
|
|
|
75
|
|
|
$manager->addTask(new DoormanQueuedJobTask($descriptor)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
sleep(1); |
79
|
|
|
|
80
|
|
|
$descriptor = $this->getNextJobDescriptorWithoutMutex($queue); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $queue |
86
|
|
|
* @return null|QueuedJobDescriptor |
87
|
|
|
*/ |
88
|
|
|
protected function getNextJobDescriptorWithoutMutex($queue) |
89
|
|
|
{ |
90
|
|
|
$list = QueuedJobDescriptor::get() |
91
|
|
|
->filter('JobType', $queue) |
92
|
|
|
->sort('ID', 'ASC'); |
93
|
|
|
|
94
|
|
|
$descriptor = $list |
95
|
|
|
->filter('JobStatus', QueuedJob::STATUS_WAIT) |
96
|
|
|
->first(); |
97
|
|
|
|
98
|
|
|
if ($descriptor) { |
99
|
|
|
return $descriptor; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $list |
103
|
|
|
->filter('JobStatus', QueuedJob::STATUS_NEW) |
104
|
|
|
->where(sprintf( |
105
|
|
|
'"StartAfter" < \'%s\' OR "StartAfter" IS NULL', |
106
|
|
|
DBDatetime::now()->getValue() |
107
|
|
|
)) |
108
|
|
|
->first(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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..