Skip to content

Commit ee804ec

Browse files
authored
[5.x] Adding new balance strategy: single (#1473)
* Adding new balance strategy: single * revert * tweak * Moving Behavior to Off / false
1 parent a85d66e commit ee804ec

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

src/AutoScaler.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,18 @@ protected function poolsByQueue(Supervisor $supervisor)
7777
protected function timeToClearPerQueue(Supervisor $supervisor, Collection $pools)
7878
{
7979
return $pools->mapWithKeys(function ($pool, $queue) use ($supervisor) {
80-
$size = $this->queue->connection($supervisor->options->connection)->readyNow($queue);
80+
$queues = collect(explode(',', $queue))->map(function ($_queue) use ($supervisor) {
81+
$size = $this->queue->connection($supervisor->options->connection)->readyNow($_queue);
82+
83+
return [
84+
'size' => $size,
85+
'time' => ($size * $this->metrics->runtimeForQueue($_queue)),
86+
];
87+
});
8188

8289
return [$queue => [
83-
'size' => $size,
84-
'time' => ($size * $this->metrics->runtimeForQueue($queue)),
90+
'size' => $queues->sum('size'),
91+
'time' => $queues->sum('time'),
8592
]];
8693
});
8794
}

src/SupervisorOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function balancing()
265265
*/
266266
public function autoScaling()
267267
{
268-
return $this->balance === 'auto';
268+
return $this->balance !== 'simple';
269269
}
270270

271271
/**

tests/Feature/SupervisorTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ public function test_supervisor_starts_multiple_pools_when_balancing()
9595
);
9696
}
9797

98+
public function test_supervisor_starts_pools_with_queues_when_balancing_is_off()
99+
{
100+
$options = $this->supervisorOptions();
101+
$options->queue = 'first,second';
102+
$this->supervisor = $supervisor = new Supervisor($options);
103+
104+
$supervisor->scale(2);
105+
$this->assertCount(2, $supervisor->processes());
106+
107+
$host = MasterSupervisor::name();
108+
109+
$this->assertSame(
110+
'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="first,second" --sleep=3 --timeout=60 --tries=0 --rest=0',
111+
$supervisor->processes()[0]->getCommandLine()
112+
);
113+
114+
$this->assertSame(
115+
'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="first,second" --sleep=3 --timeout=60 --tries=0 --rest=0',
116+
$supervisor->processes()[1]->getCommandLine()
117+
);
118+
}
119+
98120
public function test_recent_jobs_are_correctly_maintained()
99121
{
100122
$id = Queue::push(new Jobs\BasicJob);
@@ -154,6 +176,7 @@ public function test_exceptions_are_caught_and_handled_during_loop()
154176
public function test_supervisor_information_is_persisted()
155177
{
156178
$this->supervisor = $supervisor = new Supervisor($options = $this->supervisorOptions());
179+
$options->balance = 'simple';
157180
$options->queue = 'default,another';
158181

159182
$supervisor->scale(2);
@@ -184,7 +207,8 @@ public function test_supervisor_repository_returns_null_if_no_supervisor_exists_
184207

185208
public function test_processes_can_be_scaled_up()
186209
{
187-
$this->supervisor = $supervisor = new Supervisor($this->supervisorOptions());
210+
$this->supervisor = $supervisor = new Supervisor($options = $this->supervisorOptions());
211+
$options->balance = 'simple';
188212

189213
$supervisor->scale(2);
190214
$supervisor->loop();
@@ -198,6 +222,7 @@ public function test_processes_can_be_scaled_up()
198222
public function test_processes_can_be_scaled_down()
199223
{
200224
$this->supervisor = $supervisor = new Supervisor($options = $this->supervisorOptions());
225+
$options->balance = 'simple';
201226
$options->sleep = 0;
202227

203228
$supervisor->scale(3);
@@ -468,6 +493,7 @@ public function test_supervisor_processes_can_be_counted_externally()
468493
{
469494
SystemProcessCounter::$command = 'worker.php';
470495
$this->supervisor = $supervisor = new Supervisor($options = $this->supervisorOptions());
496+
$options->balance = 'simple';
471497

472498
$supervisor->scale(3);
473499
$supervisor->loop();
@@ -481,6 +507,7 @@ public function test_supervisor_does_not_start_workers_until_looped_and_active()
481507
{
482508
SystemProcessCounter::$command = 'worker.php';
483509
$this->supervisor = $supervisor = new Supervisor($options = $this->supervisorOptions());
510+
$options->balance = 'simple';
484511

485512
$supervisor->scale(3);
486513

0 commit comments

Comments
 (0)