forked from dereuromark/cakephp-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.php
More file actions
130 lines (108 loc) · 3 KB
/
Copy pathConfig.php
File metadata and controls
130 lines (108 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Queue\Queue;
use Cake\Core\Configure;
use InvalidArgumentException;
class Config {
/**
* Timeout in seconds, after which the Task is reassigned to a new worker
* if not finished successfully.
* This should be high enough that it cannot still be running on a zombie worker (>> 2x).
*
* @return int
*/
public static function defaultworkertimeout() {
return Configure::read('Queue.defaultworkertimeout', 600); // 10min
}
/**
* Seconds of running time after which the worker will terminate (0 = unlimited)
*
* @return int
*/
public static function workermaxruntime() {
return Configure::read('Queue.workermaxruntime', 120);
}
/**
* Minimum number of seconds before a cleanup run will remove a completed task (set to 0 to disable)
*
* @return int
*/
public static function cleanuptimeout() {
return Configure::read('Queue.cleanuptimeout', 2592000); // 30 days
}
/**
* @return int
*/
public static function sleeptime() {
return Configure::read('Queue.sleeptime', 10);
}
/**
* @return int
*/
public static function gcprob() {
return Configure::read('Queue.gcprob', 10);
}
/**
* @return int
*/
public static function defaultworkerretries() {
return Configure::read('Queue.defaultworkerretries', 1);
}
/**
* @return int
*/
public static function maxworkers() {
return Configure::read('Queue.maxworkers', 1);
}
/**
* @return array<string>
*/
public static function ignoredTasks(): array {
$a = Configure::read('Queue.ignoredTasks', []);
if (!is_array($a)) {
throw new InvalidArgumentException('Queue.ignoredTasks is not an array');
}
return $a;
}
/**
* @param array<string> $tasks
*
* @throws \RuntimeException
* @return array<string, array<string, mixed>>
*/
public static function taskConfig(array $tasks): array {
$config = [];
foreach ($tasks as $task => $className) {
[$pluginName, $taskName] = pluginSplit($task);
/** @var \Queue\Queue\Task $taskObject */
$taskObject = new $className();
$config[$task]['class'] = $className;
$config[$task]['name'] = $taskName;
$config[$task]['plugin'] = $pluginName;
$config[$task]['timeout'] = $taskObject->timeout ?? static::defaultworkertimeout();
$config[$task]['retries'] = $taskObject->retries ?? static::defaultworkerretries();
$config[$task]['rate'] = $taskObject->rate;
$config[$task]['costs'] = $taskObject->costs;
$config[$task]['unique'] = $taskObject->unique;
unset($taskObject);
}
return $config;
}
/**
* @phpstan-param class-string<\Queue\Queue\Task>|string $class
*
* @param string $class
*
* @return string
*/
public static function taskName(string $class): string {
preg_match('#^(.+?)\\\\Queue\\\\Task\\\\(.+?)Task$#', $class, $matches);
if (!$matches) {
throw new InvalidArgumentException('Invalid class name: ' . $class);
}
$namespace = str_replace('\\', '/', $matches[1]);
if ($namespace === Configure::read('App.namespace')) {
return $matches[2];
}
return $namespace . '.' . $matches[2];
}
}