forked from laravel/horizon
/
SupervisorOptions.php
317 lines (287 loc) · 7.38 KB
/
SupervisorOptions.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
namespace Laravel\Horizon;
class SupervisorOptions
{
/**
* The name of the supervisor.
*
* @var string
*/
public $name;
/**
* The name of the workers.
*
* @var string
*/
public $workersName;
/**
* The queue connection that should be utilized.
*
* @var string
*/
public $connection;
/**
* The queue that should be utilized.
*
* @var string
*/
public $queue;
/**
* Indicates the balancing strategy the supervisor should use.
*
* @var bool
*/
public $balance = 'off';
/**
* The maximum number of total processes to start when auto-scaling.
*
* @var int
*/
public $maxProcesses = 1;
/**
* The minimum number of processes to assign per working when auto-scaling.
*
* @var int
*/
public $minProcesses = 1;
/**
* The parent process identifier.
*
* @var int
*/
public $parentId = 0;
/**
* The process priority.
*
* @var int
*/
public $nice = 0;
/**
* The working directories that new workers should be started from.
*
* @var string
*/
public $directory;
/**
* The number of seconds to wait in between auto-scaling attempts.
*
* @var int
*/
public $balanceCooldown = 3;
/**
* The maximum number of processes to increase or decrease per one scaling.
*
* @var int
*/
public $balanceMaxShift = 1;
/**
* The number of seconds to wait before retrying a job that encountered an uncaught exception.
*
* @var string
*/
public $backoff;
/**
* The maximum number of jobs to run.
*
* @var int
*/
public $maxJobs;
/**
* The maximum number of seconds a worker may live.
*
* @var int
*/
public $maxTime;
/**
* The maximum amount of RAM the worker may consume.
*
* @var int
*/
public $memory;
/**
* The maximum number of seconds a child worker may run.
*
* @var int
*/
public $timeout;
/**
* The number of seconds to wait in between polling the queue.
*
* @var int
*/
public $sleep;
/**
* The maximum amount of times a job may be attempted.
*
* @var int
*/
public $maxTries;
/**
* Indicates if the worker should run in maintenance mode.
*
* @var bool
*/
public $force;
/**
* Create a new worker options instance.
*
* @param string $name
* @param string $connection
* @param string $queue
* @param string $workersName
* @param string $balance
* @param int $backoff
* @param int $maxTime
* @param int $maxJobs
* @param int $maxProcesses
* @param int $minProcesses
* @param int $memory
* @param int $timeout
* @param int $sleep
* @param int $maxTries
* @param bool $force
* @param int $nice
* @param int $balanceCooldown
* @param int $balanceMaxShift
* @param int $parentId
*/
public function __construct($name,
$connection,
$queue = null,
$workersName = 'default',
$balance = 'off',
$backoff = 0,
$maxTime = 0,
$maxJobs = 0,
$maxProcesses = 1,
$minProcesses = 1,
$memory = 128,
$timeout = 60,
$sleep = 3,
$maxTries = 0,
$force = false,
$nice = 0,
$balanceCooldown = 3,
$balanceMaxShift = 1,
$parentId = 0)
{
$this->name = $name;
$this->connection = $connection;
$this->queue = $queue ?: config('queue.connections.'.$connection.'.queue');
$this->workersName = $workersName;
$this->balance = $balance;
$this->backoff = $backoff;
$this->maxTime = $maxTime;
$this->maxJobs = $maxJobs;
$this->maxProcesses = $maxProcesses;
$this->minProcesses = $minProcesses;
$this->memory = $memory;
$this->timeout = $timeout;
$this->sleep = $sleep;
$this->maxTries = $maxTries;
$this->force = $force;
$this->nice = $nice;
$this->balanceCooldown = $balanceCooldown;
$this->balanceMaxShift = $balanceMaxShift;
$this->parentId = 0;
}
/**
* Create a fresh options instance with the given queue.
*
* @param string $queue
* @return static
*/
public function withQueue($queue)
{
return tap(clone $this, function ($options) use ($queue) {
$options->queue = $queue;
});
}
/**
* Determine if a balancing strategy should be used.
*
* @return bool
*/
public function balancing()
{
return in_array($this->balance, ['simple', 'auto']);
}
/**
* Determine if auto-scaling should be applied.
*
* @return bool
*/
public function autoScaling()
{
return $this->balance === 'auto';
}
/**
* Get the command-line representation of the options for a supervisor.
*
* @return string
*/
public function toSupervisorCommand()
{
return SupervisorCommandString::fromOptions($this);
}
/**
* Get the command-line representation of the options for a worker.
*
* @return string
*/
public function toWorkerCommand()
{
return WorkerCommandString::fromOptions($this);
}
/**
* Convert the options to a JSON string.
*
* @return string
*/
public function toJson()
{
return json_encode($this->toArray());
}
/**
* Convert the options to a raw array.
*
* @return array
*/
public function toArray()
{
return [
'balance' => $this->balance,
'connection' => $this->connection,
'queue' => $this->queue,
'backoff' => $this->backoff,
'force' => $this->force,
'maxProcesses' => $this->maxProcesses,
'minProcesses' => $this->minProcesses,
'maxTries' => $this->maxTries,
'maxTime' => $this->maxTime,
'maxJobs' => $this->maxJobs,
'memory' => $this->memory,
'nice' => $this->nice,
'name' => $this->name,
'workersName' => $this->workersName,
'sleep' => $this->sleep,
'timeout' => $this->timeout,
'balanceCooldown' => $this->balanceCooldown,
'balanceMaxShift' => $this->balanceMaxShift,
];
}
/**
* Create a new options instance from the given array.
*
* @param array $array
* @return static
*/
public static function fromArray(array $array)
{
return tap(new static($array['name'], $array['connection']), function ($options) use ($array) {
foreach ($array as $key => $value) {
$options->{$key} = $value;
}
});
}
}