forked from laravel/horizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoScaler.php
150 lines (132 loc) · 4.7 KB
/
AutoScaler.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
<?php
namespace Laravel\Horizon;
use Illuminate\Contracts\Queue\Factory as QueueFactory;
use Illuminate\Support\Collection;
use Laravel\Horizon\Contracts\MetricsRepository;
class AutoScaler
{
/**
* The queue factory implementation.
*
* @var \Illuminate\Contracts\Queue\Factory
*/
public $queue;
/**
* The metrics repository implementation.
*
* @var \Laravel\Horizon\Contracts\MetricsRepository
*/
public $metrics;
/**
* Create a new auto-scaler instance.
*
* @param \Illuminate\Contracts\Queue\Factory $queue
* @param \Laravel\Horizon\Contracts\MetricsRepository $metrics
* @return void
*/
public function __construct(QueueFactory $queue, MetricsRepository $metrics)
{
$this->queue = $queue;
$this->metrics = $metrics;
}
/**
* Balance the workers on the given supervisor.
*
* @param \Laravel\Horizon\Supervisor $supervisor
* @return void
*/
public function scale(Supervisor $supervisor)
{
$pools = $this->poolsByQueue($supervisor);
$workers = $this->numberOfWorkersPerQueue(
$supervisor, $this->timeToClearPerQueue($supervisor, $pools)
);
$workers->each(function ($workers, $queue) use ($supervisor, $pools) {
$this->scalePool($supervisor, $pools[$queue], $workers);
});
}
/**
* Get the process pools keyed by their queue name.
*
* @param \Laravel\Horizon\Supervisor $supervisor
* @return \Illuminate\Support\Collection
*/
protected function poolsByQueue(Supervisor $supervisor)
{
return $supervisor->processPools->mapWithKeys(function ($pool) {
return [$pool->queue() => $pool];
});
}
/**
* Get the times in milliseconds needed to clear the queues.
*
* @param \Laravel\Horizon\Supervisor $supervisor
* @param \Illuminate\Support\Collection $pools
* @return \Illuminate\Support\Collection
*/
protected function timeToClearPerQueue(Supervisor $supervisor, Collection $pools)
{
return $pools->mapWithKeys(function ($pool, $queue) use ($supervisor) {
$size = $this->queue->connection($supervisor->options->connection)->readyNow($queue);
return [$queue => [
'size' => $size,
'time' => ($size * $this->metrics->runtimeForQueue($queue)),
]];
});
}
/**
* Get the number of workers needed per queue for proper balance.
*
* @param \Laravel\Horizon\Supervisor $supervisor
* @param \Illuminate\Support\Collection $queues
* @return \Illuminate\Support\Collection
*/
protected function numberOfWorkersPerQueue(Supervisor $supervisor, Collection $queues)
{
$timeToClearAll = $queues->sum('time');
return $queues->mapWithKeys(function ($timeToClear, $queue) use ($supervisor, $timeToClearAll) {
if ($timeToClearAll > 0 &&
$supervisor->options->autoScaling()) {
return [$queue => (($timeToClear['time'] / $timeToClearAll) * $supervisor->options->maxProcesses)];
} elseif ($timeToClearAll == 0 &&
$supervisor->options->autoScaling()) {
return [
$queue => $timeToClear['size']
? $supervisor->options->maxProcesses
: $supervisor->options->minProcesses,
];
}
return [$queue => $supervisor->options->maxProcesses / count($supervisor->processPools)];
})->sort();
}
/**
* Scale the given pool to the recommended number of workers.
*
* @param \Laravel\Horizon\Supervisor $supervisor
* @param \Laravel\Horizon\ProcessPool $pool
* @param float $workers
* @return void
*/
protected function scalePool(Supervisor $supervisor, $pool, $workers)
{
$supervisor->pruneTerminatingProcesses();
$poolProcesses = $pool->totalProcessCount();
if (ceil($workers) > $poolProcesses &&
$this->wouldNotExceedMaxProcesses($supervisor)) {
$pool->scale($poolProcesses + 1);
} elseif (ceil($workers) < $poolProcesses &&
$poolProcesses > $supervisor->options->minProcesses) {
$pool->scale($poolProcesses - 1);
}
}
/**
* Determine if adding another process would exceed max process limit.
*
* @param \Laravel\Horizon\Supervisor $supervisor
* @return bool
*/
protected function wouldNotExceedMaxProcesses(Supervisor $supervisor)
{
return ($supervisor->totalProcessCount() + 1) <= $supervisor->options->maxProcesses;
}
}