Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Adding scaling to balance = false #1473

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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