Skip to content

Commit

Permalink
[5.x] Adding new balance strategy: single (#1473)
Browse files Browse the repository at this point in the history
* Adding new balance strategy: single

* revert

* tweak

* Moving Behavior to Off / false
  • Loading branch information
dbpolito authored Jul 22, 2024
1 parent a85d66e commit ee804ec
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/AutoScaler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,18 @@ protected function poolsByQueue(Supervisor $supervisor)
protected function timeToClearPerQueue(Supervisor $supervisor, Collection $pools)
{
return $pools->mapWithKeys(function ($pool, $queue) use ($supervisor) {
$size = $this->queue->connection($supervisor->options->connection)->readyNow($queue);
$queues = collect(explode(',', $queue))->map(function ($_queue) use ($supervisor) {
$size = $this->queue->connection($supervisor->options->connection)->readyNow($_queue);

return [
'size' => $size,
'time' => ($size * $this->metrics->runtimeForQueue($_queue)),
];
});

return [$queue => [
'size' => $size,
'time' => ($size * $this->metrics->runtimeForQueue($queue)),
'size' => $queues->sum('size'),
'time' => $queues->sum('time'),
]];
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/SupervisorOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function balancing()
*/
public function autoScaling()
{
return $this->balance === 'auto';
return $this->balance !== 'simple';
}

/**
Expand Down
29 changes: 28 additions & 1 deletion tests/Feature/SupervisorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ public function test_supervisor_starts_multiple_pools_when_balancing()
);
}

public function test_supervisor_starts_pools_with_queues_when_balancing_is_off()
{
$options = $this->supervisorOptions();
$options->queue = 'first,second';
$this->supervisor = $supervisor = new Supervisor($options);

$supervisor->scale(2);
$this->assertCount(2, $supervisor->processes());

$host = MasterSupervisor::name();

$this->assertSame(
'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',
$supervisor->processes()[0]->getCommandLine()
);

$this->assertSame(
'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',
$supervisor->processes()[1]->getCommandLine()
);
}

public function test_recent_jobs_are_correctly_maintained()
{
$id = Queue::push(new Jobs\BasicJob);
Expand Down Expand Up @@ -154,6 +176,7 @@ public function test_exceptions_are_caught_and_handled_during_loop()
public function test_supervisor_information_is_persisted()
{
$this->supervisor = $supervisor = new Supervisor($options = $this->supervisorOptions());
$options->balance = 'simple';
$options->queue = 'default,another';

$supervisor->scale(2);
Expand Down Expand Up @@ -184,7 +207,8 @@ public function test_supervisor_repository_returns_null_if_no_supervisor_exists_

public function test_processes_can_be_scaled_up()
{
$this->supervisor = $supervisor = new Supervisor($this->supervisorOptions());
$this->supervisor = $supervisor = new Supervisor($options = $this->supervisorOptions());
$options->balance = 'simple';

$supervisor->scale(2);
$supervisor->loop();
Expand All @@ -198,6 +222,7 @@ public function test_processes_can_be_scaled_up()
public function test_processes_can_be_scaled_down()
{
$this->supervisor = $supervisor = new Supervisor($options = $this->supervisorOptions());
$options->balance = 'simple';
$options->sleep = 0;

$supervisor->scale(3);
Expand Down Expand Up @@ -468,6 +493,7 @@ public function test_supervisor_processes_can_be_counted_externally()
{
SystemProcessCounter::$command = 'worker.php';
$this->supervisor = $supervisor = new Supervisor($options = $this->supervisorOptions());
$options->balance = 'simple';

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

$supervisor->scale(3);

Expand Down

0 comments on commit ee804ec

Please sign in to comment.