|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Tasks\Engines; |
|
4
|
|
|
|
|
5
|
|
|
use AsyncPHP\Doorman\Manager\ProcessManager; |
|
6
|
|
|
use SilverStripe\Core\Environment; |
|
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
8
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
|
9
|
|
|
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
|
10
|
|
|
use Symbiote\QueuedJobs\Jobs\DoormanQueuedJobTask; |
|
11
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
|
12
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Runs all jobs through the doorman engine |
|
16
|
|
|
*/ |
|
17
|
|
|
class DoormanRunner extends BaseRunner implements TaskRunnerEngine |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string[] |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $defaultRules = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Assign default rules for this task |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $rules |
|
28
|
|
|
* @return $this |
|
29
|
|
|
*/ |
|
30
|
|
|
public function setDefaultRules($rules) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->defaultRules = $rules; |
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return array List of rules |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getDefaultRules() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->defaultRules; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Run tasks on the given queue |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $queue |
|
48
|
|
|
*/ |
|
49
|
|
|
public function runQueue($queue) |
|
50
|
|
|
{ |
|
51
|
|
|
// check if queue can be processed |
|
52
|
|
|
$service = QueuedJobService::singleton(); |
|
53
|
|
|
if ($service->isAtMaxJobs()) { |
|
54
|
|
|
$service->getLogger()->info('Not processing queue as jobs are at max initialisation limit.'); |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// split jobs out into multiple tasks... |
|
59
|
|
|
|
|
60
|
|
|
/** @var ProcessManager $manager */ |
|
61
|
|
|
$manager = Injector::inst()->create(ProcessManager::class); |
|
62
|
|
|
$manager->setWorker( |
|
63
|
|
|
BASE_PATH . "/vendor/silverstripe/framework/cli-script.php dev/tasks/ProcessJobQueueChildTask" |
|
64
|
|
|
); |
|
65
|
|
|
$logPath = Environment::getEnv('SS_DOORMAN_LOGPATH'); |
|
|
|
|
|
|
66
|
|
|
if ($logPath) { |
|
67
|
|
|
$manager->setLogPath($logPath); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Assign default rules |
|
71
|
|
|
$defaultRules = $this->getDefaultRules(); |
|
72
|
|
|
if ($defaultRules) { |
|
|
|
|
|
|
73
|
|
|
foreach ($defaultRules as $rule) { |
|
74
|
|
|
$manager->addRule($rule); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$descriptor = $this->getNextJobDescriptorWithoutMutex($queue); |
|
79
|
|
|
|
|
80
|
|
|
while ($manager->tick() || $descriptor) { |
|
81
|
|
|
if (QueuedJobService::singleton()->isMaintenanceLockActive()) { |
|
82
|
|
|
$service->getLogger()->info('Skipped queued job descriptor since maintenance log is active.'); |
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->logDescriptorStatus($descriptor, $queue); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
if ($descriptor instanceof QueuedJobDescriptor) { |
|
89
|
|
|
$descriptor->JobStatus = QueuedJob::STATUS_INIT; |
|
90
|
|
|
$descriptor->write(); |
|
91
|
|
|
|
|
92
|
|
|
$manager->addTask(new DoormanQueuedJobTask($descriptor)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
sleep(1); |
|
96
|
|
|
|
|
97
|
|
|
$descriptor = $this->getNextJobDescriptorWithoutMutex($queue); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $queue |
|
103
|
|
|
* @return null|QueuedJobDescriptor |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getNextJobDescriptorWithoutMutex($queue) |
|
106
|
|
|
{ |
|
107
|
|
|
$list = QueuedJobDescriptor::get() |
|
108
|
|
|
->filter('JobType', $queue) |
|
109
|
|
|
->sort('ID', 'ASC'); |
|
110
|
|
|
|
|
111
|
|
|
$descriptor = $list |
|
112
|
|
|
->filter('JobStatus', QueuedJob::STATUS_WAIT) |
|
113
|
|
|
->first(); |
|
114
|
|
|
|
|
115
|
|
|
if ($descriptor) { |
|
116
|
|
|
return $descriptor; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $list |
|
120
|
|
|
->filter('JobStatus', QueuedJob::STATUS_NEW) |
|
121
|
|
|
->where(sprintf( |
|
122
|
|
|
'"StartAfter" < \'%s\' OR "StartAfter" IS NULL', |
|
123
|
|
|
DBDatetime::now()->getValue() |
|
124
|
|
|
)) |
|
125
|
|
|
->first(); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.