@@ -9,46 +9,46 @@ |
||
9 | 9 | class QueuedTasksFixture extends TestFixture |
10 | 10 | { |
11 | 11 | |
12 | - /** |
|
13 | - * Fields |
|
14 | - * |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - // @codingStandardsIgnoreStart |
|
18 | - public $fields = [ |
|
19 | - 'id' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => null, 'comment' => '', 'autoIncrement' => true, 'precision' => null], |
|
20 | - 'task' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null], |
|
21 | - 'data' => ['type' => 'text', 'length' => 16777215, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
22 | - 'not_before' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
23 | - 'fetched' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
24 | - 'completed' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
25 | - 'failed_count' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => '0', 'comment' => '', 'precision' => null, 'autoIncrement' => null], |
|
26 | - 'failure_message' => ['type' => 'text', 'length' => null, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
27 | - 'worker_key' => ['type' => 'string', 'fixed' => true, 'length' => 40, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
28 | - 'created' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
29 | - '_indexes' => [ |
|
30 | - 'completed' => ['type' => 'index', 'columns' => ['completed'], 'length' => []], |
|
31 | - 'worker_key' => ['type' => 'index', 'columns' => ['worker_key'], 'length' => []], |
|
32 | - 'task' => ['type' => 'index', 'columns' => ['task'], 'length' => []], |
|
33 | - ], |
|
34 | - '_constraints' => [ |
|
35 | - 'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], |
|
36 | - ], |
|
37 | - '_options' => [ |
|
38 | - 'engine' => 'InnoDB', |
|
39 | - 'collation' => 'utf8_general_ci' |
|
40 | - ], |
|
41 | - ]; |
|
42 | - // @codingStandardsIgnoreEnd |
|
12 | + /** |
|
13 | + * Fields |
|
14 | + * |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + // @codingStandardsIgnoreStart |
|
18 | + public $fields = [ |
|
19 | + 'id' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => null, 'comment' => '', 'autoIncrement' => true, 'precision' => null], |
|
20 | + 'task' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null], |
|
21 | + 'data' => ['type' => 'text', 'length' => 16777215, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
22 | + 'not_before' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
23 | + 'fetched' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
24 | + 'completed' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
25 | + 'failed_count' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => '0', 'comment' => '', 'precision' => null, 'autoIncrement' => null], |
|
26 | + 'failure_message' => ['type' => 'text', 'length' => null, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
27 | + 'worker_key' => ['type' => 'string', 'fixed' => true, 'length' => 40, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
28 | + 'created' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
29 | + '_indexes' => [ |
|
30 | + 'completed' => ['type' => 'index', 'columns' => ['completed'], 'length' => []], |
|
31 | + 'worker_key' => ['type' => 'index', 'columns' => ['worker_key'], 'length' => []], |
|
32 | + 'task' => ['type' => 'index', 'columns' => ['task'], 'length' => []], |
|
33 | + ], |
|
34 | + '_constraints' => [ |
|
35 | + 'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], |
|
36 | + ], |
|
37 | + '_options' => [ |
|
38 | + 'engine' => 'InnoDB', |
|
39 | + 'collation' => 'utf8_general_ci' |
|
40 | + ], |
|
41 | + ]; |
|
42 | + // @codingStandardsIgnoreEnd |
|
43 | 43 | |
44 | - /** |
|
45 | - * Init method |
|
46 | - * |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function init() |
|
50 | - { |
|
51 | - $this->records = []; |
|
52 | - parent::init(); |
|
53 | - } |
|
44 | + /** |
|
45 | + * Init method |
|
46 | + * |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function init() |
|
50 | + { |
|
51 | + $this->records = []; |
|
52 | + parent::init(); |
|
53 | + } |
|
54 | 54 | } |
@@ -31,578 +31,578 @@ |
||
31 | 31 | class QueueShell extends Shell |
32 | 32 | { |
33 | 33 | |
34 | - /** |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - public $modelClass = 'Queue.QueuedTasks'; |
|
39 | - |
|
40 | - /** |
|
41 | - * |
|
42 | - * @var array|null |
|
43 | - */ |
|
44 | - protected $_taskConf; |
|
45 | - |
|
46 | - /** |
|
47 | - * |
|
48 | - * @var int |
|
49 | - */ |
|
50 | - protected $_time = 0; |
|
51 | - |
|
52 | - /** |
|
53 | - * |
|
54 | - * @var bool |
|
55 | - */ |
|
56 | - protected $_exit = false; |
|
57 | - |
|
58 | - /** |
|
59 | - * Overwrite shell initialize to dynamically load all Queue Related Tasks. |
|
60 | - * |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - public function initialize() |
|
64 | - { |
|
65 | - $taskFinder = new TaskFinder(); |
|
66 | - $this->tasks = $taskFinder->allAppAndPluginTasks(); |
|
67 | - |
|
68 | - parent::initialize(); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public function startup() |
|
76 | - { |
|
77 | - if ($this->param('quiet')) { |
|
78 | - $this->interactive = false; |
|
79 | - } |
|
80 | - |
|
81 | - parent::startup(); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * |
|
86 | - * @return string |
|
87 | - */ |
|
88 | - public function getDescription() |
|
89 | - { |
|
90 | - $tasks = []; |
|
91 | - foreach ($this->taskNames as $loadedTask) { |
|
92 | - $tasks[] = "\t" . '* ' . $this->_taskName($loadedTask); |
|
93 | - } |
|
94 | - $tasks = implode(PHP_EOL, $tasks); |
|
95 | - |
|
96 | - $text = <<<TEXT |
|
34 | + /** |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + public $modelClass = 'Queue.QueuedTasks'; |
|
39 | + |
|
40 | + /** |
|
41 | + * |
|
42 | + * @var array|null |
|
43 | + */ |
|
44 | + protected $_taskConf; |
|
45 | + |
|
46 | + /** |
|
47 | + * |
|
48 | + * @var int |
|
49 | + */ |
|
50 | + protected $_time = 0; |
|
51 | + |
|
52 | + /** |
|
53 | + * |
|
54 | + * @var bool |
|
55 | + */ |
|
56 | + protected $_exit = false; |
|
57 | + |
|
58 | + /** |
|
59 | + * Overwrite shell initialize to dynamically load all Queue Related Tasks. |
|
60 | + * |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + public function initialize() |
|
64 | + { |
|
65 | + $taskFinder = new TaskFinder(); |
|
66 | + $this->tasks = $taskFinder->allAppAndPluginTasks(); |
|
67 | + |
|
68 | + parent::initialize(); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public function startup() |
|
76 | + { |
|
77 | + if ($this->param('quiet')) { |
|
78 | + $this->interactive = false; |
|
79 | + } |
|
80 | + |
|
81 | + parent::startup(); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * |
|
86 | + * @return string |
|
87 | + */ |
|
88 | + public function getDescription() |
|
89 | + { |
|
90 | + $tasks = []; |
|
91 | + foreach ($this->taskNames as $loadedTask) { |
|
92 | + $tasks[] = "\t" . '* ' . $this->_taskName($loadedTask); |
|
93 | + } |
|
94 | + $tasks = implode(PHP_EOL, $tasks); |
|
95 | + |
|
96 | + $text = <<<TEXT |
|
97 | 97 | Simple and minimalistic job queue (or deferred-task) system. |
98 | 98 | |
99 | 99 | Available Tasks: |
100 | 100 | $tasks |
101 | 101 | TEXT; |
102 | 102 | |
103 | - return $text; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Look for a Queue Task of hte passed name and try to call add() on it. |
|
108 | - * A QueueTask may provide an add function to enable the user to create new jobs via commandline. |
|
109 | - * |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - public function add() |
|
113 | - { |
|
114 | - if (count($this->args) < 1) { |
|
115 | - $this->out('Please call like this:'); |
|
116 | - $this->out(' bin/cake queue add <taskname>'); |
|
117 | - $this->_displayAvailableTasks(); |
|
118 | - |
|
119 | - return; |
|
120 | - } |
|
121 | - |
|
122 | - $name = Inflector::camelize($this->args[0]); |
|
123 | - if (in_array('Queue' . $name, $this->taskNames, true)) { |
|
124 | - /** @var \Queue\Shell\Task\QueueTask|\Queue\Shell\Task\AddInterface $task */ |
|
125 | - $task = $this->{'Queue' . $name}; |
|
126 | - if (!($task instanceof AddInterface)) { |
|
127 | - $this->abort('This task does not support adding via CLI call'); |
|
128 | - } |
|
129 | - $task->add(); |
|
130 | - } else { |
|
131 | - $this->out('Error: Task not found: ' . $name); |
|
132 | - $this->_displayAvailableTasks(); |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Output the task without Queue or Task |
|
138 | - * example: QueueImageTask becomes Image on display |
|
139 | - * |
|
140 | - * @param string $task Task name |
|
141 | - * @return string Cleaned task name |
|
142 | - */ |
|
143 | - protected function _taskName($task) |
|
144 | - { |
|
145 | - if (strpos($task, 'Queue') === 0) { |
|
146 | - return substr($task, 5); |
|
147 | - } |
|
148 | - |
|
149 | - return $task; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Run a QueueWorker loop. |
|
154 | - * Runs a Queue Worker process which will try to find unassigned jobs in the queue |
|
155 | - * which it may run and try to fetch and execute them. |
|
156 | - * |
|
157 | - * @return int|null |
|
158 | - */ |
|
159 | - public function runworker() |
|
160 | - { |
|
161 | - try { |
|
162 | - $pid = $this->_initPid(); |
|
163 | - } catch (PersistenceFailedException $exception) { |
|
164 | - $this->err($exception->getMessage()); |
|
165 | - $limit = (int)Configure::read('Queue.maxWorkers'); |
|
166 | - if ($limit) { |
|
167 | - $this->out('Cannot start worker: Too many workers already/still running on this server (' . $limit . '/' . $limit . ')'); |
|
168 | - } |
|
169 | - |
|
170 | - return static::CODE_ERROR; |
|
171 | - } |
|
172 | - |
|
173 | - // Enable Garbage Collector (PHP >= 5.3) |
|
174 | - if (function_exists('gc_enable')) { |
|
175 | - gc_enable(); |
|
176 | - } |
|
177 | - if (function_exists('pcntl_signal')) { |
|
178 | - pcntl_signal(SIGTERM, [ |
|
179 | - &$this, |
|
180 | - '_exit' |
|
181 | - ]); |
|
182 | - pcntl_signal(SIGINT, [ |
|
183 | - &$this, |
|
184 | - '_exit' |
|
185 | - ]); |
|
186 | - pcntl_signal(SIGTSTP, [ |
|
187 | - &$this, |
|
188 | - '_exit' |
|
189 | - ]); |
|
190 | - pcntl_signal(SIGQUIT, [ |
|
191 | - &$this, |
|
192 | - '_exit' |
|
193 | - ]); |
|
194 | - } |
|
195 | - $this->_exit = false; |
|
196 | - |
|
197 | - $startTime = time(); |
|
198 | - $types = $this->_stringToArray($this->param('type')); |
|
199 | - |
|
200 | - while (!$this->_exit) { |
|
201 | - $this->out(__d('queue', 'Looking for a job.'), 1, Shell::VERBOSE); |
|
202 | - |
|
203 | - $QueuedTask = $this->QueuedTasks->requestJob($this->_getTaskConf(), $types); |
|
204 | - |
|
205 | - if ($QueuedTask) { |
|
206 | - $this->runJob($QueuedTask, $pid); |
|
207 | - } elseif (Configure::read('Queue.exitWhenNothingToDo')) { |
|
208 | - $this->out('nothing to do, exiting.'); |
|
209 | - $this->_exit = true; |
|
210 | - } else { |
|
211 | - $this->out('nothing to do, sleeping.'); |
|
212 | - sleep(Config::sleepTime()); |
|
213 | - } |
|
214 | - |
|
215 | - // check if we are over the maximum runtime and end processing if so. |
|
216 | - if (Configure::readOrFail('Queue.workerMaxRuntime') && (time() - $startTime) >= Configure::readOrFail('Queue.workerMaxRuntime')) { |
|
217 | - $this->_exit = true; |
|
218 | - $this->out('queue', 'Reached runtime of ' . (time() - $startTime) . ' Seconds (Max ' . Configure::readOrFail('Queue.workerMaxRuntime') . '), terminating.'); |
|
219 | - } |
|
220 | - if ($this->_exit || mt_rand(0, 100) > (100 - (int)Config::gcprob())) { |
|
221 | - $this->out(__d('queue', 'Performing old job cleanup.')); |
|
222 | - $this->QueuedTasks->cleanOldJobs(); |
|
223 | - } |
|
224 | - $this->hr(); |
|
225 | - } |
|
226 | - |
|
227 | - $this->_deletePid($pid); |
|
228 | - |
|
229 | - if ($this->param('verbose')) { |
|
230 | - $this->_log('endworker', $pid); |
|
231 | - } |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * |
|
236 | - * @param \Queue\Model\Entity\QueuedTask $QueuedTask Queued task |
|
237 | - * @param string $pid PID of the process |
|
238 | - * @return void |
|
239 | - */ |
|
240 | - protected function runJob(QueuedTask $QueuedTask, $pid) |
|
241 | - { |
|
242 | - $this->out('Running Job of type "' . $QueuedTask->task . '"'); |
|
243 | - $this->_log('job ' . $QueuedTask->task . ', id ' . $QueuedTask->id, $pid, false); |
|
244 | - $taskName = 'Queue' . $QueuedTask->task; |
|
245 | - |
|
246 | - try { |
|
247 | - $this->_time = time(); |
|
248 | - |
|
249 | - $data = unserialize($QueuedTask->data); |
|
250 | - /** @var \Queue\Shell\Task\QueueTask $task */ |
|
251 | - $task = $this->{$taskName}; |
|
252 | - if (!$task instanceof QueueTaskInterface) { |
|
253 | - throw new RuntimeException('Task must implement ' . QueueTaskInterface::class); |
|
254 | - } |
|
255 | - |
|
256 | - $return = $task->run((array)$data, $QueuedTask->id); |
|
257 | - if ($return !== null) { |
|
258 | - trigger_error('run() should be void and throw exception in error case now.', E_USER_DEPRECATED); |
|
259 | - } |
|
260 | - $failureMessage = $taskName . ' failed'; |
|
261 | - } catch (Throwable $e) { |
|
262 | - $return = false; |
|
263 | - |
|
264 | - $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
265 | - if (!($e instanceof QueueException)) { |
|
266 | - $failureMessage .= "\n" . $e->getTraceAsString(); |
|
267 | - } |
|
268 | - |
|
269 | - $this->_logError($taskName . ' (job ' . $QueuedTask->id . ')' . "\n" . $failureMessage, $pid); |
|
270 | - } catch (Exception $e) { |
|
271 | - $return = false; |
|
272 | - |
|
273 | - $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
274 | - $this->_logError($taskName . "\n" . $failureMessage, $pid); |
|
275 | - } |
|
276 | - |
|
277 | - if ($return === false) { |
|
278 | - $this->QueuedTasks->markJobFailed($QueuedTask, $failureMessage); |
|
279 | - $failedStatus = $this->QueuedTasks->getFailedStatus($QueuedTask, $this->_getTaskConf()); |
|
280 | - $this->_log('job ' . $QueuedTask->task . ', id ' . $QueuedTask->id . ' failed and ' . $failedStatus, $pid); |
|
281 | - $this->out('Job did not finish, ' . $failedStatus . ' after try ' . $QueuedTask->failed . '.'); |
|
282 | - |
|
283 | - return; |
|
284 | - } |
|
285 | - |
|
286 | - $this->QueuedTasks->markJobDone($QueuedTask); |
|
287 | - $this->out('Job Finished.'); |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Manually trigger a Finished job cleanup. |
|
292 | - * |
|
293 | - * @return void |
|
294 | - */ |
|
295 | - public function clean() |
|
296 | - { |
|
297 | - if (!Configure::read('Queue.cleanupTimeout')) { |
|
298 | - $this->abort('You disabled cleanuptimout in config. Aborting.'); |
|
299 | - } |
|
300 | - |
|
301 | - $this->out('Deleting old jobs, that have finished before ' . date('Y-m-d H:i:s', time() - (int)Configure::read('Queue.cleanupTimeout'))); |
|
302 | - $this->QueuedTasks->cleanOldJobs(); |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Display current settings |
|
307 | - * |
|
308 | - * @return void |
|
309 | - */ |
|
310 | - public function settings() |
|
311 | - { |
|
312 | - $this->out('Current Settings:'); |
|
313 | - $conf = (array)Configure::read('Queue'); |
|
314 | - foreach ($conf as $key => $val) { |
|
315 | - if ($val === false) { |
|
316 | - $val = 'no'; |
|
317 | - } |
|
318 | - if ($val === true) { |
|
319 | - $val = 'yes'; |
|
320 | - } |
|
321 | - $this->out('* ' . $key . ': ' . print_r($val, true)); |
|
322 | - } |
|
323 | - |
|
324 | - $this->out(); |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Display some statistics about Finished Jobs. |
|
329 | - * |
|
330 | - * @return void |
|
331 | - */ |
|
332 | - public function stats() |
|
333 | - { |
|
334 | - $this->out('Jobs currently in the queue:'); |
|
335 | - |
|
336 | - $types = $this->QueuedTasks->getTypes()->toArray(); |
|
337 | - foreach ($types as $type) { |
|
338 | - $this->out(' ' . str_pad($type, 20, ' ', STR_PAD_RIGHT) . ': ' . $this->QueuedTasks->getLength($type)); |
|
339 | - } |
|
340 | - $this->hr(); |
|
341 | - $this->out('Total unfinished jobs: ' . $this->QueuedTasks->getLength()); |
|
342 | - $this->hr(); |
|
343 | - $this->out('Finished job statistics:'); |
|
344 | - $data = $this->QueuedTasks->getStats(); |
|
345 | - foreach ($data as $item) { |
|
346 | - $this->out(' ' . $item['task'] . ': '); |
|
347 | - $this->out(' Finished Jobs in Database: ' . $item['num']); |
|
348 | - $this->out(' Average Job existence : ' . str_pad(Number::precision($item['alltime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
349 | - $this->out(' Average Execution delay : ' . str_pad(Number::precision($item['fetchdelay']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
350 | - $this->out(' Average Execution time : ' . str_pad(Number::precision($item['runtime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
351 | - } |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Get option parser method to parse commandline options |
|
356 | - * |
|
357 | - * @return \Cake\Console\ConsoleOptionParser |
|
358 | - */ |
|
359 | - public function getOptionParser() |
|
360 | - { |
|
361 | - $subcommandParser = [ |
|
362 | - 'options' => [ |
|
363 | - /* |
|
103 | + return $text; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Look for a Queue Task of hte passed name and try to call add() on it. |
|
108 | + * A QueueTask may provide an add function to enable the user to create new jobs via commandline. |
|
109 | + * |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + public function add() |
|
113 | + { |
|
114 | + if (count($this->args) < 1) { |
|
115 | + $this->out('Please call like this:'); |
|
116 | + $this->out(' bin/cake queue add <taskname>'); |
|
117 | + $this->_displayAvailableTasks(); |
|
118 | + |
|
119 | + return; |
|
120 | + } |
|
121 | + |
|
122 | + $name = Inflector::camelize($this->args[0]); |
|
123 | + if (in_array('Queue' . $name, $this->taskNames, true)) { |
|
124 | + /** @var \Queue\Shell\Task\QueueTask|\Queue\Shell\Task\AddInterface $task */ |
|
125 | + $task = $this->{'Queue' . $name}; |
|
126 | + if (!($task instanceof AddInterface)) { |
|
127 | + $this->abort('This task does not support adding via CLI call'); |
|
128 | + } |
|
129 | + $task->add(); |
|
130 | + } else { |
|
131 | + $this->out('Error: Task not found: ' . $name); |
|
132 | + $this->_displayAvailableTasks(); |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Output the task without Queue or Task |
|
138 | + * example: QueueImageTask becomes Image on display |
|
139 | + * |
|
140 | + * @param string $task Task name |
|
141 | + * @return string Cleaned task name |
|
142 | + */ |
|
143 | + protected function _taskName($task) |
|
144 | + { |
|
145 | + if (strpos($task, 'Queue') === 0) { |
|
146 | + return substr($task, 5); |
|
147 | + } |
|
148 | + |
|
149 | + return $task; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Run a QueueWorker loop. |
|
154 | + * Runs a Queue Worker process which will try to find unassigned jobs in the queue |
|
155 | + * which it may run and try to fetch and execute them. |
|
156 | + * |
|
157 | + * @return int|null |
|
158 | + */ |
|
159 | + public function runworker() |
|
160 | + { |
|
161 | + try { |
|
162 | + $pid = $this->_initPid(); |
|
163 | + } catch (PersistenceFailedException $exception) { |
|
164 | + $this->err($exception->getMessage()); |
|
165 | + $limit = (int)Configure::read('Queue.maxWorkers'); |
|
166 | + if ($limit) { |
|
167 | + $this->out('Cannot start worker: Too many workers already/still running on this server (' . $limit . '/' . $limit . ')'); |
|
168 | + } |
|
169 | + |
|
170 | + return static::CODE_ERROR; |
|
171 | + } |
|
172 | + |
|
173 | + // Enable Garbage Collector (PHP >= 5.3) |
|
174 | + if (function_exists('gc_enable')) { |
|
175 | + gc_enable(); |
|
176 | + } |
|
177 | + if (function_exists('pcntl_signal')) { |
|
178 | + pcntl_signal(SIGTERM, [ |
|
179 | + &$this, |
|
180 | + '_exit' |
|
181 | + ]); |
|
182 | + pcntl_signal(SIGINT, [ |
|
183 | + &$this, |
|
184 | + '_exit' |
|
185 | + ]); |
|
186 | + pcntl_signal(SIGTSTP, [ |
|
187 | + &$this, |
|
188 | + '_exit' |
|
189 | + ]); |
|
190 | + pcntl_signal(SIGQUIT, [ |
|
191 | + &$this, |
|
192 | + '_exit' |
|
193 | + ]); |
|
194 | + } |
|
195 | + $this->_exit = false; |
|
196 | + |
|
197 | + $startTime = time(); |
|
198 | + $types = $this->_stringToArray($this->param('type')); |
|
199 | + |
|
200 | + while (!$this->_exit) { |
|
201 | + $this->out(__d('queue', 'Looking for a job.'), 1, Shell::VERBOSE); |
|
202 | + |
|
203 | + $QueuedTask = $this->QueuedTasks->requestJob($this->_getTaskConf(), $types); |
|
204 | + |
|
205 | + if ($QueuedTask) { |
|
206 | + $this->runJob($QueuedTask, $pid); |
|
207 | + } elseif (Configure::read('Queue.exitWhenNothingToDo')) { |
|
208 | + $this->out('nothing to do, exiting.'); |
|
209 | + $this->_exit = true; |
|
210 | + } else { |
|
211 | + $this->out('nothing to do, sleeping.'); |
|
212 | + sleep(Config::sleepTime()); |
|
213 | + } |
|
214 | + |
|
215 | + // check if we are over the maximum runtime and end processing if so. |
|
216 | + if (Configure::readOrFail('Queue.workerMaxRuntime') && (time() - $startTime) >= Configure::readOrFail('Queue.workerMaxRuntime')) { |
|
217 | + $this->_exit = true; |
|
218 | + $this->out('queue', 'Reached runtime of ' . (time() - $startTime) . ' Seconds (Max ' . Configure::readOrFail('Queue.workerMaxRuntime') . '), terminating.'); |
|
219 | + } |
|
220 | + if ($this->_exit || mt_rand(0, 100) > (100 - (int)Config::gcprob())) { |
|
221 | + $this->out(__d('queue', 'Performing old job cleanup.')); |
|
222 | + $this->QueuedTasks->cleanOldJobs(); |
|
223 | + } |
|
224 | + $this->hr(); |
|
225 | + } |
|
226 | + |
|
227 | + $this->_deletePid($pid); |
|
228 | + |
|
229 | + if ($this->param('verbose')) { |
|
230 | + $this->_log('endworker', $pid); |
|
231 | + } |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * |
|
236 | + * @param \Queue\Model\Entity\QueuedTask $QueuedTask Queued task |
|
237 | + * @param string $pid PID of the process |
|
238 | + * @return void |
|
239 | + */ |
|
240 | + protected function runJob(QueuedTask $QueuedTask, $pid) |
|
241 | + { |
|
242 | + $this->out('Running Job of type "' . $QueuedTask->task . '"'); |
|
243 | + $this->_log('job ' . $QueuedTask->task . ', id ' . $QueuedTask->id, $pid, false); |
|
244 | + $taskName = 'Queue' . $QueuedTask->task; |
|
245 | + |
|
246 | + try { |
|
247 | + $this->_time = time(); |
|
248 | + |
|
249 | + $data = unserialize($QueuedTask->data); |
|
250 | + /** @var \Queue\Shell\Task\QueueTask $task */ |
|
251 | + $task = $this->{$taskName}; |
|
252 | + if (!$task instanceof QueueTaskInterface) { |
|
253 | + throw new RuntimeException('Task must implement ' . QueueTaskInterface::class); |
|
254 | + } |
|
255 | + |
|
256 | + $return = $task->run((array)$data, $QueuedTask->id); |
|
257 | + if ($return !== null) { |
|
258 | + trigger_error('run() should be void and throw exception in error case now.', E_USER_DEPRECATED); |
|
259 | + } |
|
260 | + $failureMessage = $taskName . ' failed'; |
|
261 | + } catch (Throwable $e) { |
|
262 | + $return = false; |
|
263 | + |
|
264 | + $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
265 | + if (!($e instanceof QueueException)) { |
|
266 | + $failureMessage .= "\n" . $e->getTraceAsString(); |
|
267 | + } |
|
268 | + |
|
269 | + $this->_logError($taskName . ' (job ' . $QueuedTask->id . ')' . "\n" . $failureMessage, $pid); |
|
270 | + } catch (Exception $e) { |
|
271 | + $return = false; |
|
272 | + |
|
273 | + $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
274 | + $this->_logError($taskName . "\n" . $failureMessage, $pid); |
|
275 | + } |
|
276 | + |
|
277 | + if ($return === false) { |
|
278 | + $this->QueuedTasks->markJobFailed($QueuedTask, $failureMessage); |
|
279 | + $failedStatus = $this->QueuedTasks->getFailedStatus($QueuedTask, $this->_getTaskConf()); |
|
280 | + $this->_log('job ' . $QueuedTask->task . ', id ' . $QueuedTask->id . ' failed and ' . $failedStatus, $pid); |
|
281 | + $this->out('Job did not finish, ' . $failedStatus . ' after try ' . $QueuedTask->failed . '.'); |
|
282 | + |
|
283 | + return; |
|
284 | + } |
|
285 | + |
|
286 | + $this->QueuedTasks->markJobDone($QueuedTask); |
|
287 | + $this->out('Job Finished.'); |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Manually trigger a Finished job cleanup. |
|
292 | + * |
|
293 | + * @return void |
|
294 | + */ |
|
295 | + public function clean() |
|
296 | + { |
|
297 | + if (!Configure::read('Queue.cleanupTimeout')) { |
|
298 | + $this->abort('You disabled cleanuptimout in config. Aborting.'); |
|
299 | + } |
|
300 | + |
|
301 | + $this->out('Deleting old jobs, that have finished before ' . date('Y-m-d H:i:s', time() - (int)Configure::read('Queue.cleanupTimeout'))); |
|
302 | + $this->QueuedTasks->cleanOldJobs(); |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Display current settings |
|
307 | + * |
|
308 | + * @return void |
|
309 | + */ |
|
310 | + public function settings() |
|
311 | + { |
|
312 | + $this->out('Current Settings:'); |
|
313 | + $conf = (array)Configure::read('Queue'); |
|
314 | + foreach ($conf as $key => $val) { |
|
315 | + if ($val === false) { |
|
316 | + $val = 'no'; |
|
317 | + } |
|
318 | + if ($val === true) { |
|
319 | + $val = 'yes'; |
|
320 | + } |
|
321 | + $this->out('* ' . $key . ': ' . print_r($val, true)); |
|
322 | + } |
|
323 | + |
|
324 | + $this->out(); |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Display some statistics about Finished Jobs. |
|
329 | + * |
|
330 | + * @return void |
|
331 | + */ |
|
332 | + public function stats() |
|
333 | + { |
|
334 | + $this->out('Jobs currently in the queue:'); |
|
335 | + |
|
336 | + $types = $this->QueuedTasks->getTypes()->toArray(); |
|
337 | + foreach ($types as $type) { |
|
338 | + $this->out(' ' . str_pad($type, 20, ' ', STR_PAD_RIGHT) . ': ' . $this->QueuedTasks->getLength($type)); |
|
339 | + } |
|
340 | + $this->hr(); |
|
341 | + $this->out('Total unfinished jobs: ' . $this->QueuedTasks->getLength()); |
|
342 | + $this->hr(); |
|
343 | + $this->out('Finished job statistics:'); |
|
344 | + $data = $this->QueuedTasks->getStats(); |
|
345 | + foreach ($data as $item) { |
|
346 | + $this->out(' ' . $item['task'] . ': '); |
|
347 | + $this->out(' Finished Jobs in Database: ' . $item['num']); |
|
348 | + $this->out(' Average Job existence : ' . str_pad(Number::precision($item['alltime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
349 | + $this->out(' Average Execution delay : ' . str_pad(Number::precision($item['fetchdelay']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
350 | + $this->out(' Average Execution time : ' . str_pad(Number::precision($item['runtime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
351 | + } |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Get option parser method to parse commandline options |
|
356 | + * |
|
357 | + * @return \Cake\Console\ConsoleOptionParser |
|
358 | + */ |
|
359 | + public function getOptionParser() |
|
360 | + { |
|
361 | + $subcommandParser = [ |
|
362 | + 'options' => [ |
|
363 | + /* |
|
364 | 364 | * 'dry-run'=> array( |
365 | 365 | * 'short' => 'd', |
366 | 366 | * 'help' => 'Dry run the update, no jobs will actually be added.', |
367 | 367 | * 'boolean' => true |
368 | 368 | * ), |
369 | 369 | */ |
370 | - ] |
|
371 | - ]; |
|
372 | - $subcommandParserFull = $subcommandParser; |
|
373 | - $subcommandParserFull['options']['type'] = [ |
|
374 | - 'short' => 't', |
|
375 | - 'help' => 'Type (comma separated list possible)', |
|
376 | - 'default' => null |
|
377 | - ]; |
|
378 | - |
|
379 | - return parent::getOptionParser()->setDescription($this->getDescription()) |
|
380 | - ->addSubcommand('clean', [ |
|
381 | - 'help' => 'Remove old jobs (cleanup)', |
|
382 | - 'parser' => $subcommandParser |
|
383 | - ]) |
|
384 | - ->addSubcommand('add', [ |
|
385 | - 'help' => 'Add Job', |
|
386 | - 'parser' => $subcommandParser |
|
387 | - ]) |
|
388 | - ->addSubcommand('stats', [ |
|
389 | - 'help' => 'Stats', |
|
390 | - 'parser' => $subcommandParserFull |
|
391 | - ]) |
|
392 | - ->addSubcommand('settings', [ |
|
393 | - 'help' => 'Settings', |
|
394 | - 'parser' => $subcommandParserFull |
|
395 | - ]) |
|
396 | - ->addSubcommand('runworker', [ |
|
397 | - 'help' => 'Run Worker', |
|
398 | - 'parser' => $subcommandParserFull |
|
399 | - ]); |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Timestamped log. |
|
404 | - * |
|
405 | - * @param string $message Log type |
|
406 | - * @param string|null $pid PID of the process |
|
407 | - * @param bool $addDetails Details |
|
408 | - * @return void |
|
409 | - */ |
|
410 | - protected function _log($message, $pid = null, $addDetails = true) |
|
411 | - { |
|
412 | - if (!Configure::read('Queue.log')) { |
|
413 | - return; |
|
414 | - } |
|
415 | - |
|
416 | - if ($addDetails) { |
|
417 | - $timeNeeded = $this->_timeNeeded(); |
|
418 | - $memoryUsage = $this->_memoryUsage(); |
|
419 | - $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
420 | - } |
|
421 | - |
|
422 | - if ($pid) { |
|
423 | - $message .= ' (pid ' . $pid . ')'; |
|
424 | - } |
|
425 | - Log::write('info', $message, [ |
|
426 | - 'scope' => 'queue' |
|
427 | - ]); |
|
428 | - } |
|
429 | - |
|
430 | - /** |
|
431 | - * |
|
432 | - * @param string $message Message |
|
433 | - * @param string|null $pid PID of the process |
|
434 | - * @return void |
|
435 | - */ |
|
436 | - protected function _logError($message, $pid = null) |
|
437 | - { |
|
438 | - $timeNeeded = $this->_timeNeeded(); |
|
439 | - $memoryUsage = $this->_memoryUsage(); |
|
440 | - $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
441 | - |
|
442 | - if ($pid) { |
|
443 | - $message .= ' (pid ' . $pid . ')'; |
|
444 | - } |
|
445 | - |
|
446 | - Log::write('error', $message); |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Returns a List of available QueueTasks and their individual configurations. |
|
451 | - * |
|
452 | - * @return array |
|
453 | - */ |
|
454 | - protected function _getTaskConf() |
|
455 | - { |
|
456 | - if (!is_array($this->_taskConf)) { |
|
457 | - $this->_taskConf = []; |
|
458 | - foreach ($this->tasks as $task) { |
|
459 | - list ($pluginName, $taskName) = pluginSplit($task); |
|
460 | - |
|
461 | - $this->_taskConf[$taskName]['name'] = substr($taskName, 5); |
|
462 | - $this->_taskConf[$taskName]['plugin'] = $pluginName; |
|
463 | - if (property_exists($this->{$taskName}, 'timeout')) { |
|
464 | - $this->_taskConf[$taskName]['timeout'] = $this->{$taskName}->timeout; |
|
465 | - } else { |
|
466 | - $this->_taskConf[$taskName]['timeout'] = Config::defaultWorkerTimeout(); |
|
467 | - } |
|
468 | - if (property_exists($this->{$taskName}, 'retries')) { |
|
469 | - $this->_taskConf[$taskName]['retries'] = $this->{$taskName}->retries; |
|
470 | - } else { |
|
471 | - $this->_taskConf[$taskName]['retries'] = Config::defaultWorkerRetries(); |
|
472 | - } |
|
473 | - if (property_exists($this->{$taskName}, 'cleanupTimeout')) { |
|
474 | - $this->_taskConf[$taskName]['cleanupTimeout'] = $this->{$taskName}->cleanupTimeout; |
|
475 | - } else { |
|
476 | - $this->_taskConf[$taskName]['cleanupTimeout'] = Config::cleanupTimeout(); |
|
477 | - } |
|
478 | - } |
|
479 | - } |
|
480 | - |
|
481 | - return $this->_taskConf; |
|
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * Signal handling to queue worker for clean shutdown |
|
486 | - * |
|
487 | - * @param int $signal The signal |
|
488 | - * @return void |
|
489 | - */ |
|
490 | - protected function _exit($signal) |
|
491 | - { |
|
492 | - $this->out(__d('queue', 'Caught %d signal, exiting.', $signal)); |
|
493 | - $this->_exit = true; |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * |
|
498 | - * @return void |
|
499 | - */ |
|
500 | - protected function _displayAvailableTasks() |
|
501 | - { |
|
502 | - $this->out('Available Tasks:'); |
|
503 | - foreach ($this->taskNames as $loadedTask) { |
|
504 | - $this->out("\t" . '* ' . $this->_taskName($loadedTask)); |
|
505 | - } |
|
506 | - } |
|
507 | - |
|
508 | - /** |
|
509 | - * |
|
510 | - * @return string |
|
511 | - */ |
|
512 | - protected function _initPid() |
|
513 | - { |
|
514 | - $this->_pid = $this->_retrievePid(); |
|
515 | - |
|
516 | - return $this->_pid; |
|
517 | - } |
|
518 | - |
|
519 | - /** |
|
520 | - * |
|
521 | - * @return string |
|
522 | - */ |
|
523 | - protected function _retrievePid() |
|
524 | - { |
|
525 | - if (function_exists('posix_getpid')) { |
|
526 | - $pid = (string)posix_getpid(); |
|
527 | - } else { |
|
528 | - $pid = $this->QueuedTasks->key(); |
|
529 | - } |
|
530 | - |
|
531 | - return $pid; |
|
532 | - } |
|
533 | - |
|
534 | - /** |
|
535 | - * |
|
536 | - * @return string Memory usage in MB. |
|
537 | - */ |
|
538 | - protected function _memoryUsage() |
|
539 | - { |
|
540 | - $limit = ini_get('memory_limit'); |
|
541 | - |
|
542 | - $used = number_format(memory_get_peak_usage(true) / (1024 * 1024), 0) . 'MB'; |
|
543 | - if ($limit !== '-1') { |
|
544 | - $used .= '/' . $limit; |
|
545 | - } |
|
546 | - |
|
547 | - return $used; |
|
548 | - } |
|
549 | - |
|
550 | - /** |
|
551 | - * |
|
552 | - * @param string|null $pid PID of the process |
|
553 | - * |
|
554 | - * @return void |
|
555 | - */ |
|
556 | - protected function _deletePid($pid) |
|
557 | - { |
|
558 | - if (!$pid) { |
|
559 | - $pid = $this->_pid; |
|
560 | - } |
|
561 | - if (!$pid) { |
|
562 | - return; |
|
563 | - } |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * |
|
568 | - * @return string |
|
569 | - */ |
|
570 | - protected function _timeNeeded() |
|
571 | - { |
|
572 | - $diff = $this->_time() - $this->_time($this->_time); |
|
573 | - $seconds = max($diff, 1); |
|
574 | - |
|
575 | - return $seconds . 's'; |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * |
|
580 | - * @param int|null $providedTime Provided time |
|
581 | - * |
|
582 | - * @return int |
|
583 | - */ |
|
584 | - protected function _time($providedTime = null) |
|
585 | - { |
|
586 | - if ($providedTime) { |
|
587 | - return $providedTime; |
|
588 | - } |
|
589 | - |
|
590 | - return time(); |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * |
|
595 | - * @param string|null $param String to convert |
|
596 | - * @return array |
|
597 | - */ |
|
598 | - protected function _stringToArray($param) |
|
599 | - { |
|
600 | - if (!$param) { |
|
601 | - return []; |
|
602 | - } |
|
603 | - |
|
604 | - $array = Text::tokenize($param); |
|
605 | - |
|
606 | - return array_filter($array); |
|
607 | - } |
|
370 | + ] |
|
371 | + ]; |
|
372 | + $subcommandParserFull = $subcommandParser; |
|
373 | + $subcommandParserFull['options']['type'] = [ |
|
374 | + 'short' => 't', |
|
375 | + 'help' => 'Type (comma separated list possible)', |
|
376 | + 'default' => null |
|
377 | + ]; |
|
378 | + |
|
379 | + return parent::getOptionParser()->setDescription($this->getDescription()) |
|
380 | + ->addSubcommand('clean', [ |
|
381 | + 'help' => 'Remove old jobs (cleanup)', |
|
382 | + 'parser' => $subcommandParser |
|
383 | + ]) |
|
384 | + ->addSubcommand('add', [ |
|
385 | + 'help' => 'Add Job', |
|
386 | + 'parser' => $subcommandParser |
|
387 | + ]) |
|
388 | + ->addSubcommand('stats', [ |
|
389 | + 'help' => 'Stats', |
|
390 | + 'parser' => $subcommandParserFull |
|
391 | + ]) |
|
392 | + ->addSubcommand('settings', [ |
|
393 | + 'help' => 'Settings', |
|
394 | + 'parser' => $subcommandParserFull |
|
395 | + ]) |
|
396 | + ->addSubcommand('runworker', [ |
|
397 | + 'help' => 'Run Worker', |
|
398 | + 'parser' => $subcommandParserFull |
|
399 | + ]); |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Timestamped log. |
|
404 | + * |
|
405 | + * @param string $message Log type |
|
406 | + * @param string|null $pid PID of the process |
|
407 | + * @param bool $addDetails Details |
|
408 | + * @return void |
|
409 | + */ |
|
410 | + protected function _log($message, $pid = null, $addDetails = true) |
|
411 | + { |
|
412 | + if (!Configure::read('Queue.log')) { |
|
413 | + return; |
|
414 | + } |
|
415 | + |
|
416 | + if ($addDetails) { |
|
417 | + $timeNeeded = $this->_timeNeeded(); |
|
418 | + $memoryUsage = $this->_memoryUsage(); |
|
419 | + $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
420 | + } |
|
421 | + |
|
422 | + if ($pid) { |
|
423 | + $message .= ' (pid ' . $pid . ')'; |
|
424 | + } |
|
425 | + Log::write('info', $message, [ |
|
426 | + 'scope' => 'queue' |
|
427 | + ]); |
|
428 | + } |
|
429 | + |
|
430 | + /** |
|
431 | + * |
|
432 | + * @param string $message Message |
|
433 | + * @param string|null $pid PID of the process |
|
434 | + * @return void |
|
435 | + */ |
|
436 | + protected function _logError($message, $pid = null) |
|
437 | + { |
|
438 | + $timeNeeded = $this->_timeNeeded(); |
|
439 | + $memoryUsage = $this->_memoryUsage(); |
|
440 | + $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
441 | + |
|
442 | + if ($pid) { |
|
443 | + $message .= ' (pid ' . $pid . ')'; |
|
444 | + } |
|
445 | + |
|
446 | + Log::write('error', $message); |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Returns a List of available QueueTasks and their individual configurations. |
|
451 | + * |
|
452 | + * @return array |
|
453 | + */ |
|
454 | + protected function _getTaskConf() |
|
455 | + { |
|
456 | + if (!is_array($this->_taskConf)) { |
|
457 | + $this->_taskConf = []; |
|
458 | + foreach ($this->tasks as $task) { |
|
459 | + list ($pluginName, $taskName) = pluginSplit($task); |
|
460 | + |
|
461 | + $this->_taskConf[$taskName]['name'] = substr($taskName, 5); |
|
462 | + $this->_taskConf[$taskName]['plugin'] = $pluginName; |
|
463 | + if (property_exists($this->{$taskName}, 'timeout')) { |
|
464 | + $this->_taskConf[$taskName]['timeout'] = $this->{$taskName}->timeout; |
|
465 | + } else { |
|
466 | + $this->_taskConf[$taskName]['timeout'] = Config::defaultWorkerTimeout(); |
|
467 | + } |
|
468 | + if (property_exists($this->{$taskName}, 'retries')) { |
|
469 | + $this->_taskConf[$taskName]['retries'] = $this->{$taskName}->retries; |
|
470 | + } else { |
|
471 | + $this->_taskConf[$taskName]['retries'] = Config::defaultWorkerRetries(); |
|
472 | + } |
|
473 | + if (property_exists($this->{$taskName}, 'cleanupTimeout')) { |
|
474 | + $this->_taskConf[$taskName]['cleanupTimeout'] = $this->{$taskName}->cleanupTimeout; |
|
475 | + } else { |
|
476 | + $this->_taskConf[$taskName]['cleanupTimeout'] = Config::cleanupTimeout(); |
|
477 | + } |
|
478 | + } |
|
479 | + } |
|
480 | + |
|
481 | + return $this->_taskConf; |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * Signal handling to queue worker for clean shutdown |
|
486 | + * |
|
487 | + * @param int $signal The signal |
|
488 | + * @return void |
|
489 | + */ |
|
490 | + protected function _exit($signal) |
|
491 | + { |
|
492 | + $this->out(__d('queue', 'Caught %d signal, exiting.', $signal)); |
|
493 | + $this->_exit = true; |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * |
|
498 | + * @return void |
|
499 | + */ |
|
500 | + protected function _displayAvailableTasks() |
|
501 | + { |
|
502 | + $this->out('Available Tasks:'); |
|
503 | + foreach ($this->taskNames as $loadedTask) { |
|
504 | + $this->out("\t" . '* ' . $this->_taskName($loadedTask)); |
|
505 | + } |
|
506 | + } |
|
507 | + |
|
508 | + /** |
|
509 | + * |
|
510 | + * @return string |
|
511 | + */ |
|
512 | + protected function _initPid() |
|
513 | + { |
|
514 | + $this->_pid = $this->_retrievePid(); |
|
515 | + |
|
516 | + return $this->_pid; |
|
517 | + } |
|
518 | + |
|
519 | + /** |
|
520 | + * |
|
521 | + * @return string |
|
522 | + */ |
|
523 | + protected function _retrievePid() |
|
524 | + { |
|
525 | + if (function_exists('posix_getpid')) { |
|
526 | + $pid = (string)posix_getpid(); |
|
527 | + } else { |
|
528 | + $pid = $this->QueuedTasks->key(); |
|
529 | + } |
|
530 | + |
|
531 | + return $pid; |
|
532 | + } |
|
533 | + |
|
534 | + /** |
|
535 | + * |
|
536 | + * @return string Memory usage in MB. |
|
537 | + */ |
|
538 | + protected function _memoryUsage() |
|
539 | + { |
|
540 | + $limit = ini_get('memory_limit'); |
|
541 | + |
|
542 | + $used = number_format(memory_get_peak_usage(true) / (1024 * 1024), 0) . 'MB'; |
|
543 | + if ($limit !== '-1') { |
|
544 | + $used .= '/' . $limit; |
|
545 | + } |
|
546 | + |
|
547 | + return $used; |
|
548 | + } |
|
549 | + |
|
550 | + /** |
|
551 | + * |
|
552 | + * @param string|null $pid PID of the process |
|
553 | + * |
|
554 | + * @return void |
|
555 | + */ |
|
556 | + protected function _deletePid($pid) |
|
557 | + { |
|
558 | + if (!$pid) { |
|
559 | + $pid = $this->_pid; |
|
560 | + } |
|
561 | + if (!$pid) { |
|
562 | + return; |
|
563 | + } |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * |
|
568 | + * @return string |
|
569 | + */ |
|
570 | + protected function _timeNeeded() |
|
571 | + { |
|
572 | + $diff = $this->_time() - $this->_time($this->_time); |
|
573 | + $seconds = max($diff, 1); |
|
574 | + |
|
575 | + return $seconds . 's'; |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * |
|
580 | + * @param int|null $providedTime Provided time |
|
581 | + * |
|
582 | + * @return int |
|
583 | + */ |
|
584 | + protected function _time($providedTime = null) |
|
585 | + { |
|
586 | + if ($providedTime) { |
|
587 | + return $providedTime; |
|
588 | + } |
|
589 | + |
|
590 | + return time(); |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * |
|
595 | + * @param string|null $param String to convert |
|
596 | + * @return array |
|
597 | + */ |
|
598 | + protected function _stringToArray($param) |
|
599 | + { |
|
600 | + if (!$param) { |
|
601 | + return []; |
|
602 | + } |
|
603 | + |
|
604 | + $array = Text::tokenize($param); |
|
605 | + |
|
606 | + return array_filter($array); |
|
607 | + } |
|
608 | 608 | } |
@@ -28,596 +28,596 @@ |
||
28 | 28 | class QueuedTasksTable extends Table |
29 | 29 | { |
30 | 30 | |
31 | - const DRIVER_MYSQL = 'Mysql'; |
|
32 | - |
|
33 | - const DRIVER_POSTGRES = 'Postgres'; |
|
34 | - |
|
35 | - const DRIVER_SQLSERVER = 'Sqlserver'; |
|
36 | - |
|
37 | - const STATS_LIMIT = 100000; |
|
38 | - |
|
39 | - /** |
|
40 | - * |
|
41 | - * @var string|null |
|
42 | - */ |
|
43 | - protected $_key; |
|
44 | - |
|
45 | - /** |
|
46 | - * Initialize method |
|
47 | - * |
|
48 | - * @param array $config The configuration for the Table. |
|
49 | - * @return void |
|
50 | - */ |
|
51 | - public function initialize(array $config) |
|
52 | - { |
|
53 | - parent::initialize($config); |
|
54 | - |
|
55 | - $this->setTable('queued_tasks'); |
|
56 | - $this->setDisplayField('id'); |
|
57 | - $this->setPrimaryKey('id'); |
|
58 | - |
|
59 | - $this->addBehavior('Timestamp'); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * set connection name |
|
64 | - * |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public static function defaultConnectionName() |
|
68 | - { |
|
69 | - $connection = Configure::read('Queue.connection'); |
|
70 | - if (!empty($connection)) { |
|
71 | - return $connection; |
|
72 | - } |
|
73 | - |
|
74 | - return parent::defaultConnectionName(); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * |
|
79 | - * @param \Cake\Event\Event $event Model event |
|
80 | - * @param \ArrayObject $data The data |
|
81 | - * @param \ArrayObject $options The options |
|
82 | - * @return void |
|
83 | - */ |
|
84 | - public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) |
|
85 | - { |
|
86 | - if (isset($data['data']) && $data['data'] === '') { |
|
87 | - $data['data'] = null; |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Adds a new job to the queue. |
|
93 | - * |
|
94 | - * @param string $taskName Task name |
|
95 | - * @param array|null $data Array of data |
|
96 | - * @param string $notBefore A datetime which indicates when the job may be executed |
|
97 | - * @return \Queue\Model\Entity\QueuedTask Saved job entity |
|
98 | - */ |
|
99 | - public function createJob($taskName, array $data = null, string $notBefore = null) |
|
100 | - { |
|
101 | - $task = [ |
|
102 | - 'task' => $taskName, |
|
103 | - 'data' => serialize($data), |
|
104 | - 'not_before' => $this->getDateTime() |
|
105 | - ]; |
|
106 | - |
|
107 | - if (!empty($notBefore)) { |
|
108 | - $task['not_before'] = $this->getDateTime(strtotime($notBefore)); |
|
109 | - } |
|
110 | - |
|
111 | - $queuedTask = $this->newEntity($task); |
|
112 | - var_dump($task); |
|
113 | - var_dump($queuedTask); |
|
114 | - |
|
115 | - return $this->saveOrFail($queuedTask); |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * |
|
120 | - * @param string|null $taskName Task name |
|
121 | - * @return bool |
|
122 | - */ |
|
123 | - public function isQueued($taskName = null) |
|
124 | - { |
|
125 | - $conditions = [ |
|
126 | - 'completed IS' => null |
|
127 | - ]; |
|
128 | - if ($taskName) { |
|
129 | - $conditions['task'] = $taskName; |
|
130 | - } |
|
131 | - |
|
132 | - return (bool)$this->find() |
|
133 | - ->where($conditions) |
|
134 | - ->select([ |
|
135 | - 'id' |
|
136 | - ]) |
|
137 | - ->first(); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Returns the number of items in the queue. |
|
142 | - * Either returns the number of ALL pending jobs, or the number of pending jobs of the passed type. |
|
143 | - * |
|
144 | - * @param string|null $taskName Task type to Count |
|
145 | - * @return int |
|
146 | - */ |
|
147 | - public function getLength($taskName = null) |
|
148 | - { |
|
149 | - $findConf = [ |
|
150 | - 'conditions' => [ |
|
151 | - 'completed IS' => null |
|
152 | - ] |
|
153 | - ]; |
|
154 | - if ($taskName !== null) { |
|
155 | - $findConf['conditions']['task'] = $taskName; |
|
156 | - } |
|
157 | - |
|
158 | - return $this->find('all', $findConf)->count(); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Return a list of all task types in the Queue. |
|
163 | - * |
|
164 | - * @return \Cake\ORM\Query |
|
165 | - */ |
|
166 | - public function getTypes() |
|
167 | - { |
|
168 | - $findCond = [ |
|
169 | - 'fields' => [ |
|
170 | - 'task' |
|
171 | - ], |
|
172 | - 'group' => [ |
|
173 | - 'task' |
|
174 | - ], |
|
175 | - 'keyField' => 'task', |
|
176 | - 'valueField' => 'task' |
|
177 | - ]; |
|
178 | - |
|
179 | - return $this->find('list', $findCond); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Return some statistics about finished jobs still in the Database. |
|
184 | - * TO-DO: rewrite as virtual field |
|
185 | - * |
|
186 | - * @return \Cake\ORM\Query |
|
187 | - */ |
|
188 | - public function getStats() |
|
189 | - { |
|
190 | - $driverName = $this->_getDriverName(); |
|
191 | - $options = [ |
|
192 | - 'fields' => function (Query $query) use ($driverName) { |
|
193 | - $alltime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(created)'); |
|
194 | - $runtime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
195 | - $fetchdelay = $query->func()->avg('UNIX_TIMESTAMP(fetched) - IF(not_before is NULL, UNIX_TIMESTAMP(created), UNIX_TIMESTAMP(not_before))'); |
|
196 | - switch ($driverName) { |
|
197 | - case static::DRIVER_SQLSERVER: |
|
198 | - $alltime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', created)"); |
|
199 | - $runtime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
200 | - $fetchdelay = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', fetched) - (CASE WHEN not_before IS NULL THEN DATEDIFF(s, '1970-01-01 00:00:00', created) ELSE DATEDIFF(s, '1970-01-01 00:00:00', not_before) END)"); |
|
201 | - break; |
|
202 | - } |
|
203 | - /** |
|
204 | - * |
|
205 | - * @var \Cake\ORM\Query |
|
206 | - */ |
|
207 | - return [ |
|
208 | - 'task', |
|
209 | - 'num' => $query->func()->count('*'), |
|
210 | - 'alltime' => $alltime, |
|
211 | - 'runtime' => $runtime, |
|
212 | - 'fetchdelay' => $fetchdelay |
|
213 | - ]; |
|
214 | - }, |
|
215 | - 'conditions' => [ |
|
216 | - 'completed IS NOT' => null |
|
217 | - ], |
|
218 | - 'group' => [ |
|
219 | - 'task' |
|
220 | - ] |
|
221 | - ]; |
|
222 | - |
|
223 | - return $this->find('all', $options); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Returns [ |
|
228 | - * 'Task' => [ |
|
229 | - * 'YYYY-MM-DD' => INT, |
|
230 | - * ... |
|
231 | - * ] |
|
232 | - * ] |
|
233 | - * |
|
234 | - * @param string|null $taskName The task name |
|
235 | - * @return array |
|
236 | - */ |
|
237 | - public function getFullStats($taskName = null) |
|
238 | - { |
|
239 | - $driverName = $this->_getDriverName(); |
|
240 | - $fields = function (Query $query) use ($driverName) { |
|
241 | - $runtime = $query->newExpr('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
242 | - switch ($driverName) { |
|
243 | - case static::DRIVER_SQLSERVER: |
|
244 | - $runtime = $query->newExpr("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
245 | - break; |
|
246 | - } |
|
247 | - |
|
248 | - return [ |
|
249 | - 'task', |
|
250 | - 'created', |
|
251 | - 'duration' => $runtime |
|
252 | - ]; |
|
253 | - }; |
|
254 | - |
|
255 | - $conditions = [ |
|
256 | - 'completed IS NOT' => null |
|
257 | - ]; |
|
258 | - if ($taskName) { |
|
259 | - $conditions['task'] = $taskName; |
|
260 | - } |
|
261 | - |
|
262 | - $tasks = $this->find() |
|
263 | - ->select($fields) |
|
264 | - ->where($conditions) |
|
265 | - ->enableHydration(false) |
|
266 | - ->orderDesc('id') |
|
267 | - ->limit(static::STATS_LIMIT) |
|
268 | - ->all() |
|
269 | - ->toArray(); |
|
270 | - |
|
271 | - $result = []; |
|
272 | - |
|
273 | - $days = []; |
|
274 | - |
|
275 | - foreach ($tasks as $task) { |
|
276 | - /** @var \DateTime $created */ |
|
277 | - $created = $task['created']; |
|
278 | - $day = $created->format('Y-m-d'); |
|
279 | - if (!isset($days[$day])) { |
|
280 | - $days[$day] = $day; |
|
281 | - } |
|
282 | - |
|
283 | - $result[$task['task']][$day][] = $task['duration']; |
|
284 | - } |
|
285 | - |
|
286 | - foreach ($result as $type => $tasks) { |
|
287 | - foreach ($tasks as $day => $durations) { |
|
288 | - $average = array_sum($durations) / count($durations); |
|
289 | - $result[$type][$day] = (int)$average; |
|
290 | - } |
|
291 | - |
|
292 | - foreach ($days as $day) { |
|
293 | - if (isset($result[$type][$day])) { |
|
294 | - continue; |
|
295 | - } |
|
296 | - |
|
297 | - $result[$type][$day] = 0; |
|
298 | - } |
|
299 | - |
|
300 | - ksort($result[$type]); |
|
301 | - } |
|
302 | - |
|
303 | - return $result; |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Look for a new job that can be processed with the current abilities and |
|
308 | - * from the specified group (or any if null). |
|
309 | - * |
|
310 | - * @param array $capabilities Available QueueWorkerTasks. |
|
311 | - * @param array $types Request a job from these types (or exclude certain types), or any otherwise. |
|
312 | - * @return \Queue\Model\Entity\QueuedTask|null |
|
313 | - */ |
|
314 | - public function requestJob(array $capabilities, array $types = []) |
|
315 | - { |
|
316 | - $now = $this->getDateTime(); |
|
317 | - $nowStr = $now->toDateTimeString(); |
|
318 | - $driverName = $this->_getDriverName(); |
|
319 | - |
|
320 | - $query = $this->find(); |
|
321 | - $age = $query->newExpr()->add('IFNULL(TIMESTAMPDIFF(SECOND, "' . $nowStr . '", not_before), 0)'); |
|
322 | - switch ($driverName) { |
|
323 | - case static::DRIVER_SQLSERVER: |
|
324 | - $age = $query->newExpr()->add('ISNULL(DATEDIFF(SECOND, GETDATE(), not_before), 0)'); |
|
325 | - break; |
|
326 | - case static::DRIVER_POSTGRES: |
|
327 | - $age = $query->newExpr()->add('COALESCE((EXTRACT(EPOCH FROM now()) - EXTRACT(EPOCH FROM not_before)), 0)'); |
|
328 | - break; |
|
329 | - } |
|
330 | - $options = [ |
|
331 | - 'conditions' => [ |
|
332 | - 'completed IS' => null, |
|
333 | - 'OR' => [] |
|
334 | - ], |
|
335 | - 'fields' => [ |
|
336 | - 'age' => $age |
|
337 | - ], |
|
338 | - 'order' => [ |
|
339 | - 'age' => 'ASC', |
|
340 | - 'id' => 'ASC' |
|
341 | - ] |
|
342 | - ]; |
|
343 | - |
|
344 | - if ($types) { |
|
345 | - $options['conditions'] = $this->addFilter($options['conditions'], 'task', $types); |
|
346 | - } |
|
347 | - |
|
348 | - // Generate the task specific conditions. |
|
349 | - foreach ($capabilities as $task) { |
|
350 | - list ($plugin, $name) = pluginSplit($task['name']); |
|
351 | - $timeoutAt = $now->copy(); |
|
352 | - $tmp = [ |
|
353 | - 'task' => $name, |
|
354 | - 'AND' => [ |
|
355 | - [ |
|
356 | - 'OR' => [ |
|
357 | - 'not_before <' => $nowStr, |
|
358 | - 'not_before IS' => null |
|
359 | - ] |
|
360 | - ], |
|
361 | - [ |
|
362 | - 'OR' => [ |
|
363 | - 'fetched <' => $timeoutAt->subSeconds($task['timeout']), |
|
364 | - 'fetched IS' => null |
|
365 | - ] |
|
366 | - ] |
|
367 | - ], |
|
368 | - 'failed_count <' => ($task['retries'] + 1) |
|
369 | - ]; |
|
370 | - $options['conditions']['OR'][] = $tmp; |
|
371 | - } |
|
372 | - |
|
373 | - /** @var \Queue\Model\Entity\QueuedTask|null $task */ |
|
374 | - $task = $this->getConnection()->transactional(function () use ($query, $options, $now) { |
|
375 | - $task = $query->find('all', $options) |
|
376 | - ->enableAutoFields(true) |
|
377 | - ->epilog('FOR UPDATE') |
|
378 | - ->first(); |
|
379 | - |
|
380 | - if (!$task) { |
|
381 | - return null; |
|
382 | - } |
|
383 | - |
|
384 | - $key = $this->key(); |
|
385 | - $task = $this->patchEntity($task, [ |
|
386 | - 'worker_key' => $key, |
|
387 | - 'fetched' => $now |
|
388 | - ]); |
|
389 | - |
|
390 | - return $this->saveOrFail($task); |
|
391 | - }); |
|
392 | - |
|
393 | - if (!$task) { |
|
394 | - return null; |
|
395 | - } |
|
396 | - |
|
397 | - return $task; |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * Mark a task as Completed, removing it from the queue. |
|
402 | - * |
|
403 | - * @param \Queue\Model\Entity\QueuedTask $task Task |
|
404 | - * @return bool Success |
|
405 | - */ |
|
406 | - public function markJobDone(QueuedTask $task) |
|
407 | - { |
|
408 | - $fields = [ |
|
409 | - 'completed' => $this->getDateTime() |
|
410 | - ]; |
|
411 | - $task = $this->patchEntity($task, $fields); |
|
412 | - |
|
413 | - return (bool)$this->save($task); |
|
414 | - } |
|
415 | - |
|
416 | - /** |
|
417 | - * Mark a job as Failed, incrementing the failed-counter and Requeueing it. |
|
418 | - * |
|
419 | - * @param \Queue\Model\Entity\QueuedTask $task Task |
|
420 | - * @param string|null $failureMessage Optional message to append to the failure_message field. |
|
421 | - * @return bool Success |
|
422 | - */ |
|
423 | - public function markJobFailed(QueuedTask $task, $failureMessage = null) |
|
424 | - { |
|
425 | - $fields = [ |
|
426 | - 'failed_count' => $task->failed_count + 1, |
|
427 | - 'failure_message' => $failureMessage |
|
428 | - ]; |
|
429 | - $task = $this->patchEntity($task, $fields); |
|
430 | - |
|
431 | - return (bool)$this->save($task); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Reset current jobs |
|
436 | - * |
|
437 | - * @param int|null $id ID |
|
438 | - * |
|
439 | - * @return int Success |
|
440 | - */ |
|
441 | - public function reset($id = null) |
|
442 | - { |
|
443 | - $fields = [ |
|
444 | - 'completed' => null, |
|
445 | - 'fetched' => null, |
|
446 | - 'failed_count' => 0, |
|
447 | - 'worker_key' => null, |
|
448 | - 'failure_message' => null |
|
449 | - ]; |
|
450 | - $conditions = [ |
|
451 | - 'completed IS' => null |
|
452 | - ]; |
|
453 | - if ($id) { |
|
454 | - $conditions['id'] = $id; |
|
455 | - } |
|
456 | - |
|
457 | - return $this->updateAll($fields, $conditions); |
|
458 | - } |
|
459 | - |
|
460 | - /** |
|
461 | - * |
|
462 | - * @param string $taskName Task name |
|
463 | - * |
|
464 | - * @return int |
|
465 | - */ |
|
466 | - public function rerun($taskName) |
|
467 | - { |
|
468 | - $fields = [ |
|
469 | - 'completed' => null, |
|
470 | - 'fetched' => null, |
|
471 | - 'failed_count' => 0, |
|
472 | - 'worker_key' => null, |
|
473 | - 'failure_message' => null |
|
474 | - ]; |
|
475 | - $conditions = [ |
|
476 | - 'completed IS NOT' => null, |
|
477 | - 'task' => $taskName |
|
478 | - ]; |
|
479 | - |
|
480 | - return $this->updateAll($fields, $conditions); |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Cleanup/Delete Completed Tasks. |
|
485 | - * |
|
486 | - * @return void |
|
487 | - */ |
|
488 | - public function cleanOldJobs() |
|
489 | - { |
|
490 | - if (!Configure::read('Queue.cleanuptimeout')) { |
|
491 | - return; |
|
492 | - } |
|
493 | - |
|
494 | - $this->deleteAll([ |
|
495 | - 'completed <' => time() - (int)Configure::read('Queue.cleanuptimeout') |
|
496 | - ]); |
|
497 | - } |
|
498 | - |
|
499 | - /** |
|
500 | - * |
|
501 | - * @param \Queue\Model\Entity\QueuedTask $queuedTask Queued task |
|
502 | - * @param array $taskConfiguration Task configuration |
|
503 | - * @return string |
|
504 | - */ |
|
505 | - public function getFailedStatus($queuedTask, array $taskConfiguration) |
|
506 | - { |
|
507 | - $failureMessageRequeued = 'requeued'; |
|
508 | - |
|
509 | - $queuedTaskName = 'Queue' . $queuedTask->task; |
|
510 | - if (empty($taskConfiguration[$queuedTaskName])) { |
|
511 | - return $failureMessageRequeued; |
|
512 | - } |
|
513 | - $retries = $taskConfiguration[$queuedTaskName]['retries']; |
|
514 | - if ($queuedTask->failed_count <= $retries) { |
|
515 | - return $failureMessageRequeued; |
|
516 | - } |
|
517 | - |
|
518 | - return 'aborted'; |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Generates a unique Identifier for the current worker thread. |
|
523 | - * |
|
524 | - * Useful to identify the currently running processes for this thread. |
|
525 | - * |
|
526 | - * @return string Identifier |
|
527 | - */ |
|
528 | - public function key() |
|
529 | - { |
|
530 | - if ($this->_key !== null) { |
|
531 | - return $this->_key; |
|
532 | - } |
|
533 | - $this->_key = sha1(microtime()); |
|
534 | - if (!$this->_key) { |
|
535 | - throw new RuntimeException('Invalid key generated'); |
|
536 | - } |
|
537 | - |
|
538 | - return $this->_key; |
|
539 | - } |
|
540 | - |
|
541 | - /** |
|
542 | - * Resets worker Identifier |
|
543 | - * |
|
544 | - * @return void |
|
545 | - */ |
|
546 | - public function clearKey() |
|
547 | - { |
|
548 | - $this->_key = null; |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * truncate() |
|
553 | - * |
|
554 | - * @return void |
|
555 | - */ |
|
556 | - public function truncate() |
|
557 | - { |
|
558 | - $sql = $this->getSchema()->truncateSql($this->_connection); |
|
559 | - foreach ($sql as $snippet) { |
|
560 | - $this->_connection->execute($snippet); |
|
561 | - } |
|
562 | - } |
|
563 | - |
|
564 | - /** |
|
565 | - * get the name of the driver |
|
566 | - * |
|
567 | - * @return string |
|
568 | - */ |
|
569 | - protected function _getDriverName() |
|
570 | - { |
|
571 | - $className = explode('\\', $this->getConnection()->config()['driver']); |
|
572 | - $name = end($className); |
|
573 | - |
|
574 | - return $name; |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * |
|
579 | - * @param array $conditions Conditions |
|
580 | - * @param string $key Key |
|
581 | - * @param array $values Values |
|
582 | - * @return array |
|
583 | - */ |
|
584 | - protected function addFilter(array $conditions, $key, array $values) |
|
585 | - { |
|
586 | - $include = []; |
|
587 | - $exclude = []; |
|
588 | - foreach ($values as $value) { |
|
589 | - if (substr($value, 0, 1) === '-') { |
|
590 | - $exclude[] = substr($value, 1); |
|
591 | - } else { |
|
592 | - $include[] = $value; |
|
593 | - } |
|
594 | - } |
|
595 | - |
|
596 | - if ($include) { |
|
597 | - $conditions[$key . ' IN'] = $include; |
|
598 | - } |
|
599 | - if ($exclude) { |
|
600 | - $conditions[$key . ' NOT IN'] = $exclude; |
|
601 | - } |
|
602 | - |
|
603 | - return $conditions; |
|
604 | - } |
|
605 | - |
|
606 | - /** |
|
607 | - * Returns a DateTime object from different input. |
|
608 | - * |
|
609 | - * Without argument this will be "now". |
|
610 | - * |
|
611 | - * @param int|string|\Cake\I18n\FrozenTime|\Cake\I18n\Time|null $notBefore Not before time |
|
612 | - * |
|
613 | - * @return \Cake\I18n\FrozenTime|\Cake\I18n\Time |
|
614 | - */ |
|
615 | - protected function getDateTime($notBefore = null) |
|
616 | - { |
|
617 | - if (is_object($notBefore)) { |
|
618 | - return $notBefore; |
|
619 | - } |
|
620 | - |
|
621 | - return new FrozenTime($notBefore); |
|
622 | - } |
|
31 | + const DRIVER_MYSQL = 'Mysql'; |
|
32 | + |
|
33 | + const DRIVER_POSTGRES = 'Postgres'; |
|
34 | + |
|
35 | + const DRIVER_SQLSERVER = 'Sqlserver'; |
|
36 | + |
|
37 | + const STATS_LIMIT = 100000; |
|
38 | + |
|
39 | + /** |
|
40 | + * |
|
41 | + * @var string|null |
|
42 | + */ |
|
43 | + protected $_key; |
|
44 | + |
|
45 | + /** |
|
46 | + * Initialize method |
|
47 | + * |
|
48 | + * @param array $config The configuration for the Table. |
|
49 | + * @return void |
|
50 | + */ |
|
51 | + public function initialize(array $config) |
|
52 | + { |
|
53 | + parent::initialize($config); |
|
54 | + |
|
55 | + $this->setTable('queued_tasks'); |
|
56 | + $this->setDisplayField('id'); |
|
57 | + $this->setPrimaryKey('id'); |
|
58 | + |
|
59 | + $this->addBehavior('Timestamp'); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * set connection name |
|
64 | + * |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public static function defaultConnectionName() |
|
68 | + { |
|
69 | + $connection = Configure::read('Queue.connection'); |
|
70 | + if (!empty($connection)) { |
|
71 | + return $connection; |
|
72 | + } |
|
73 | + |
|
74 | + return parent::defaultConnectionName(); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * |
|
79 | + * @param \Cake\Event\Event $event Model event |
|
80 | + * @param \ArrayObject $data The data |
|
81 | + * @param \ArrayObject $options The options |
|
82 | + * @return void |
|
83 | + */ |
|
84 | + public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) |
|
85 | + { |
|
86 | + if (isset($data['data']) && $data['data'] === '') { |
|
87 | + $data['data'] = null; |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Adds a new job to the queue. |
|
93 | + * |
|
94 | + * @param string $taskName Task name |
|
95 | + * @param array|null $data Array of data |
|
96 | + * @param string $notBefore A datetime which indicates when the job may be executed |
|
97 | + * @return \Queue\Model\Entity\QueuedTask Saved job entity |
|
98 | + */ |
|
99 | + public function createJob($taskName, array $data = null, string $notBefore = null) |
|
100 | + { |
|
101 | + $task = [ |
|
102 | + 'task' => $taskName, |
|
103 | + 'data' => serialize($data), |
|
104 | + 'not_before' => $this->getDateTime() |
|
105 | + ]; |
|
106 | + |
|
107 | + if (!empty($notBefore)) { |
|
108 | + $task['not_before'] = $this->getDateTime(strtotime($notBefore)); |
|
109 | + } |
|
110 | + |
|
111 | + $queuedTask = $this->newEntity($task); |
|
112 | + var_dump($task); |
|
113 | + var_dump($queuedTask); |
|
114 | + |
|
115 | + return $this->saveOrFail($queuedTask); |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * |
|
120 | + * @param string|null $taskName Task name |
|
121 | + * @return bool |
|
122 | + */ |
|
123 | + public function isQueued($taskName = null) |
|
124 | + { |
|
125 | + $conditions = [ |
|
126 | + 'completed IS' => null |
|
127 | + ]; |
|
128 | + if ($taskName) { |
|
129 | + $conditions['task'] = $taskName; |
|
130 | + } |
|
131 | + |
|
132 | + return (bool)$this->find() |
|
133 | + ->where($conditions) |
|
134 | + ->select([ |
|
135 | + 'id' |
|
136 | + ]) |
|
137 | + ->first(); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Returns the number of items in the queue. |
|
142 | + * Either returns the number of ALL pending jobs, or the number of pending jobs of the passed type. |
|
143 | + * |
|
144 | + * @param string|null $taskName Task type to Count |
|
145 | + * @return int |
|
146 | + */ |
|
147 | + public function getLength($taskName = null) |
|
148 | + { |
|
149 | + $findConf = [ |
|
150 | + 'conditions' => [ |
|
151 | + 'completed IS' => null |
|
152 | + ] |
|
153 | + ]; |
|
154 | + if ($taskName !== null) { |
|
155 | + $findConf['conditions']['task'] = $taskName; |
|
156 | + } |
|
157 | + |
|
158 | + return $this->find('all', $findConf)->count(); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Return a list of all task types in the Queue. |
|
163 | + * |
|
164 | + * @return \Cake\ORM\Query |
|
165 | + */ |
|
166 | + public function getTypes() |
|
167 | + { |
|
168 | + $findCond = [ |
|
169 | + 'fields' => [ |
|
170 | + 'task' |
|
171 | + ], |
|
172 | + 'group' => [ |
|
173 | + 'task' |
|
174 | + ], |
|
175 | + 'keyField' => 'task', |
|
176 | + 'valueField' => 'task' |
|
177 | + ]; |
|
178 | + |
|
179 | + return $this->find('list', $findCond); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Return some statistics about finished jobs still in the Database. |
|
184 | + * TO-DO: rewrite as virtual field |
|
185 | + * |
|
186 | + * @return \Cake\ORM\Query |
|
187 | + */ |
|
188 | + public function getStats() |
|
189 | + { |
|
190 | + $driverName = $this->_getDriverName(); |
|
191 | + $options = [ |
|
192 | + 'fields' => function (Query $query) use ($driverName) { |
|
193 | + $alltime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(created)'); |
|
194 | + $runtime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
195 | + $fetchdelay = $query->func()->avg('UNIX_TIMESTAMP(fetched) - IF(not_before is NULL, UNIX_TIMESTAMP(created), UNIX_TIMESTAMP(not_before))'); |
|
196 | + switch ($driverName) { |
|
197 | + case static::DRIVER_SQLSERVER: |
|
198 | + $alltime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', created)"); |
|
199 | + $runtime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
200 | + $fetchdelay = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', fetched) - (CASE WHEN not_before IS NULL THEN DATEDIFF(s, '1970-01-01 00:00:00', created) ELSE DATEDIFF(s, '1970-01-01 00:00:00', not_before) END)"); |
|
201 | + break; |
|
202 | + } |
|
203 | + /** |
|
204 | + * |
|
205 | + * @var \Cake\ORM\Query |
|
206 | + */ |
|
207 | + return [ |
|
208 | + 'task', |
|
209 | + 'num' => $query->func()->count('*'), |
|
210 | + 'alltime' => $alltime, |
|
211 | + 'runtime' => $runtime, |
|
212 | + 'fetchdelay' => $fetchdelay |
|
213 | + ]; |
|
214 | + }, |
|
215 | + 'conditions' => [ |
|
216 | + 'completed IS NOT' => null |
|
217 | + ], |
|
218 | + 'group' => [ |
|
219 | + 'task' |
|
220 | + ] |
|
221 | + ]; |
|
222 | + |
|
223 | + return $this->find('all', $options); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Returns [ |
|
228 | + * 'Task' => [ |
|
229 | + * 'YYYY-MM-DD' => INT, |
|
230 | + * ... |
|
231 | + * ] |
|
232 | + * ] |
|
233 | + * |
|
234 | + * @param string|null $taskName The task name |
|
235 | + * @return array |
|
236 | + */ |
|
237 | + public function getFullStats($taskName = null) |
|
238 | + { |
|
239 | + $driverName = $this->_getDriverName(); |
|
240 | + $fields = function (Query $query) use ($driverName) { |
|
241 | + $runtime = $query->newExpr('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
242 | + switch ($driverName) { |
|
243 | + case static::DRIVER_SQLSERVER: |
|
244 | + $runtime = $query->newExpr("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
245 | + break; |
|
246 | + } |
|
247 | + |
|
248 | + return [ |
|
249 | + 'task', |
|
250 | + 'created', |
|
251 | + 'duration' => $runtime |
|
252 | + ]; |
|
253 | + }; |
|
254 | + |
|
255 | + $conditions = [ |
|
256 | + 'completed IS NOT' => null |
|
257 | + ]; |
|
258 | + if ($taskName) { |
|
259 | + $conditions['task'] = $taskName; |
|
260 | + } |
|
261 | + |
|
262 | + $tasks = $this->find() |
|
263 | + ->select($fields) |
|
264 | + ->where($conditions) |
|
265 | + ->enableHydration(false) |
|
266 | + ->orderDesc('id') |
|
267 | + ->limit(static::STATS_LIMIT) |
|
268 | + ->all() |
|
269 | + ->toArray(); |
|
270 | + |
|
271 | + $result = []; |
|
272 | + |
|
273 | + $days = []; |
|
274 | + |
|
275 | + foreach ($tasks as $task) { |
|
276 | + /** @var \DateTime $created */ |
|
277 | + $created = $task['created']; |
|
278 | + $day = $created->format('Y-m-d'); |
|
279 | + if (!isset($days[$day])) { |
|
280 | + $days[$day] = $day; |
|
281 | + } |
|
282 | + |
|
283 | + $result[$task['task']][$day][] = $task['duration']; |
|
284 | + } |
|
285 | + |
|
286 | + foreach ($result as $type => $tasks) { |
|
287 | + foreach ($tasks as $day => $durations) { |
|
288 | + $average = array_sum($durations) / count($durations); |
|
289 | + $result[$type][$day] = (int)$average; |
|
290 | + } |
|
291 | + |
|
292 | + foreach ($days as $day) { |
|
293 | + if (isset($result[$type][$day])) { |
|
294 | + continue; |
|
295 | + } |
|
296 | + |
|
297 | + $result[$type][$day] = 0; |
|
298 | + } |
|
299 | + |
|
300 | + ksort($result[$type]); |
|
301 | + } |
|
302 | + |
|
303 | + return $result; |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Look for a new job that can be processed with the current abilities and |
|
308 | + * from the specified group (or any if null). |
|
309 | + * |
|
310 | + * @param array $capabilities Available QueueWorkerTasks. |
|
311 | + * @param array $types Request a job from these types (or exclude certain types), or any otherwise. |
|
312 | + * @return \Queue\Model\Entity\QueuedTask|null |
|
313 | + */ |
|
314 | + public function requestJob(array $capabilities, array $types = []) |
|
315 | + { |
|
316 | + $now = $this->getDateTime(); |
|
317 | + $nowStr = $now->toDateTimeString(); |
|
318 | + $driverName = $this->_getDriverName(); |
|
319 | + |
|
320 | + $query = $this->find(); |
|
321 | + $age = $query->newExpr()->add('IFNULL(TIMESTAMPDIFF(SECOND, "' . $nowStr . '", not_before), 0)'); |
|
322 | + switch ($driverName) { |
|
323 | + case static::DRIVER_SQLSERVER: |
|
324 | + $age = $query->newExpr()->add('ISNULL(DATEDIFF(SECOND, GETDATE(), not_before), 0)'); |
|
325 | + break; |
|
326 | + case static::DRIVER_POSTGRES: |
|
327 | + $age = $query->newExpr()->add('COALESCE((EXTRACT(EPOCH FROM now()) - EXTRACT(EPOCH FROM not_before)), 0)'); |
|
328 | + break; |
|
329 | + } |
|
330 | + $options = [ |
|
331 | + 'conditions' => [ |
|
332 | + 'completed IS' => null, |
|
333 | + 'OR' => [] |
|
334 | + ], |
|
335 | + 'fields' => [ |
|
336 | + 'age' => $age |
|
337 | + ], |
|
338 | + 'order' => [ |
|
339 | + 'age' => 'ASC', |
|
340 | + 'id' => 'ASC' |
|
341 | + ] |
|
342 | + ]; |
|
343 | + |
|
344 | + if ($types) { |
|
345 | + $options['conditions'] = $this->addFilter($options['conditions'], 'task', $types); |
|
346 | + } |
|
347 | + |
|
348 | + // Generate the task specific conditions. |
|
349 | + foreach ($capabilities as $task) { |
|
350 | + list ($plugin, $name) = pluginSplit($task['name']); |
|
351 | + $timeoutAt = $now->copy(); |
|
352 | + $tmp = [ |
|
353 | + 'task' => $name, |
|
354 | + 'AND' => [ |
|
355 | + [ |
|
356 | + 'OR' => [ |
|
357 | + 'not_before <' => $nowStr, |
|
358 | + 'not_before IS' => null |
|
359 | + ] |
|
360 | + ], |
|
361 | + [ |
|
362 | + 'OR' => [ |
|
363 | + 'fetched <' => $timeoutAt->subSeconds($task['timeout']), |
|
364 | + 'fetched IS' => null |
|
365 | + ] |
|
366 | + ] |
|
367 | + ], |
|
368 | + 'failed_count <' => ($task['retries'] + 1) |
|
369 | + ]; |
|
370 | + $options['conditions']['OR'][] = $tmp; |
|
371 | + } |
|
372 | + |
|
373 | + /** @var \Queue\Model\Entity\QueuedTask|null $task */ |
|
374 | + $task = $this->getConnection()->transactional(function () use ($query, $options, $now) { |
|
375 | + $task = $query->find('all', $options) |
|
376 | + ->enableAutoFields(true) |
|
377 | + ->epilog('FOR UPDATE') |
|
378 | + ->first(); |
|
379 | + |
|
380 | + if (!$task) { |
|
381 | + return null; |
|
382 | + } |
|
383 | + |
|
384 | + $key = $this->key(); |
|
385 | + $task = $this->patchEntity($task, [ |
|
386 | + 'worker_key' => $key, |
|
387 | + 'fetched' => $now |
|
388 | + ]); |
|
389 | + |
|
390 | + return $this->saveOrFail($task); |
|
391 | + }); |
|
392 | + |
|
393 | + if (!$task) { |
|
394 | + return null; |
|
395 | + } |
|
396 | + |
|
397 | + return $task; |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * Mark a task as Completed, removing it from the queue. |
|
402 | + * |
|
403 | + * @param \Queue\Model\Entity\QueuedTask $task Task |
|
404 | + * @return bool Success |
|
405 | + */ |
|
406 | + public function markJobDone(QueuedTask $task) |
|
407 | + { |
|
408 | + $fields = [ |
|
409 | + 'completed' => $this->getDateTime() |
|
410 | + ]; |
|
411 | + $task = $this->patchEntity($task, $fields); |
|
412 | + |
|
413 | + return (bool)$this->save($task); |
|
414 | + } |
|
415 | + |
|
416 | + /** |
|
417 | + * Mark a job as Failed, incrementing the failed-counter and Requeueing it. |
|
418 | + * |
|
419 | + * @param \Queue\Model\Entity\QueuedTask $task Task |
|
420 | + * @param string|null $failureMessage Optional message to append to the failure_message field. |
|
421 | + * @return bool Success |
|
422 | + */ |
|
423 | + public function markJobFailed(QueuedTask $task, $failureMessage = null) |
|
424 | + { |
|
425 | + $fields = [ |
|
426 | + 'failed_count' => $task->failed_count + 1, |
|
427 | + 'failure_message' => $failureMessage |
|
428 | + ]; |
|
429 | + $task = $this->patchEntity($task, $fields); |
|
430 | + |
|
431 | + return (bool)$this->save($task); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Reset current jobs |
|
436 | + * |
|
437 | + * @param int|null $id ID |
|
438 | + * |
|
439 | + * @return int Success |
|
440 | + */ |
|
441 | + public function reset($id = null) |
|
442 | + { |
|
443 | + $fields = [ |
|
444 | + 'completed' => null, |
|
445 | + 'fetched' => null, |
|
446 | + 'failed_count' => 0, |
|
447 | + 'worker_key' => null, |
|
448 | + 'failure_message' => null |
|
449 | + ]; |
|
450 | + $conditions = [ |
|
451 | + 'completed IS' => null |
|
452 | + ]; |
|
453 | + if ($id) { |
|
454 | + $conditions['id'] = $id; |
|
455 | + } |
|
456 | + |
|
457 | + return $this->updateAll($fields, $conditions); |
|
458 | + } |
|
459 | + |
|
460 | + /** |
|
461 | + * |
|
462 | + * @param string $taskName Task name |
|
463 | + * |
|
464 | + * @return int |
|
465 | + */ |
|
466 | + public function rerun($taskName) |
|
467 | + { |
|
468 | + $fields = [ |
|
469 | + 'completed' => null, |
|
470 | + 'fetched' => null, |
|
471 | + 'failed_count' => 0, |
|
472 | + 'worker_key' => null, |
|
473 | + 'failure_message' => null |
|
474 | + ]; |
|
475 | + $conditions = [ |
|
476 | + 'completed IS NOT' => null, |
|
477 | + 'task' => $taskName |
|
478 | + ]; |
|
479 | + |
|
480 | + return $this->updateAll($fields, $conditions); |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Cleanup/Delete Completed Tasks. |
|
485 | + * |
|
486 | + * @return void |
|
487 | + */ |
|
488 | + public function cleanOldJobs() |
|
489 | + { |
|
490 | + if (!Configure::read('Queue.cleanuptimeout')) { |
|
491 | + return; |
|
492 | + } |
|
493 | + |
|
494 | + $this->deleteAll([ |
|
495 | + 'completed <' => time() - (int)Configure::read('Queue.cleanuptimeout') |
|
496 | + ]); |
|
497 | + } |
|
498 | + |
|
499 | + /** |
|
500 | + * |
|
501 | + * @param \Queue\Model\Entity\QueuedTask $queuedTask Queued task |
|
502 | + * @param array $taskConfiguration Task configuration |
|
503 | + * @return string |
|
504 | + */ |
|
505 | + public function getFailedStatus($queuedTask, array $taskConfiguration) |
|
506 | + { |
|
507 | + $failureMessageRequeued = 'requeued'; |
|
508 | + |
|
509 | + $queuedTaskName = 'Queue' . $queuedTask->task; |
|
510 | + if (empty($taskConfiguration[$queuedTaskName])) { |
|
511 | + return $failureMessageRequeued; |
|
512 | + } |
|
513 | + $retries = $taskConfiguration[$queuedTaskName]['retries']; |
|
514 | + if ($queuedTask->failed_count <= $retries) { |
|
515 | + return $failureMessageRequeued; |
|
516 | + } |
|
517 | + |
|
518 | + return 'aborted'; |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Generates a unique Identifier for the current worker thread. |
|
523 | + * |
|
524 | + * Useful to identify the currently running processes for this thread. |
|
525 | + * |
|
526 | + * @return string Identifier |
|
527 | + */ |
|
528 | + public function key() |
|
529 | + { |
|
530 | + if ($this->_key !== null) { |
|
531 | + return $this->_key; |
|
532 | + } |
|
533 | + $this->_key = sha1(microtime()); |
|
534 | + if (!$this->_key) { |
|
535 | + throw new RuntimeException('Invalid key generated'); |
|
536 | + } |
|
537 | + |
|
538 | + return $this->_key; |
|
539 | + } |
|
540 | + |
|
541 | + /** |
|
542 | + * Resets worker Identifier |
|
543 | + * |
|
544 | + * @return void |
|
545 | + */ |
|
546 | + public function clearKey() |
|
547 | + { |
|
548 | + $this->_key = null; |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * truncate() |
|
553 | + * |
|
554 | + * @return void |
|
555 | + */ |
|
556 | + public function truncate() |
|
557 | + { |
|
558 | + $sql = $this->getSchema()->truncateSql($this->_connection); |
|
559 | + foreach ($sql as $snippet) { |
|
560 | + $this->_connection->execute($snippet); |
|
561 | + } |
|
562 | + } |
|
563 | + |
|
564 | + /** |
|
565 | + * get the name of the driver |
|
566 | + * |
|
567 | + * @return string |
|
568 | + */ |
|
569 | + protected function _getDriverName() |
|
570 | + { |
|
571 | + $className = explode('\\', $this->getConnection()->config()['driver']); |
|
572 | + $name = end($className); |
|
573 | + |
|
574 | + return $name; |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * |
|
579 | + * @param array $conditions Conditions |
|
580 | + * @param string $key Key |
|
581 | + * @param array $values Values |
|
582 | + * @return array |
|
583 | + */ |
|
584 | + protected function addFilter(array $conditions, $key, array $values) |
|
585 | + { |
|
586 | + $include = []; |
|
587 | + $exclude = []; |
|
588 | + foreach ($values as $value) { |
|
589 | + if (substr($value, 0, 1) === '-') { |
|
590 | + $exclude[] = substr($value, 1); |
|
591 | + } else { |
|
592 | + $include[] = $value; |
|
593 | + } |
|
594 | + } |
|
595 | + |
|
596 | + if ($include) { |
|
597 | + $conditions[$key . ' IN'] = $include; |
|
598 | + } |
|
599 | + if ($exclude) { |
|
600 | + $conditions[$key . ' NOT IN'] = $exclude; |
|
601 | + } |
|
602 | + |
|
603 | + return $conditions; |
|
604 | + } |
|
605 | + |
|
606 | + /** |
|
607 | + * Returns a DateTime object from different input. |
|
608 | + * |
|
609 | + * Without argument this will be "now". |
|
610 | + * |
|
611 | + * @param int|string|\Cake\I18n\FrozenTime|\Cake\I18n\Time|null $notBefore Not before time |
|
612 | + * |
|
613 | + * @return \Cake\I18n\FrozenTime|\Cake\I18n\Time |
|
614 | + */ |
|
615 | + protected function getDateTime($notBefore = null) |
|
616 | + { |
|
617 | + if (is_object($notBefore)) { |
|
618 | + return $notBefore; |
|
619 | + } |
|
620 | + |
|
621 | + return new FrozenTime($notBefore); |
|
622 | + } |
|
623 | 623 | } |