From 4b77e54efd4846197c501373d5d377ba0b95b00d Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Fri, 6 Dec 2019 11:34:15 +0200 Subject: [PATCH 1/2] scale to the maxProcesses if timeToClearAll is zero and there are jobs waiting --- src/AutoScaler.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/AutoScaler.php b/src/AutoScaler.php index 557ab5f4..9e72cbeb 100644 --- a/src/AutoScaler.php +++ b/src/AutoScaler.php @@ -79,7 +79,10 @@ 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 * $this->metrics->runtimeForQueue($queue))]; + return [$queue => [ + 'size' => $size, + 'time' => ($size * $this->metrics->runtimeForQueue($queue)) + ]]; }); } @@ -87,20 +90,23 @@ protected function timeToClearPerQueue(Supervisor $supervisor, Collection $pools * Get the number of workers needed per queue for proper balance. * * @param \Laravel\Horizon\Supervisor $supervisor - * @param \Illuminate\Support\Collection $timeToClear + * @param \Illuminate\Support\Collection $queues * @return \Illuminate\Support\Collection */ - protected function numberOfWorkersPerQueue(Supervisor $supervisor, Collection $timeToClear) + protected function numberOfWorkersPerQueue(Supervisor $supervisor, Collection $queues) { - $timeToClearAll = $timeToClear->sum(); + $timeToClearAll = $queues->sum('time'); - return $timeToClear->mapWithKeys(function ($timeToClear, $queue) use ($supervisor, $timeToClearAll) { + return $queues->mapWithKeys(function ($timeToClear, $queue) use ($supervisor, $timeToClearAll) { if ($timeToClearAll > 0 && $supervisor->options->autoScaling()) { - return [$queue => (($timeToClear / $timeToClearAll) * $supervisor->options->maxProcesses)]; + return [$queue => (($timeToClear['time'] / $timeToClearAll) * $supervisor->options->maxProcesses)]; } elseif ($timeToClearAll == 0 && $supervisor->options->autoScaling()) { - return [$queue => $supervisor->options->minProcesses]; + return [ + $queue => $timeToClear['size'] ? + $supervisor->options->maxProcesses : $supervisor->options->minProcesses + ]; } return [$queue => $supervisor->options->maxProcesses / count($supervisor->processPools)]; From 24a05e663aeb073cdc1154e1cafe46f060f0af12 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Fri, 6 Dec 2019 11:37:58 +0200 Subject: [PATCH 2/2] fix style --- src/AutoScaler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AutoScaler.php b/src/AutoScaler.php index 9e72cbeb..7d79b6d8 100644 --- a/src/AutoScaler.php +++ b/src/AutoScaler.php @@ -81,7 +81,7 @@ protected function timeToClearPerQueue(Supervisor $supervisor, Collection $pools return [$queue => [ 'size' => $size, - 'time' => ($size * $this->metrics->runtimeForQueue($queue)) + 'time' => ($size * $this->metrics->runtimeForQueue($queue)), ]]; }); } @@ -105,7 +105,7 @@ protected function numberOfWorkersPerQueue(Supervisor $supervisor, Collection $q $supervisor->options->autoScaling()) { return [ $queue => $timeToClear['size'] ? - $supervisor->options->maxProcesses : $supervisor->options->minProcesses + $supervisor->options->maxProcesses : $supervisor->options->minProcesses, ]; }