Skip to content

Commit 7686a8e

Browse files
authored
Set AutoScaler logic to target process scaling based on queue size wh… (#1552)
* Set AutoScaler logic to target process scaling based on queue size when balancing is disabled. * Simplify scaling test * Fix still using maxProcesses when runtime > 0
1 parent fdb6b06 commit 7686a8e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/AutoScaler.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ protected function numberOfWorkersPerQueue(Supervisor $supervisor, Collection $q
106106
$totalJobs = $queues->sum('size');
107107

108108
return $queues->mapWithKeys(function ($timeToClear, $queue) use ($supervisor, $timeToClearAll, $totalJobs) {
109+
if (! $supervisor->options->balancing()) {
110+
$targetProcesses = min(
111+
$supervisor->options->maxProcesses,
112+
max($supervisor->options->minProcesses, $timeToClear['size'])
113+
);
114+
115+
return [$queue => $targetProcesses];
116+
}
117+
109118
if ($timeToClearAll > 0 &&
110119
$supervisor->options->autoScaling()) {
111120
$numberOfProcesses = $supervisor->options->autoScaleByNumberOfJobs()

tests/Feature/AutoScalerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ public function test_scaler_will_not_scale_below_minimum_worker_threshold()
148148
$this->assertSame(2, $supervisor->processPools['second']->totalProcessCount());
149149
}
150150

151+
/**
152+
* @return array{0: AutoScaler, 1: Supervisor}
153+
*/
151154
protected function with_scaling_scenario($maxProcesses, array $pools, array $extraOptions = [])
152155
{
153156
// Mock dependencies...
@@ -258,4 +261,31 @@ public function test_scaler_assigns_more_processes_to_queue_with_more_jobs_when_
258261
$this->assertSame(52, $supervisor->processPools['first']->totalProcessCount());
259262
$this->assertSame(48, $supervisor->processPools['second']->totalProcessCount());
260263
}
264+
265+
public function test_scaler_works_with_a_single_process_pool()
266+
{
267+
[$scaler, $supervisor] = $this->with_scaling_scenario(10, [
268+
'default' => ['current' => 10, 'size' => 1, 'runtime' => 0],
269+
], ['balance' => false]);
270+
271+
$scaler->scale($supervisor);
272+
273+
$this->assertSame(9, $supervisor->processPools['default']->totalProcessCount());
274+
275+
[$scaler, $supervisor] = $this->with_scaling_scenario(10, [
276+
'default' => ['current' => 10, 'size' => 1, 'runtime' => 1000],
277+
], ['balance' => false]);
278+
279+
$scaler->scale($supervisor);
280+
281+
$this->assertSame(9, $supervisor->processPools['default']->totalProcessCount());
282+
283+
[$scaler, $supervisor] = $this->with_scaling_scenario(10, [
284+
'default' => ['current' => 5, 'size' => 11, 'runtime' => 1000],
285+
], ['balance' => false]);
286+
287+
$scaler->scale($supervisor);
288+
289+
$this->assertSame(6, $supervisor->processPools['default']->totalProcessCount());
290+
}
261291
}

0 commit comments

Comments
 (0)