@@ -6,66 +6,66 @@ |
||
6 | 6 | class Config |
7 | 7 | { |
8 | 8 | |
9 | - /** |
|
10 | - * |
|
11 | - * @return int |
|
12 | - */ |
|
13 | - public static function defaultWorkerTimeout() |
|
14 | - { |
|
15 | - return Configure::read('Queue.defaultWorkerTimeout', 600); // 10min |
|
16 | - } |
|
9 | + /** |
|
10 | + * |
|
11 | + * @return int |
|
12 | + */ |
|
13 | + public static function defaultWorkerTimeout() |
|
14 | + { |
|
15 | + return Configure::read('Queue.defaultWorkerTimeout', 600); // 10min |
|
16 | + } |
|
17 | 17 | |
18 | - /** |
|
19 | - * |
|
20 | - * @return int |
|
21 | - */ |
|
22 | - public static function workerMaxRuntime() |
|
23 | - { |
|
24 | - return Configure::read('Queue.workerMaxRuntime', 120); |
|
25 | - } |
|
18 | + /** |
|
19 | + * |
|
20 | + * @return int |
|
21 | + */ |
|
22 | + public static function workerMaxRuntime() |
|
23 | + { |
|
24 | + return Configure::read('Queue.workerMaxRuntime', 120); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * |
|
29 | - * @return int |
|
30 | - */ |
|
31 | - public static function cleanupTimeout() |
|
32 | - { |
|
33 | - return Configure::read('Queue.cleanupTimeout', 2592000); // 30 days |
|
34 | - } |
|
27 | + /** |
|
28 | + * |
|
29 | + * @return int |
|
30 | + */ |
|
31 | + public static function cleanupTimeout() |
|
32 | + { |
|
33 | + return Configure::read('Queue.cleanupTimeout', 2592000); // 30 days |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * @return int |
|
39 | - */ |
|
40 | - public static function sleepTime() |
|
41 | - { |
|
42 | - return Configure::read('Queue.sleepTime', 10); |
|
43 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * @return int |
|
39 | + */ |
|
40 | + public static function sleepTime() |
|
41 | + { |
|
42 | + return Configure::read('Queue.sleepTime', 10); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * |
|
47 | - * @return int |
|
48 | - */ |
|
49 | - public static function gcprob() |
|
50 | - { |
|
51 | - return Configure::read('Queue.gcprob', 10); |
|
52 | - } |
|
45 | + /** |
|
46 | + * |
|
47 | + * @return int |
|
48 | + */ |
|
49 | + public static function gcprob() |
|
50 | + { |
|
51 | + return Configure::read('Queue.gcprob', 10); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * |
|
56 | - * @return int |
|
57 | - */ |
|
58 | - public static function defaultWorkerRetries() |
|
59 | - { |
|
60 | - return Configure::read('Queue.defaultWorkerRetries', 1); |
|
61 | - } |
|
54 | + /** |
|
55 | + * |
|
56 | + * @return int |
|
57 | + */ |
|
58 | + public static function defaultWorkerRetries() |
|
59 | + { |
|
60 | + return Configure::read('Queue.defaultWorkerRetries', 1); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * |
|
65 | - * @return int |
|
66 | - */ |
|
67 | - public static function maxWorkers() |
|
68 | - { |
|
69 | - return Configure::read('Queue.maxWorkers', 1); |
|
70 | - } |
|
63 | + /** |
|
64 | + * |
|
65 | + * @return int |
|
66 | + */ |
|
67 | + public static function maxWorkers() |
|
68 | + { |
|
69 | + return Configure::read('Queue.maxWorkers', 1); |
|
70 | + } |
|
71 | 71 | } |
@@ -10,12 +10,12 @@ |
||
10 | 10 | interface AddInterface |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * Allows adding a task to the queue. |
|
15 | - * |
|
16 | - * Will create one example task in the queue, which later will be executed using run(). |
|
17 | - * |
|
18 | - * @return void |
|
19 | - */ |
|
20 | - public function add(): void; |
|
13 | + /** |
|
14 | + * Allows adding a task to the queue. |
|
15 | + * |
|
16 | + * Will create one example task in the queue, which later will be executed using run(). |
|
17 | + * |
|
18 | + * @return void |
|
19 | + */ |
|
20 | + public function add(): void; |
|
21 | 21 | } |
@@ -20,21 +20,21 @@ |
||
20 | 20 | class QueuedTask extends Entity |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * |
|
25 | - * {@inheritdoc} |
|
26 | - * |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $_accessible = [ |
|
30 | - 'task' => true, |
|
31 | - 'data' => true, |
|
32 | - 'not_before' => true, |
|
33 | - 'fetched' => true, |
|
34 | - 'completed' => true, |
|
35 | - 'failed_count' => true, |
|
36 | - 'failure_message' => true, |
|
37 | - 'worker_key' => true, |
|
38 | - 'created' => true |
|
39 | - ]; |
|
23 | + /** |
|
24 | + * |
|
25 | + * {@inheritdoc} |
|
26 | + * |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $_accessible = [ |
|
30 | + 'task' => true, |
|
31 | + 'data' => true, |
|
32 | + 'not_before' => true, |
|
33 | + 'fetched' => true, |
|
34 | + 'completed' => true, |
|
35 | + 'failed_count' => true, |
|
36 | + 'failure_message' => true, |
|
37 | + 'worker_key' => true, |
|
38 | + 'created' => true |
|
39 | + ]; |
|
40 | 40 | } |
@@ -1,18 +1,18 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | return [ |
4 | - 'Queue' => [ |
|
5 | - // time (in seconds) after which a job is requeued if the worker doesn't report back |
|
6 | - 'defaultWorkerTimeout' => 1800, |
|
4 | + 'Queue' => [ |
|
5 | + // time (in seconds) after which a job is requeued if the worker doesn't report back |
|
6 | + 'defaultWorkerTimeout' => 1800, |
|
7 | 7 | |
8 | - // seconds of running time after which the worker will terminate (0 = unlimited) |
|
9 | - 'workerMaxRuntime' => 120, |
|
8 | + // seconds of running time after which the worker will terminate (0 = unlimited) |
|
9 | + 'workerMaxRuntime' => 120, |
|
10 | 10 | |
11 | - // minimum time (in seconds) which a task remains in the database before being cleaned up. |
|
12 | - 'cleanupTimeout' => 2592000, // 30 days |
|
11 | + // minimum time (in seconds) which a task remains in the database before being cleaned up. |
|
12 | + 'cleanupTimeout' => 2592000, // 30 days |
|
13 | 13 | |
14 | - /* Optional */ |
|
14 | + /* Optional */ |
|
15 | 15 | |
16 | - 'isSearchEnabled' => true, |
|
17 | - ], |
|
16 | + 'isSearchEnabled' => true, |
|
17 | + ], |
|
18 | 18 | ]; |
@@ -8,80 +8,80 @@ |
||
8 | 8 | class TaskFinder |
9 | 9 | { |
10 | 10 | |
11 | - /** |
|
12 | - * |
|
13 | - * @var array|null |
|
14 | - */ |
|
15 | - protected $tasks; |
|
11 | + /** |
|
12 | + * |
|
13 | + * @var array|null |
|
14 | + */ |
|
15 | + protected $tasks; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Returns all possible Queue tasks. |
|
19 | - * |
|
20 | - * Makes sure that app tasks are prioritized over plugin ones. |
|
21 | - * |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function allAppAndPluginTasks() |
|
25 | - { |
|
26 | - if ($this->tasks !== null) { |
|
27 | - return $this->tasks; |
|
28 | - } |
|
17 | + /** |
|
18 | + * Returns all possible Queue tasks. |
|
19 | + * |
|
20 | + * Makes sure that app tasks are prioritized over plugin ones. |
|
21 | + * |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function allAppAndPluginTasks() |
|
25 | + { |
|
26 | + if ($this->tasks !== null) { |
|
27 | + return $this->tasks; |
|
28 | + } |
|
29 | 29 | |
30 | - $paths = App::path('Shell/Task'); |
|
31 | - $this->tasks = []; |
|
30 | + $paths = App::path('Shell/Task'); |
|
31 | + $this->tasks = []; |
|
32 | 32 | |
33 | - foreach ($paths as $path) { |
|
34 | - $Folder = new Folder($path); |
|
35 | - $this->tasks = $this->getAppPaths($Folder); |
|
36 | - } |
|
37 | - $plugins = Plugin::loaded(); |
|
38 | - foreach ($plugins as $plugin) { |
|
39 | - $pluginPaths = App::path('Shell/Task', $plugin); |
|
40 | - foreach ($pluginPaths as $pluginPath) { |
|
41 | - $Folder = new Folder($pluginPath); |
|
42 | - $pluginTasks = $this->getPluginPaths($Folder, $plugin); |
|
43 | - $this->tasks = array_merge($this->tasks, $pluginTasks); |
|
44 | - } |
|
45 | - } |
|
33 | + foreach ($paths as $path) { |
|
34 | + $Folder = new Folder($path); |
|
35 | + $this->tasks = $this->getAppPaths($Folder); |
|
36 | + } |
|
37 | + $plugins = Plugin::loaded(); |
|
38 | + foreach ($plugins as $plugin) { |
|
39 | + $pluginPaths = App::path('Shell/Task', $plugin); |
|
40 | + foreach ($pluginPaths as $pluginPath) { |
|
41 | + $Folder = new Folder($pluginPath); |
|
42 | + $pluginTasks = $this->getPluginPaths($Folder, $plugin); |
|
43 | + $this->tasks = array_merge($this->tasks, $pluginTasks); |
|
44 | + } |
|
45 | + } |
|
46 | 46 | |
47 | - return $this->tasks; |
|
48 | - } |
|
47 | + return $this->tasks; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * |
|
52 | - * @param \Cake\Filesystem\Folder $Folder The directory |
|
53 | - * |
|
54 | - * @return array |
|
55 | - */ |
|
56 | - protected function getAppPaths(Folder $Folder) |
|
57 | - { |
|
58 | - $res = array_merge($this->tasks, $Folder->find('Queue.+\.php')); |
|
59 | - foreach ($res as &$r) { |
|
60 | - $r = basename($r, 'Task.php'); |
|
61 | - } |
|
50 | + /** |
|
51 | + * |
|
52 | + * @param \Cake\Filesystem\Folder $Folder The directory |
|
53 | + * |
|
54 | + * @return array |
|
55 | + */ |
|
56 | + protected function getAppPaths(Folder $Folder) |
|
57 | + { |
|
58 | + $res = array_merge($this->tasks, $Folder->find('Queue.+\.php')); |
|
59 | + foreach ($res as &$r) { |
|
60 | + $r = basename($r, 'Task.php'); |
|
61 | + } |
|
62 | 62 | |
63 | - return $res; |
|
64 | - } |
|
63 | + return $res; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * |
|
68 | - * @param \Cake\Filesystem\Folder $Folder The directory |
|
69 | - * @param string $plugin The plugin name |
|
70 | - * |
|
71 | - * @return array |
|
72 | - */ |
|
73 | - protected function getPluginPaths(Folder $Folder, $plugin) |
|
74 | - { |
|
75 | - $res = $Folder->find('Queue.+Task\.php'); |
|
76 | - foreach ($res as $key => $r) { |
|
77 | - $name = basename($r, 'Task.php'); |
|
78 | - if (in_array($name, $this->tasks)) { |
|
79 | - unset($res[$key]); |
|
80 | - continue; |
|
81 | - } |
|
82 | - $res[$key] = $plugin . '.' . $name; |
|
83 | - } |
|
66 | + /** |
|
67 | + * |
|
68 | + * @param \Cake\Filesystem\Folder $Folder The directory |
|
69 | + * @param string $plugin The plugin name |
|
70 | + * |
|
71 | + * @return array |
|
72 | + */ |
|
73 | + protected function getPluginPaths(Folder $Folder, $plugin) |
|
74 | + { |
|
75 | + $res = $Folder->find('Queue.+Task\.php'); |
|
76 | + foreach ($res as $key => $r) { |
|
77 | + $name = basename($r, 'Task.php'); |
|
78 | + if (in_array($name, $this->tasks)) { |
|
79 | + unset($res[$key]); |
|
80 | + continue; |
|
81 | + } |
|
82 | + $res[$key] = $plugin . '.' . $name; |
|
83 | + } |
|
84 | 84 | |
85 | - return $res; |
|
86 | - } |
|
85 | + return $res; |
|
86 | + } |
|
87 | 87 | } |
@@ -18,57 +18,57 @@ |
||
18 | 18 | abstract class QueueTask extends Shell implements QueueTaskInterface |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - public $queueModelClass = 'Queue.QueuedTasks'; |
|
21 | + /** |
|
22 | + * |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + public $queueModelClass = 'Queue.QueuedTasks'; |
|
26 | 26 | |
27 | - /** |
|
28 | - * |
|
29 | - * @var \Queue\Model\Table\QueuedTasksTable |
|
30 | - */ |
|
31 | - public $QueuedTasks; |
|
27 | + /** |
|
28 | + * |
|
29 | + * @var \Queue\Model\Table\QueuedTasksTable |
|
30 | + */ |
|
31 | + public $QueuedTasks; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Timeout for run, after which the Task is reassigned to a new worker. |
|
35 | - * |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - public $timeout = 120; |
|
33 | + /** |
|
34 | + * Timeout for run, after which the Task is reassigned to a new worker. |
|
35 | + * |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + public $timeout = 120; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Number of times a failed instance of this task should be restarted before giving up. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - public $retries = 1; |
|
40 | + /** |
|
41 | + * Number of times a failed instance of this task should be restarted before giving up. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + public $retries = 1; |
|
46 | 46 | |
47 | - /** |
|
48 | - * |
|
49 | - * @param \Cake\Console\ConsoleIo|null $io IO |
|
50 | - */ |
|
51 | - public function __construct(ConsoleIo $io = null) |
|
52 | - { |
|
53 | - parent::__construct($io); |
|
47 | + /** |
|
48 | + * |
|
49 | + * @param \Cake\Console\ConsoleIo|null $io IO |
|
50 | + */ |
|
51 | + public function __construct(ConsoleIo $io = null) |
|
52 | + { |
|
53 | + parent::__construct($io); |
|
54 | 54 | |
55 | - $this->loadModel($this->queueModelClass); |
|
56 | - } |
|
55 | + $this->loadModel($this->queueModelClass); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * |
|
60 | - * @return string |
|
61 | - * @throws \InvalidArgumentException |
|
62 | - */ |
|
63 | - protected function queueTaskName() |
|
64 | - { |
|
65 | - $class = get_class($this); |
|
58 | + /** |
|
59 | + * |
|
60 | + * @return string |
|
61 | + * @throws \InvalidArgumentException |
|
62 | + */ |
|
63 | + protected function queueTaskName() |
|
64 | + { |
|
65 | + $class = get_class($this); |
|
66 | 66 | |
67 | - preg_match('#\\\\Queue(.+)Task$#', $class, $matches); |
|
68 | - if (!$matches) { |
|
69 | - throw new InvalidArgumentException('Invalid class name: ' . $class); |
|
70 | - } |
|
67 | + preg_match('#\\\\Queue(.+)Task$#', $class, $matches); |
|
68 | + if (!$matches) { |
|
69 | + throw new InvalidArgumentException('Invalid class name: ' . $class); |
|
70 | + } |
|
71 | 71 | |
72 | - return $matches[1]; |
|
73 | - } |
|
72 | + return $matches[1]; |
|
73 | + } |
|
74 | 74 | } |
@@ -7,82 +7,82 @@ |
||
7 | 7 | class QueueExampleTask extends QueueTask implements AddInterface |
8 | 8 | { |
9 | 9 | |
10 | - /** |
|
11 | - * Timeout for run, after which the task is reassigned to a new worker. |
|
12 | - * |
|
13 | - * @var int |
|
14 | - */ |
|
15 | - public $timeout = 10; |
|
10 | + /** |
|
11 | + * Timeout for run, after which the task is reassigned to a new worker. |
|
12 | + * |
|
13 | + * @var int |
|
14 | + */ |
|
15 | + public $timeout = 10; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Timeout for cleanup, after which completed jobs are deleted (in seconds). |
|
19 | - * |
|
20 | - * @var int |
|
21 | - */ |
|
22 | - public $cleanupTimeout = 600; |
|
17 | + /** |
|
18 | + * Timeout for cleanup, after which completed jobs are deleted (in seconds). |
|
19 | + * |
|
20 | + * @var int |
|
21 | + */ |
|
22 | + public $cleanupTimeout = 600; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Number of times a failed instance of this task should be restarted before giving up. |
|
26 | - * |
|
27 | - * @var int |
|
28 | - */ |
|
29 | - public $retries = 0; |
|
24 | + /** |
|
25 | + * Number of times a failed instance of this task should be restarted before giving up. |
|
26 | + * |
|
27 | + * @var int |
|
28 | + */ |
|
29 | + public $retries = 0; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Stores any failure messages triggered during run(). |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - public $failureMessage = ''; |
|
31 | + /** |
|
32 | + * Stores any failure messages triggered during run(). |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + public $failureMessage = ''; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Example add functionality. |
|
40 | - * Will create one example job in the queue, which later will be executed using run(); |
|
41 | - * |
|
42 | - * To invoke from CLI execute: |
|
43 | - * - bin/cake queue add Example |
|
44 | - * |
|
45 | - * @return void |
|
46 | - */ |
|
47 | - public function add(): void |
|
48 | - { |
|
49 | - $this->out(__d('queue', 'CakePHP Queue Example task.')); |
|
50 | - $this->hr(); |
|
51 | - $this->out(__d('queue', 'This is a very simple example of a queueTask.')); |
|
52 | - $this->out(__d('queue', 'Now adding an example Task Job into the Queue.')); |
|
53 | - $this->out(__d('queue', 'This task will only produce some console output on the worker that it runs on.')); |
|
54 | - $this->out(' '); |
|
55 | - $this->out(__d('queue', 'To run a Worker use:')); |
|
56 | - $this->out(__d('queue', ' cake queue runworker')); |
|
57 | - $this->out(' '); |
|
58 | - $this->out(__d('queue', 'You can find the sourcecode of this task in: ')); |
|
59 | - $this->out(__FILE__); |
|
60 | - $this->out(' '); |
|
38 | + /** |
|
39 | + * Example add functionality. |
|
40 | + * Will create one example job in the queue, which later will be executed using run(); |
|
41 | + * |
|
42 | + * To invoke from CLI execute: |
|
43 | + * - bin/cake queue add Example |
|
44 | + * |
|
45 | + * @return void |
|
46 | + */ |
|
47 | + public function add(): void |
|
48 | + { |
|
49 | + $this->out(__d('queue', 'CakePHP Queue Example task.')); |
|
50 | + $this->hr(); |
|
51 | + $this->out(__d('queue', 'This is a very simple example of a queueTask.')); |
|
52 | + $this->out(__d('queue', 'Now adding an example Task Job into the Queue.')); |
|
53 | + $this->out(__d('queue', 'This task will only produce some console output on the worker that it runs on.')); |
|
54 | + $this->out(' '); |
|
55 | + $this->out(__d('queue', 'To run a Worker use:')); |
|
56 | + $this->out(__d('queue', ' cake queue runworker')); |
|
57 | + $this->out(' '); |
|
58 | + $this->out(__d('queue', 'You can find the sourcecode of this task in: ')); |
|
59 | + $this->out(__FILE__); |
|
60 | + $this->out(' '); |
|
61 | 61 | |
62 | - // Adding a task of type 'example' with no additionally passed data |
|
63 | - if ($this->QueuedTasks->createJob('Example')) { |
|
64 | - $this->out(__d('queue', 'OK, job created, now run the worker')); |
|
65 | - } else { |
|
66 | - $this->err(__d('queue', 'Could not create Job')); |
|
67 | - } |
|
68 | - } |
|
62 | + // Adding a task of type 'example' with no additionally passed data |
|
63 | + if ($this->QueuedTasks->createJob('Example')) { |
|
64 | + $this->out(__d('queue', 'OK, job created, now run the worker')); |
|
65 | + } else { |
|
66 | + $this->err(__d('queue', 'Could not create Job')); |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Example run function. |
|
72 | - * This function is executed, when a worker is executing a task. |
|
73 | - * The return parameter will determine, if the task will be marked completed, or be requeued. |
|
74 | - * |
|
75 | - * @param array $data The array passed to QueuedTasksTable::createJob() |
|
76 | - * @param int $taskId The id of the QueuedTask entity |
|
77 | - * @return void |
|
78 | - */ |
|
79 | - public function run(array $data, $taskId): void |
|
80 | - { |
|
81 | - $this->hr(); |
|
82 | - $this->out(__d('queue', 'CakePHP Queue Example task.')); |
|
83 | - $this->hr(); |
|
84 | - $this->out(__d('queue', ' ->Success, the Example Task was run.<-')); |
|
85 | - $this->out(' '); |
|
86 | - $this->out(' '); |
|
87 | - } |
|
70 | + /** |
|
71 | + * Example run function. |
|
72 | + * This function is executed, when a worker is executing a task. |
|
73 | + * The return parameter will determine, if the task will be marked completed, or be requeued. |
|
74 | + * |
|
75 | + * @param array $data The array passed to QueuedTasksTable::createJob() |
|
76 | + * @param int $taskId The id of the QueuedTask entity |
|
77 | + * @return void |
|
78 | + */ |
|
79 | + public function run(array $data, $taskId): void |
|
80 | + { |
|
81 | + $this->hr(); |
|
82 | + $this->out(__d('queue', 'CakePHP Queue Example task.')); |
|
83 | + $this->hr(); |
|
84 | + $this->out(__d('queue', ' ->Success, the Example Task was run.<-')); |
|
85 | + $this->out(' '); |
|
86 | + $this->out(' '); |
|
87 | + } |
|
88 | 88 | } |
@@ -13,12 +13,12 @@ |
||
13 | 13 | interface QueueTaskInterface |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * Main execution of the task. |
|
18 | - * |
|
19 | - * @param array $data The array passed to QueuedTasksTable::createJob() |
|
20 | - * @param int $taskId The id of the QueuedTask entity |
|
21 | - * @return void |
|
22 | - */ |
|
23 | - public function run(array $data, $taskId): void; |
|
16 | + /** |
|
17 | + * Main execution of the task. |
|
18 | + * |
|
19 | + * @param array $data The array passed to QueuedTasksTable::createJob() |
|
20 | + * @param int $taskId The id of the QueuedTask entity |
|
21 | + * @return void |
|
22 | + */ |
|
23 | + public function run(array $data, $taskId): void; |
|
24 | 24 | } |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | { |
190 | 190 | $driverName = $this->_getDriverName(); |
191 | 191 | $options = [ |
192 | - 'fields' => function (Query $query) use ($driverName) { |
|
192 | + 'fields' => function(Query $query) use ($driverName) { |
|
193 | 193 | $alltime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(created)'); |
194 | 194 | $runtime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
195 | 195 | $fetchdelay = $query->func()->avg('UNIX_TIMESTAMP(fetched) - IF(not_before is NULL, UNIX_TIMESTAMP(created), UNIX_TIMESTAMP(not_before))'); |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | public function getFullStats($taskName = null) |
237 | 237 | { |
238 | 238 | $driverName = $this->_getDriverName(); |
239 | - $fields = function (Query $query) use ($driverName) { |
|
239 | + $fields = function(Query $query) use ($driverName) { |
|
240 | 240 | $runtime = $query->newExpr('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
241 | 241 | switch ($driverName) { |
242 | 242 | case static::DRIVER_SQLSERVER: |
@@ -370,7 +370,7 @@ discard block |
||
370 | 370 | } |
371 | 371 | |
372 | 372 | /** @var \Queue\Model\Entity\QueuedTask|null $task */ |
373 | - $task = $this->getConnection()->transactional(function () use ($query, $options, $now) { |
|
373 | + $task = $this->getConnection()->transactional(function() use ($query, $options, $now) { |
|
374 | 374 | $task = $query->find('all', $options) |
375 | 375 | ->enableAutoFields(true) |
376 | 376 | ->epilog('FOR UPDATE') |
@@ -28,536 +28,536 @@ |
||
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 | - * Initialize method |
|
41 | - * |
|
42 | - * @param array $config The configuration for the Table. |
|
43 | - * @return void |
|
44 | - */ |
|
45 | - public function initialize(array $config) |
|
46 | - { |
|
47 | - parent::initialize($config); |
|
48 | - |
|
49 | - $this->setTable('queued_tasks'); |
|
50 | - $this->setDisplayField('id'); |
|
51 | - $this->setPrimaryKey('id'); |
|
52 | - |
|
53 | - $this->addBehavior('Timestamp'); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * set connection name |
|
58 | - * |
|
59 | - * @return string |
|
60 | - */ |
|
61 | - public static function defaultConnectionName() |
|
62 | - { |
|
63 | - $connection = Configure::read('Queue.connection'); |
|
64 | - if (!empty($connection)) { |
|
65 | - return $connection; |
|
66 | - } |
|
67 | - |
|
68 | - return parent::defaultConnectionName(); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * |
|
73 | - * @param \Cake\Event\Event $event Model event |
|
74 | - * @param \ArrayObject $data The data |
|
75 | - * @param \ArrayObject $options The options |
|
76 | - * @return void |
|
77 | - */ |
|
78 | - public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) |
|
79 | - { |
|
80 | - if (isset($data['data']) && $data['data'] === '') { |
|
81 | - $data['data'] = null; |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Adds a new job to the queue. |
|
87 | - * |
|
88 | - * @param string $taskName Task name |
|
89 | - * @param array|null $data Array of data |
|
90 | - * @param string $notBefore A datetime which indicates when the job may be executed |
|
91 | - * @return \Queue\Model\Entity\QueuedTask Saved job entity |
|
92 | - */ |
|
93 | - public function createJob($taskName, array $data = null, string $notBefore = null) |
|
94 | - { |
|
95 | - $task = [ |
|
96 | - 'task' => $taskName, |
|
97 | - 'data' => serialize($data), |
|
98 | - 'not_before' => $this->getDateTime() |
|
99 | - ]; |
|
100 | - |
|
101 | - if (!empty($notBefore)) { |
|
102 | - $task['not_before'] = $this->getDateTime(strtotime($notBefore)); |
|
103 | - } |
|
104 | - |
|
105 | - $queuedTask = $this->newEntity($task); |
|
106 | - |
|
107 | - return $this->saveOrFail($queuedTask); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Returns the number of items in the queue. |
|
112 | - * Either returns the number of ALL pending jobs, or the number of pending jobs of the passed type. |
|
113 | - * |
|
114 | - * @param string|null $taskName Task type to Count |
|
115 | - * @return int |
|
116 | - */ |
|
117 | - public function getLength($taskName = null) |
|
118 | - { |
|
119 | - $findConf = [ |
|
120 | - 'conditions' => [ |
|
121 | - 'completed IS' => null |
|
122 | - ] |
|
123 | - ]; |
|
124 | - if ($taskName !== null) { |
|
125 | - $findConf['conditions']['task'] = $taskName; |
|
126 | - } |
|
127 | - |
|
128 | - return $this->find('all', $findConf)->count(); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Return a list of all task types in the Queue. |
|
133 | - * |
|
134 | - * @return \Cake\ORM\Query |
|
135 | - */ |
|
136 | - public function getTypes() |
|
137 | - { |
|
138 | - $findCond = [ |
|
139 | - 'fields' => [ |
|
140 | - 'task' |
|
141 | - ], |
|
142 | - 'group' => [ |
|
143 | - 'task' |
|
144 | - ], |
|
145 | - 'keyField' => 'task', |
|
146 | - 'valueField' => 'task' |
|
147 | - ]; |
|
148 | - |
|
149 | - return $this->find('list', $findCond); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Return some statistics about finished jobs still in the Database. |
|
154 | - * TO-DO: rewrite as virtual field |
|
155 | - * |
|
156 | - * @return \Cake\ORM\Query |
|
157 | - */ |
|
158 | - public function getStats() |
|
159 | - { |
|
160 | - $driverName = $this->_getDriverName(); |
|
161 | - $options = [ |
|
162 | - 'fields' => function (Query $query) use ($driverName) { |
|
163 | - $alltime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(created)'); |
|
164 | - $runtime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
165 | - $fetchdelay = $query->func()->avg('UNIX_TIMESTAMP(fetched) - IF(not_before is NULL, UNIX_TIMESTAMP(created), UNIX_TIMESTAMP(not_before))'); |
|
166 | - switch ($driverName) { |
|
167 | - case static::DRIVER_SQLSERVER: |
|
168 | - $alltime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', created)"); |
|
169 | - $runtime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
170 | - $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)"); |
|
171 | - break; |
|
172 | - } |
|
173 | - /** |
|
174 | - * |
|
175 | - * @var \Cake\ORM\Query |
|
176 | - */ |
|
177 | - return [ |
|
178 | - 'task', |
|
179 | - 'num' => $query->func()->count('*'), |
|
180 | - 'alltime' => $alltime, |
|
181 | - 'runtime' => $runtime, |
|
182 | - 'fetchdelay' => $fetchdelay |
|
183 | - ]; |
|
184 | - }, |
|
185 | - 'conditions' => [ |
|
186 | - 'completed IS NOT' => null |
|
187 | - ], |
|
188 | - 'group' => [ |
|
189 | - 'task' |
|
190 | - ] |
|
191 | - ]; |
|
192 | - |
|
193 | - return $this->find('all', $options); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Returns [ |
|
198 | - * 'Task' => [ |
|
199 | - * 'YYYY-MM-DD' => INT, |
|
200 | - * ... |
|
201 | - * ] |
|
202 | - * ] |
|
203 | - * |
|
204 | - * @param string|null $taskName The task name |
|
205 | - * @return array |
|
206 | - */ |
|
207 | - public function getFullStats($taskName = null) |
|
208 | - { |
|
209 | - $driverName = $this->_getDriverName(); |
|
210 | - $fields = function (Query $query) use ($driverName) { |
|
211 | - $runtime = $query->newExpr('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
212 | - switch ($driverName) { |
|
213 | - case static::DRIVER_SQLSERVER: |
|
214 | - $runtime = $query->newExpr("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
215 | - break; |
|
216 | - } |
|
217 | - |
|
218 | - return [ |
|
219 | - 'task', |
|
220 | - 'created', |
|
221 | - 'duration' => $runtime |
|
222 | - ]; |
|
223 | - }; |
|
224 | - |
|
225 | - $conditions = [ |
|
226 | - 'completed IS NOT' => null |
|
227 | - ]; |
|
228 | - if ($taskName) { |
|
229 | - $conditions['task'] = $taskName; |
|
230 | - } |
|
231 | - |
|
232 | - $tasks = $this->find() |
|
233 | - ->select($fields) |
|
234 | - ->where($conditions) |
|
235 | - ->enableHydration(false) |
|
236 | - ->orderDesc('id') |
|
237 | - ->limit(static::STATS_LIMIT) |
|
238 | - ->all() |
|
239 | - ->toArray(); |
|
240 | - |
|
241 | - $result = []; |
|
242 | - |
|
243 | - $days = []; |
|
244 | - |
|
245 | - foreach ($tasks as $task) { |
|
246 | - /** @var \DateTime $created */ |
|
247 | - $created = $task['created']; |
|
248 | - $day = $created->format('Y-m-d'); |
|
249 | - if (!isset($days[$day])) { |
|
250 | - $days[$day] = $day; |
|
251 | - } |
|
252 | - |
|
253 | - $result[$task['task']][$day][] = $task['duration']; |
|
254 | - } |
|
255 | - |
|
256 | - foreach ($result as $type => $tasks) { |
|
257 | - foreach ($tasks as $day => $durations) { |
|
258 | - $average = array_sum($durations) / count($durations); |
|
259 | - $result[$type][$day] = (int)$average; |
|
260 | - } |
|
261 | - |
|
262 | - foreach ($days as $day) { |
|
263 | - if (isset($result[$type][$day])) { |
|
264 | - continue; |
|
265 | - } |
|
266 | - |
|
267 | - $result[$type][$day] = 0; |
|
268 | - } |
|
269 | - |
|
270 | - ksort($result[$type]); |
|
271 | - } |
|
272 | - |
|
273 | - return $result; |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Look for a new job that can be processed with the current abilities and |
|
278 | - * from the specified group (or any if null). |
|
279 | - * |
|
280 | - * @param array $capabilities Available QueueWorkerTasks. |
|
281 | - * @param array $types Request a job from these types (or exclude certain types), or any otherwise. |
|
282 | - * @return \Queue\Model\Entity\QueuedTask|null |
|
283 | - */ |
|
284 | - public function requestJob(array $capabilities, array $types = []) |
|
285 | - { |
|
286 | - $now = $this->getDateTime(); |
|
287 | - $nowStr = $now->toDateTimeString(); |
|
288 | - $driverName = $this->_getDriverName(); |
|
289 | - |
|
290 | - $query = $this->find(); |
|
291 | - $age = $query->newExpr()->add('IFNULL(TIMESTAMPDIFF(SECOND, "' . $nowStr . '", not_before), 0)'); |
|
292 | - switch ($driverName) { |
|
293 | - case static::DRIVER_SQLSERVER: |
|
294 | - $age = $query->newExpr()->add('ISNULL(DATEDIFF(SECOND, GETDATE(), not_before), 0)'); |
|
295 | - break; |
|
296 | - case static::DRIVER_POSTGRES: |
|
297 | - $age = $query->newExpr()->add('COALESCE((EXTRACT(EPOCH FROM now()) - EXTRACT(EPOCH FROM not_before)), 0)'); |
|
298 | - break; |
|
299 | - } |
|
300 | - $options = [ |
|
301 | - 'conditions' => [ |
|
302 | - 'completed IS' => null, |
|
303 | - 'OR' => [] |
|
304 | - ], |
|
305 | - 'fields' => [ |
|
306 | - 'age' => $age |
|
307 | - ], |
|
308 | - 'order' => [ |
|
309 | - 'age' => 'ASC', |
|
310 | - 'id' => 'ASC' |
|
311 | - ] |
|
312 | - ]; |
|
313 | - |
|
314 | - if ($types) { |
|
315 | - $options['conditions'] = $this->addFilter($options['conditions'], 'task', $types); |
|
316 | - } |
|
317 | - |
|
318 | - // Generate the task specific conditions. |
|
319 | - foreach ($capabilities as $task) { |
|
320 | - list ($plugin, $name) = pluginSplit($task['name']); |
|
321 | - $timeoutAt = $now->copy(); |
|
322 | - $tmp = [ |
|
323 | - 'task' => $name, |
|
324 | - 'AND' => [ |
|
325 | - [ |
|
326 | - 'OR' => [ |
|
327 | - 'not_before <=' => $nowStr, |
|
328 | - 'not_before IS' => null |
|
329 | - ] |
|
330 | - ], |
|
331 | - [ |
|
332 | - 'OR' => [ |
|
333 | - 'fetched <' => $timeoutAt->subSeconds($task['timeout']), |
|
334 | - 'fetched IS' => null |
|
335 | - ] |
|
336 | - ] |
|
337 | - ], |
|
338 | - 'failed_count <' => ($task['retries'] + 1) |
|
339 | - ]; |
|
340 | - $options['conditions']['OR'][] = $tmp; |
|
341 | - } |
|
342 | - |
|
343 | - /** @var \Queue\Model\Entity\QueuedTask|null $task */ |
|
344 | - $task = $this->getConnection()->transactional(function () use ($query, $options, $now) { |
|
345 | - $task = $query->find('all', $options) |
|
346 | - ->enableAutoFields(true) |
|
347 | - ->epilog('FOR UPDATE') |
|
348 | - ->first(); |
|
349 | - |
|
350 | - if (!$task) { |
|
351 | - return null; |
|
352 | - } |
|
353 | - |
|
354 | - $key = sha1(microtime()); |
|
355 | - $task = $this->patchEntity($task, [ |
|
356 | - 'worker_key' => $key, |
|
357 | - 'fetched' => $now |
|
358 | - ]); |
|
359 | - |
|
360 | - return $this->saveOrFail($task); |
|
361 | - }); |
|
362 | - |
|
363 | - if (!$task) { |
|
364 | - return null; |
|
365 | - } |
|
366 | - |
|
367 | - return $task; |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * Mark a task as Completed, removing it from the queue. |
|
372 | - * |
|
373 | - * @param \Queue\Model\Entity\QueuedTask $task Task |
|
374 | - * @return bool Success |
|
375 | - */ |
|
376 | - public function markJobDone(QueuedTask $task) |
|
377 | - { |
|
378 | - $fields = [ |
|
379 | - 'completed' => $this->getDateTime() |
|
380 | - ]; |
|
381 | - $task = $this->patchEntity($task, $fields); |
|
382 | - |
|
383 | - return (bool)$this->save($task); |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * Mark a job as Failed, incrementing the failed-counter and Requeueing it. |
|
388 | - * |
|
389 | - * @param \Queue\Model\Entity\QueuedTask $task Task |
|
390 | - * @param string|null $failureMessage Optional message to append to the failure_message field. |
|
391 | - * @return bool Success |
|
392 | - */ |
|
393 | - public function markJobFailed(QueuedTask $task, $failureMessage = null) |
|
394 | - { |
|
395 | - $fields = [ |
|
396 | - 'failed_count' => $task->failed_count + 1, |
|
397 | - 'failure_message' => $failureMessage |
|
398 | - ]; |
|
399 | - $task = $this->patchEntity($task, $fields); |
|
400 | - |
|
401 | - return (bool)$this->save($task); |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * Reset current jobs |
|
406 | - * |
|
407 | - * @param int|null $id ID |
|
408 | - * |
|
409 | - * @return int Success |
|
410 | - */ |
|
411 | - public function reset($id = null) |
|
412 | - { |
|
413 | - $fields = [ |
|
414 | - 'completed' => null, |
|
415 | - 'fetched' => null, |
|
416 | - 'failed_count' => 0, |
|
417 | - 'worker_key' => null, |
|
418 | - 'failure_message' => null |
|
419 | - ]; |
|
420 | - $conditions = [ |
|
421 | - 'completed IS' => null |
|
422 | - ]; |
|
423 | - if ($id) { |
|
424 | - $conditions['id'] = $id; |
|
425 | - } |
|
426 | - |
|
427 | - return $this->updateAll($fields, $conditions); |
|
428 | - } |
|
429 | - |
|
430 | - /** |
|
431 | - * |
|
432 | - * @param string $taskName Task name |
|
433 | - * |
|
434 | - * @return int |
|
435 | - */ |
|
436 | - public function rerun($taskName) |
|
437 | - { |
|
438 | - $fields = [ |
|
439 | - 'completed' => null, |
|
440 | - 'fetched' => null, |
|
441 | - 'failed_count' => 0, |
|
442 | - 'worker_key' => null, |
|
443 | - 'failure_message' => null |
|
444 | - ]; |
|
445 | - $conditions = [ |
|
446 | - 'completed IS NOT' => null, |
|
447 | - 'task' => $taskName |
|
448 | - ]; |
|
449 | - |
|
450 | - return $this->updateAll($fields, $conditions); |
|
451 | - } |
|
452 | - |
|
453 | - /** |
|
454 | - * Cleanup/Delete Completed Tasks. |
|
455 | - * |
|
456 | - * @return void |
|
457 | - */ |
|
458 | - public function cleanOldJobs() |
|
459 | - { |
|
460 | - if (!Configure::read('Queue.cleanuptimeout')) { |
|
461 | - return; |
|
462 | - } |
|
463 | - |
|
464 | - $this->deleteAll([ |
|
465 | - 'completed <' => time() - (int)Configure::read('Queue.cleanuptimeout') |
|
466 | - ]); |
|
467 | - } |
|
468 | - |
|
469 | - /** |
|
470 | - * |
|
471 | - * @param \Queue\Model\Entity\QueuedTask $queuedTask Queued task |
|
472 | - * @param array $taskConfiguration Task configuration |
|
473 | - * @return string |
|
474 | - */ |
|
475 | - public function getFailedStatus($queuedTask, array $taskConfiguration) |
|
476 | - { |
|
477 | - $failureMessageRequeued = 'requeued'; |
|
478 | - |
|
479 | - $queuedTaskName = 'Queue' . $queuedTask->task; |
|
480 | - if (empty($taskConfiguration[$queuedTaskName])) { |
|
481 | - return $failureMessageRequeued; |
|
482 | - } |
|
483 | - $retries = $taskConfiguration[$queuedTaskName]['retries']; |
|
484 | - if ($queuedTask->failed_count <= $retries) { |
|
485 | - return $failureMessageRequeued; |
|
486 | - } |
|
487 | - |
|
488 | - return 'aborted'; |
|
489 | - } |
|
490 | - |
|
491 | - /** |
|
492 | - * truncate() |
|
493 | - * |
|
494 | - * @return void |
|
495 | - */ |
|
496 | - public function truncate() |
|
497 | - { |
|
498 | - $sql = $this->getSchema()->truncateSql($this->_connection); |
|
499 | - foreach ($sql as $snippet) { |
|
500 | - $this->_connection->execute($snippet); |
|
501 | - } |
|
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * get the name of the driver |
|
506 | - * |
|
507 | - * @return string |
|
508 | - */ |
|
509 | - protected function _getDriverName() |
|
510 | - { |
|
511 | - $className = explode('\\', $this->getConnection()->config()['driver']); |
|
512 | - $name = end($className); |
|
513 | - |
|
514 | - return $name; |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * |
|
519 | - * @param array $conditions Conditions |
|
520 | - * @param string $key Key |
|
521 | - * @param array $values Values |
|
522 | - * @return array |
|
523 | - */ |
|
524 | - protected function addFilter(array $conditions, $key, array $values) |
|
525 | - { |
|
526 | - $include = []; |
|
527 | - $exclude = []; |
|
528 | - foreach ($values as $value) { |
|
529 | - if (substr($value, 0, 1) === '-') { |
|
530 | - $exclude[] = substr($value, 1); |
|
531 | - } else { |
|
532 | - $include[] = $value; |
|
533 | - } |
|
534 | - } |
|
535 | - |
|
536 | - if ($include) { |
|
537 | - $conditions[$key . ' IN'] = $include; |
|
538 | - } |
|
539 | - if ($exclude) { |
|
540 | - $conditions[$key . ' NOT IN'] = $exclude; |
|
541 | - } |
|
542 | - |
|
543 | - return $conditions; |
|
544 | - } |
|
545 | - |
|
546 | - /** |
|
547 | - * Returns a DateTime object from different input. |
|
548 | - * |
|
549 | - * Without argument this will be "now". |
|
550 | - * |
|
551 | - * @param int|string|\Cake\I18n\FrozenTime|\Cake\I18n\Time|null $notBefore Not before time |
|
552 | - * |
|
553 | - * @return \Cake\I18n\FrozenTime|\Cake\I18n\Time |
|
554 | - */ |
|
555 | - protected function getDateTime($notBefore = null) |
|
556 | - { |
|
557 | - if (is_object($notBefore)) { |
|
558 | - return $notBefore; |
|
559 | - } |
|
560 | - |
|
561 | - return new FrozenTime($notBefore); |
|
562 | - } |
|
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 | + * Initialize method |
|
41 | + * |
|
42 | + * @param array $config The configuration for the Table. |
|
43 | + * @return void |
|
44 | + */ |
|
45 | + public function initialize(array $config) |
|
46 | + { |
|
47 | + parent::initialize($config); |
|
48 | + |
|
49 | + $this->setTable('queued_tasks'); |
|
50 | + $this->setDisplayField('id'); |
|
51 | + $this->setPrimaryKey('id'); |
|
52 | + |
|
53 | + $this->addBehavior('Timestamp'); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * set connection name |
|
58 | + * |
|
59 | + * @return string |
|
60 | + */ |
|
61 | + public static function defaultConnectionName() |
|
62 | + { |
|
63 | + $connection = Configure::read('Queue.connection'); |
|
64 | + if (!empty($connection)) { |
|
65 | + return $connection; |
|
66 | + } |
|
67 | + |
|
68 | + return parent::defaultConnectionName(); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * |
|
73 | + * @param \Cake\Event\Event $event Model event |
|
74 | + * @param \ArrayObject $data The data |
|
75 | + * @param \ArrayObject $options The options |
|
76 | + * @return void |
|
77 | + */ |
|
78 | + public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) |
|
79 | + { |
|
80 | + if (isset($data['data']) && $data['data'] === '') { |
|
81 | + $data['data'] = null; |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Adds a new job to the queue. |
|
87 | + * |
|
88 | + * @param string $taskName Task name |
|
89 | + * @param array|null $data Array of data |
|
90 | + * @param string $notBefore A datetime which indicates when the job may be executed |
|
91 | + * @return \Queue\Model\Entity\QueuedTask Saved job entity |
|
92 | + */ |
|
93 | + public function createJob($taskName, array $data = null, string $notBefore = null) |
|
94 | + { |
|
95 | + $task = [ |
|
96 | + 'task' => $taskName, |
|
97 | + 'data' => serialize($data), |
|
98 | + 'not_before' => $this->getDateTime() |
|
99 | + ]; |
|
100 | + |
|
101 | + if (!empty($notBefore)) { |
|
102 | + $task['not_before'] = $this->getDateTime(strtotime($notBefore)); |
|
103 | + } |
|
104 | + |
|
105 | + $queuedTask = $this->newEntity($task); |
|
106 | + |
|
107 | + return $this->saveOrFail($queuedTask); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Returns the number of items in the queue. |
|
112 | + * Either returns the number of ALL pending jobs, or the number of pending jobs of the passed type. |
|
113 | + * |
|
114 | + * @param string|null $taskName Task type to Count |
|
115 | + * @return int |
|
116 | + */ |
|
117 | + public function getLength($taskName = null) |
|
118 | + { |
|
119 | + $findConf = [ |
|
120 | + 'conditions' => [ |
|
121 | + 'completed IS' => null |
|
122 | + ] |
|
123 | + ]; |
|
124 | + if ($taskName !== null) { |
|
125 | + $findConf['conditions']['task'] = $taskName; |
|
126 | + } |
|
127 | + |
|
128 | + return $this->find('all', $findConf)->count(); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Return a list of all task types in the Queue. |
|
133 | + * |
|
134 | + * @return \Cake\ORM\Query |
|
135 | + */ |
|
136 | + public function getTypes() |
|
137 | + { |
|
138 | + $findCond = [ |
|
139 | + 'fields' => [ |
|
140 | + 'task' |
|
141 | + ], |
|
142 | + 'group' => [ |
|
143 | + 'task' |
|
144 | + ], |
|
145 | + 'keyField' => 'task', |
|
146 | + 'valueField' => 'task' |
|
147 | + ]; |
|
148 | + |
|
149 | + return $this->find('list', $findCond); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Return some statistics about finished jobs still in the Database. |
|
154 | + * TO-DO: rewrite as virtual field |
|
155 | + * |
|
156 | + * @return \Cake\ORM\Query |
|
157 | + */ |
|
158 | + public function getStats() |
|
159 | + { |
|
160 | + $driverName = $this->_getDriverName(); |
|
161 | + $options = [ |
|
162 | + 'fields' => function (Query $query) use ($driverName) { |
|
163 | + $alltime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(created)'); |
|
164 | + $runtime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
165 | + $fetchdelay = $query->func()->avg('UNIX_TIMESTAMP(fetched) - IF(not_before is NULL, UNIX_TIMESTAMP(created), UNIX_TIMESTAMP(not_before))'); |
|
166 | + switch ($driverName) { |
|
167 | + case static::DRIVER_SQLSERVER: |
|
168 | + $alltime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', created)"); |
|
169 | + $runtime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
170 | + $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)"); |
|
171 | + break; |
|
172 | + } |
|
173 | + /** |
|
174 | + * |
|
175 | + * @var \Cake\ORM\Query |
|
176 | + */ |
|
177 | + return [ |
|
178 | + 'task', |
|
179 | + 'num' => $query->func()->count('*'), |
|
180 | + 'alltime' => $alltime, |
|
181 | + 'runtime' => $runtime, |
|
182 | + 'fetchdelay' => $fetchdelay |
|
183 | + ]; |
|
184 | + }, |
|
185 | + 'conditions' => [ |
|
186 | + 'completed IS NOT' => null |
|
187 | + ], |
|
188 | + 'group' => [ |
|
189 | + 'task' |
|
190 | + ] |
|
191 | + ]; |
|
192 | + |
|
193 | + return $this->find('all', $options); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Returns [ |
|
198 | + * 'Task' => [ |
|
199 | + * 'YYYY-MM-DD' => INT, |
|
200 | + * ... |
|
201 | + * ] |
|
202 | + * ] |
|
203 | + * |
|
204 | + * @param string|null $taskName The task name |
|
205 | + * @return array |
|
206 | + */ |
|
207 | + public function getFullStats($taskName = null) |
|
208 | + { |
|
209 | + $driverName = $this->_getDriverName(); |
|
210 | + $fields = function (Query $query) use ($driverName) { |
|
211 | + $runtime = $query->newExpr('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
212 | + switch ($driverName) { |
|
213 | + case static::DRIVER_SQLSERVER: |
|
214 | + $runtime = $query->newExpr("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
215 | + break; |
|
216 | + } |
|
217 | + |
|
218 | + return [ |
|
219 | + 'task', |
|
220 | + 'created', |
|
221 | + 'duration' => $runtime |
|
222 | + ]; |
|
223 | + }; |
|
224 | + |
|
225 | + $conditions = [ |
|
226 | + 'completed IS NOT' => null |
|
227 | + ]; |
|
228 | + if ($taskName) { |
|
229 | + $conditions['task'] = $taskName; |
|
230 | + } |
|
231 | + |
|
232 | + $tasks = $this->find() |
|
233 | + ->select($fields) |
|
234 | + ->where($conditions) |
|
235 | + ->enableHydration(false) |
|
236 | + ->orderDesc('id') |
|
237 | + ->limit(static::STATS_LIMIT) |
|
238 | + ->all() |
|
239 | + ->toArray(); |
|
240 | + |
|
241 | + $result = []; |
|
242 | + |
|
243 | + $days = []; |
|
244 | + |
|
245 | + foreach ($tasks as $task) { |
|
246 | + /** @var \DateTime $created */ |
|
247 | + $created = $task['created']; |
|
248 | + $day = $created->format('Y-m-d'); |
|
249 | + if (!isset($days[$day])) { |
|
250 | + $days[$day] = $day; |
|
251 | + } |
|
252 | + |
|
253 | + $result[$task['task']][$day][] = $task['duration']; |
|
254 | + } |
|
255 | + |
|
256 | + foreach ($result as $type => $tasks) { |
|
257 | + foreach ($tasks as $day => $durations) { |
|
258 | + $average = array_sum($durations) / count($durations); |
|
259 | + $result[$type][$day] = (int)$average; |
|
260 | + } |
|
261 | + |
|
262 | + foreach ($days as $day) { |
|
263 | + if (isset($result[$type][$day])) { |
|
264 | + continue; |
|
265 | + } |
|
266 | + |
|
267 | + $result[$type][$day] = 0; |
|
268 | + } |
|
269 | + |
|
270 | + ksort($result[$type]); |
|
271 | + } |
|
272 | + |
|
273 | + return $result; |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Look for a new job that can be processed with the current abilities and |
|
278 | + * from the specified group (or any if null). |
|
279 | + * |
|
280 | + * @param array $capabilities Available QueueWorkerTasks. |
|
281 | + * @param array $types Request a job from these types (or exclude certain types), or any otherwise. |
|
282 | + * @return \Queue\Model\Entity\QueuedTask|null |
|
283 | + */ |
|
284 | + public function requestJob(array $capabilities, array $types = []) |
|
285 | + { |
|
286 | + $now = $this->getDateTime(); |
|
287 | + $nowStr = $now->toDateTimeString(); |
|
288 | + $driverName = $this->_getDriverName(); |
|
289 | + |
|
290 | + $query = $this->find(); |
|
291 | + $age = $query->newExpr()->add('IFNULL(TIMESTAMPDIFF(SECOND, "' . $nowStr . '", not_before), 0)'); |
|
292 | + switch ($driverName) { |
|
293 | + case static::DRIVER_SQLSERVER: |
|
294 | + $age = $query->newExpr()->add('ISNULL(DATEDIFF(SECOND, GETDATE(), not_before), 0)'); |
|
295 | + break; |
|
296 | + case static::DRIVER_POSTGRES: |
|
297 | + $age = $query->newExpr()->add('COALESCE((EXTRACT(EPOCH FROM now()) - EXTRACT(EPOCH FROM not_before)), 0)'); |
|
298 | + break; |
|
299 | + } |
|
300 | + $options = [ |
|
301 | + 'conditions' => [ |
|
302 | + 'completed IS' => null, |
|
303 | + 'OR' => [] |
|
304 | + ], |
|
305 | + 'fields' => [ |
|
306 | + 'age' => $age |
|
307 | + ], |
|
308 | + 'order' => [ |
|
309 | + 'age' => 'ASC', |
|
310 | + 'id' => 'ASC' |
|
311 | + ] |
|
312 | + ]; |
|
313 | + |
|
314 | + if ($types) { |
|
315 | + $options['conditions'] = $this->addFilter($options['conditions'], 'task', $types); |
|
316 | + } |
|
317 | + |
|
318 | + // Generate the task specific conditions. |
|
319 | + foreach ($capabilities as $task) { |
|
320 | + list ($plugin, $name) = pluginSplit($task['name']); |
|
321 | + $timeoutAt = $now->copy(); |
|
322 | + $tmp = [ |
|
323 | + 'task' => $name, |
|
324 | + 'AND' => [ |
|
325 | + [ |
|
326 | + 'OR' => [ |
|
327 | + 'not_before <=' => $nowStr, |
|
328 | + 'not_before IS' => null |
|
329 | + ] |
|
330 | + ], |
|
331 | + [ |
|
332 | + 'OR' => [ |
|
333 | + 'fetched <' => $timeoutAt->subSeconds($task['timeout']), |
|
334 | + 'fetched IS' => null |
|
335 | + ] |
|
336 | + ] |
|
337 | + ], |
|
338 | + 'failed_count <' => ($task['retries'] + 1) |
|
339 | + ]; |
|
340 | + $options['conditions']['OR'][] = $tmp; |
|
341 | + } |
|
342 | + |
|
343 | + /** @var \Queue\Model\Entity\QueuedTask|null $task */ |
|
344 | + $task = $this->getConnection()->transactional(function () use ($query, $options, $now) { |
|
345 | + $task = $query->find('all', $options) |
|
346 | + ->enableAutoFields(true) |
|
347 | + ->epilog('FOR UPDATE') |
|
348 | + ->first(); |
|
349 | + |
|
350 | + if (!$task) { |
|
351 | + return null; |
|
352 | + } |
|
353 | + |
|
354 | + $key = sha1(microtime()); |
|
355 | + $task = $this->patchEntity($task, [ |
|
356 | + 'worker_key' => $key, |
|
357 | + 'fetched' => $now |
|
358 | + ]); |
|
359 | + |
|
360 | + return $this->saveOrFail($task); |
|
361 | + }); |
|
362 | + |
|
363 | + if (!$task) { |
|
364 | + return null; |
|
365 | + } |
|
366 | + |
|
367 | + return $task; |
|
368 | + } |
|
369 | + |
|
370 | + /** |
|
371 | + * Mark a task as Completed, removing it from the queue. |
|
372 | + * |
|
373 | + * @param \Queue\Model\Entity\QueuedTask $task Task |
|
374 | + * @return bool Success |
|
375 | + */ |
|
376 | + public function markJobDone(QueuedTask $task) |
|
377 | + { |
|
378 | + $fields = [ |
|
379 | + 'completed' => $this->getDateTime() |
|
380 | + ]; |
|
381 | + $task = $this->patchEntity($task, $fields); |
|
382 | + |
|
383 | + return (bool)$this->save($task); |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * Mark a job as Failed, incrementing the failed-counter and Requeueing it. |
|
388 | + * |
|
389 | + * @param \Queue\Model\Entity\QueuedTask $task Task |
|
390 | + * @param string|null $failureMessage Optional message to append to the failure_message field. |
|
391 | + * @return bool Success |
|
392 | + */ |
|
393 | + public function markJobFailed(QueuedTask $task, $failureMessage = null) |
|
394 | + { |
|
395 | + $fields = [ |
|
396 | + 'failed_count' => $task->failed_count + 1, |
|
397 | + 'failure_message' => $failureMessage |
|
398 | + ]; |
|
399 | + $task = $this->patchEntity($task, $fields); |
|
400 | + |
|
401 | + return (bool)$this->save($task); |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * Reset current jobs |
|
406 | + * |
|
407 | + * @param int|null $id ID |
|
408 | + * |
|
409 | + * @return int Success |
|
410 | + */ |
|
411 | + public function reset($id = null) |
|
412 | + { |
|
413 | + $fields = [ |
|
414 | + 'completed' => null, |
|
415 | + 'fetched' => null, |
|
416 | + 'failed_count' => 0, |
|
417 | + 'worker_key' => null, |
|
418 | + 'failure_message' => null |
|
419 | + ]; |
|
420 | + $conditions = [ |
|
421 | + 'completed IS' => null |
|
422 | + ]; |
|
423 | + if ($id) { |
|
424 | + $conditions['id'] = $id; |
|
425 | + } |
|
426 | + |
|
427 | + return $this->updateAll($fields, $conditions); |
|
428 | + } |
|
429 | + |
|
430 | + /** |
|
431 | + * |
|
432 | + * @param string $taskName Task name |
|
433 | + * |
|
434 | + * @return int |
|
435 | + */ |
|
436 | + public function rerun($taskName) |
|
437 | + { |
|
438 | + $fields = [ |
|
439 | + 'completed' => null, |
|
440 | + 'fetched' => null, |
|
441 | + 'failed_count' => 0, |
|
442 | + 'worker_key' => null, |
|
443 | + 'failure_message' => null |
|
444 | + ]; |
|
445 | + $conditions = [ |
|
446 | + 'completed IS NOT' => null, |
|
447 | + 'task' => $taskName |
|
448 | + ]; |
|
449 | + |
|
450 | + return $this->updateAll($fields, $conditions); |
|
451 | + } |
|
452 | + |
|
453 | + /** |
|
454 | + * Cleanup/Delete Completed Tasks. |
|
455 | + * |
|
456 | + * @return void |
|
457 | + */ |
|
458 | + public function cleanOldJobs() |
|
459 | + { |
|
460 | + if (!Configure::read('Queue.cleanuptimeout')) { |
|
461 | + return; |
|
462 | + } |
|
463 | + |
|
464 | + $this->deleteAll([ |
|
465 | + 'completed <' => time() - (int)Configure::read('Queue.cleanuptimeout') |
|
466 | + ]); |
|
467 | + } |
|
468 | + |
|
469 | + /** |
|
470 | + * |
|
471 | + * @param \Queue\Model\Entity\QueuedTask $queuedTask Queued task |
|
472 | + * @param array $taskConfiguration Task configuration |
|
473 | + * @return string |
|
474 | + */ |
|
475 | + public function getFailedStatus($queuedTask, array $taskConfiguration) |
|
476 | + { |
|
477 | + $failureMessageRequeued = 'requeued'; |
|
478 | + |
|
479 | + $queuedTaskName = 'Queue' . $queuedTask->task; |
|
480 | + if (empty($taskConfiguration[$queuedTaskName])) { |
|
481 | + return $failureMessageRequeued; |
|
482 | + } |
|
483 | + $retries = $taskConfiguration[$queuedTaskName]['retries']; |
|
484 | + if ($queuedTask->failed_count <= $retries) { |
|
485 | + return $failureMessageRequeued; |
|
486 | + } |
|
487 | + |
|
488 | + return 'aborted'; |
|
489 | + } |
|
490 | + |
|
491 | + /** |
|
492 | + * truncate() |
|
493 | + * |
|
494 | + * @return void |
|
495 | + */ |
|
496 | + public function truncate() |
|
497 | + { |
|
498 | + $sql = $this->getSchema()->truncateSql($this->_connection); |
|
499 | + foreach ($sql as $snippet) { |
|
500 | + $this->_connection->execute($snippet); |
|
501 | + } |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * get the name of the driver |
|
506 | + * |
|
507 | + * @return string |
|
508 | + */ |
|
509 | + protected function _getDriverName() |
|
510 | + { |
|
511 | + $className = explode('\\', $this->getConnection()->config()['driver']); |
|
512 | + $name = end($className); |
|
513 | + |
|
514 | + return $name; |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * |
|
519 | + * @param array $conditions Conditions |
|
520 | + * @param string $key Key |
|
521 | + * @param array $values Values |
|
522 | + * @return array |
|
523 | + */ |
|
524 | + protected function addFilter(array $conditions, $key, array $values) |
|
525 | + { |
|
526 | + $include = []; |
|
527 | + $exclude = []; |
|
528 | + foreach ($values as $value) { |
|
529 | + if (substr($value, 0, 1) === '-') { |
|
530 | + $exclude[] = substr($value, 1); |
|
531 | + } else { |
|
532 | + $include[] = $value; |
|
533 | + } |
|
534 | + } |
|
535 | + |
|
536 | + if ($include) { |
|
537 | + $conditions[$key . ' IN'] = $include; |
|
538 | + } |
|
539 | + if ($exclude) { |
|
540 | + $conditions[$key . ' NOT IN'] = $exclude; |
|
541 | + } |
|
542 | + |
|
543 | + return $conditions; |
|
544 | + } |
|
545 | + |
|
546 | + /** |
|
547 | + * Returns a DateTime object from different input. |
|
548 | + * |
|
549 | + * Without argument this will be "now". |
|
550 | + * |
|
551 | + * @param int|string|\Cake\I18n\FrozenTime|\Cake\I18n\Time|null $notBefore Not before time |
|
552 | + * |
|
553 | + * @return \Cake\I18n\FrozenTime|\Cake\I18n\Time |
|
554 | + */ |
|
555 | + protected function getDateTime($notBefore = null) |
|
556 | + { |
|
557 | + if (is_object($notBefore)) { |
|
558 | + return $notBefore; |
|
559 | + } |
|
560 | + |
|
561 | + return new FrozenTime($notBefore); |
|
562 | + } |
|
563 | 563 | } |