From 557f74013779c02bcf503f0fe46bcd7cf1823482 Mon Sep 17 00:00:00 2001 From: Craig Anderson Date: Fri, 19 Mar 2021 11:26:09 -0400 Subject: [PATCH 1/2] Add Supervisor Rest Option --- src/Console/SupervisorCommand.php | 2 ++ src/QueueCommandString.php | 4 ++-- src/SupervisorOptions.php | 11 +++++++++++ tests/Feature/AddSupervisorTest.php | 2 +- tests/Feature/SupervisorTest.php | 6 +++--- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Console/SupervisorCommand.php b/src/Console/SupervisorCommand.php index ead85e10..8f7015cb 100644 --- a/src/Console/SupervisorCommand.php +++ b/src/Console/SupervisorCommand.php @@ -30,6 +30,7 @@ class SupervisorCommand extends Command {--paused : Start the supervisor in a paused state} {--queue= : The names of the queues to work} {--sleep=3 : Number of seconds to sleep when no job is available} + {--rest=0 : Number of seconds to rest between jobs} {--timeout=60 : The number of seconds a child process can run} {--tries=0 : Number of times to attempt a job before logging it failed} {--balance-cooldown=3 : The number of seconds to wait in between auto-scaling attempts} @@ -124,6 +125,7 @@ protected function supervisorOptions() $this->option('memory'), $this->option('timeout'), $this->option('sleep'), + $this->option('rest'), $this->option('tries'), $this->option('force'), $this->option('nice'), diff --git a/src/QueueCommandString.php b/src/QueueCommandString.php index be03d3ec..15e7d98a 100644 --- a/src/QueueCommandString.php +++ b/src/QueueCommandString.php @@ -49,9 +49,9 @@ public static function toSupervisorOptionsString(SupervisorOptions $options) */ public static function toOptionsString(SupervisorOptions $options, $paused = false) { - $string = sprintf('--backoff=%s --max-time=%s --max-jobs=%s --memory=%s --queue="%s" --sleep=%s --timeout=%s --tries=%s', + $string = sprintf('--backoff=%s --max-time=%s --max-jobs=%s --memory=%s --queue="%s" --sleep=%s --rest=%s --timeout=%s --tries=%s', $options->backoff, $options->maxTime, $options->maxJobs, $options->memory, - $options->queue, $options->sleep, $options->timeout, $options->maxTries + $options->queue, $options->sleep, $options->rest, $options->timeout, $options->maxTries ); if ($options->force) { diff --git a/src/SupervisorOptions.php b/src/SupervisorOptions.php index fc276d03..ca524110 100644 --- a/src/SupervisorOptions.php +++ b/src/SupervisorOptions.php @@ -130,6 +130,13 @@ class SupervisorOptions */ public $sleep; + /** + * The number of seconds to rest between jobs. + * + * @var int + */ + public $rest; + /** * The maximum amount of times a job may be attempted. * @@ -160,6 +167,7 @@ class SupervisorOptions * @param int $memory * @param int $timeout * @param int $sleep + * @param int $rest * @param int $maxTries * @param bool $force * @param int $nice @@ -180,6 +188,7 @@ public function __construct($name, $memory = 128, $timeout = 60, $sleep = 3, + $rest = 0, $maxTries = 0, $force = false, $nice = 0, @@ -200,6 +209,7 @@ public function __construct($name, $this->memory = $memory; $this->timeout = $timeout; $this->sleep = $sleep; + $this->rest = $rest; $this->maxTries = $maxTries; $this->force = $force; $this->nice = $nice; @@ -294,6 +304,7 @@ public function toArray() 'name' => $this->name, 'workersName' => $this->workersName, 'sleep' => $this->sleep, + 'rest' => $this->rest, 'timeout' => $this->timeout, 'balanceCooldown' => $this->balanceCooldown, 'balanceMaxShift' => $this->balanceMaxShift, diff --git a/tests/Feature/AddSupervisorTest.php b/tests/Feature/AddSupervisorTest.php index 9f1431e6..9ef5cd32 100644 --- a/tests/Feature/AddSupervisorTest.php +++ b/tests/Feature/AddSupervisorTest.php @@ -28,7 +28,7 @@ public function test_add_supervisor_command_creates_new_supervisor_on_master_pro $this->assertCount(1, $master->supervisors); $this->assertSame( - 'exec '.$phpBinary.' artisan horizon:supervisor my-supervisor redis --workers-name=default --balance=off --max-processes=1 --min-processes=1 --nice=0 --balance-cooldown=3 --balance-max-shift=1 --parent-id=0 --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="default" --sleep=3 --timeout=60 --tries=0', + 'exec '.$phpBinary.' artisan horizon:supervisor my-supervisor redis --workers-name=default --balance=off --max-processes=1 --min-processes=1 --nice=0 --balance-cooldown=3 --balance-max-shift=1 --parent-id=0 --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="default" --sleep=3 --rest=0 --timeout=60 --tries=0', $master->supervisors->first()->process->getCommandLine() ); } diff --git a/tests/Feature/SupervisorTest.php b/tests/Feature/SupervisorTest.php index 61cbde92..11d7a8dc 100644 --- a/tests/Feature/SupervisorTest.php +++ b/tests/Feature/SupervisorTest.php @@ -67,7 +67,7 @@ public function test_supervisor_can_start_worker_process_with_given_options() $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="default" --sleep=3 --timeout=60 --tries=0', + 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="default" --sleep=3 --rest=0 --timeout=60 --tries=0', $supervisor->processes()[0]->getCommandLine() ); } @@ -85,12 +85,12 @@ public function test_supervisor_starts_multiple_pools_when_balancing() $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" --sleep=3 --timeout=60 --tries=0', + 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="first" --sleep=3 --rest=0 --timeout=60 --tries=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="second" --sleep=3 --timeout=60 --tries=0', + 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="second" --sleep=3 --rest=0 --timeout=60 --tries=0', $supervisor->processes()[1]->getCommandLine() ); } From f99d08d11c3f707cce667086a3a916f722ffb0f1 Mon Sep 17 00:00:00 2001 From: Craig Anderson Date: Fri, 19 Mar 2021 11:45:58 -0400 Subject: [PATCH 2/2] Change argument order to prevent breaking change --- src/Console/SupervisorCommand.php | 8 ++++---- src/QueueCommandString.php | 4 ++-- src/SupervisorOptions.php | 24 ++++++++++++------------ tests/Feature/AddSupervisorTest.php | 2 +- tests/Feature/SupervisorTest.php | 6 +++--- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Console/SupervisorCommand.php b/src/Console/SupervisorCommand.php index 8f7015cb..f35c9b6c 100644 --- a/src/Console/SupervisorCommand.php +++ b/src/Console/SupervisorCommand.php @@ -30,13 +30,13 @@ class SupervisorCommand extends Command {--paused : Start the supervisor in a paused state} {--queue= : The names of the queues to work} {--sleep=3 : Number of seconds to sleep when no job is available} - {--rest=0 : Number of seconds to rest between jobs} {--timeout=60 : The number of seconds a child process can run} {--tries=0 : Number of times to attempt a job before logging it failed} {--balance-cooldown=3 : The number of seconds to wait in between auto-scaling attempts} {--balance-max-shift=1 : The maximum number of processes to increase or decrease per one scaling} {--workers-name=default : The name that should be assigned to the workers} - {--parent-id=0 : The parent process ID}'; + {--parent-id=0 : The parent process ID} + {--rest=0 : Number of seconds to rest between jobs}'; /** * The console command description. @@ -125,13 +125,13 @@ protected function supervisorOptions() $this->option('memory'), $this->option('timeout'), $this->option('sleep'), - $this->option('rest'), $this->option('tries'), $this->option('force'), $this->option('nice'), $this->option('balance-cooldown'), $this->option('balance-max-shift'), - $this->option('parent-id') + $this->option('parent-id'), + $this->option('rest') ); } diff --git a/src/QueueCommandString.php b/src/QueueCommandString.php index 15e7d98a..0277b581 100644 --- a/src/QueueCommandString.php +++ b/src/QueueCommandString.php @@ -49,9 +49,9 @@ public static function toSupervisorOptionsString(SupervisorOptions $options) */ public static function toOptionsString(SupervisorOptions $options, $paused = false) { - $string = sprintf('--backoff=%s --max-time=%s --max-jobs=%s --memory=%s --queue="%s" --sleep=%s --rest=%s --timeout=%s --tries=%s', + $string = sprintf('--backoff=%s --max-time=%s --max-jobs=%s --memory=%s --queue="%s" --sleep=%s --timeout=%s --tries=%s --rest=%s', $options->backoff, $options->maxTime, $options->maxJobs, $options->memory, - $options->queue, $options->sleep, $options->rest, $options->timeout, $options->maxTries + $options->queue, $options->sleep, $options->timeout, $options->maxTries, $options->rest ); if ($options->force) { diff --git a/src/SupervisorOptions.php b/src/SupervisorOptions.php index ca524110..95721094 100644 --- a/src/SupervisorOptions.php +++ b/src/SupervisorOptions.php @@ -130,13 +130,6 @@ class SupervisorOptions */ public $sleep; - /** - * The number of seconds to rest between jobs. - * - * @var int - */ - public $rest; - /** * The maximum amount of times a job may be attempted. * @@ -151,6 +144,13 @@ class SupervisorOptions */ public $force; + /** + * The number of seconds to rest between jobs. + * + * @var int + */ + public $rest; + /** * Create a new worker options instance. * @@ -167,13 +167,13 @@ class SupervisorOptions * @param int $memory * @param int $timeout * @param int $sleep - * @param int $rest * @param int $maxTries * @param bool $force * @param int $nice * @param int $balanceCooldown * @param int $balanceMaxShift * @param int $parentId + * @param int $rest */ public function __construct($name, $connection, @@ -188,13 +188,13 @@ public function __construct($name, $memory = 128, $timeout = 60, $sleep = 3, - $rest = 0, $maxTries = 0, $force = false, $nice = 0, $balanceCooldown = 3, $balanceMaxShift = 1, - $parentId = 0) + $parentId = 0, + $rest = 0) { $this->name = $name; $this->connection = $connection; @@ -209,13 +209,13 @@ public function __construct($name, $this->memory = $memory; $this->timeout = $timeout; $this->sleep = $sleep; - $this->rest = $rest; $this->maxTries = $maxTries; $this->force = $force; $this->nice = $nice; $this->balanceCooldown = $balanceCooldown; $this->balanceMaxShift = $balanceMaxShift; $this->parentId = $parentId; + $this->rest = $rest; } /** @@ -304,11 +304,11 @@ public function toArray() 'name' => $this->name, 'workersName' => $this->workersName, 'sleep' => $this->sleep, - 'rest' => $this->rest, 'timeout' => $this->timeout, 'balanceCooldown' => $this->balanceCooldown, 'balanceMaxShift' => $this->balanceMaxShift, 'parentId' => $this->parentId, + 'rest' => $this->rest, ]; } diff --git a/tests/Feature/AddSupervisorTest.php b/tests/Feature/AddSupervisorTest.php index 9ef5cd32..f2af33d3 100644 --- a/tests/Feature/AddSupervisorTest.php +++ b/tests/Feature/AddSupervisorTest.php @@ -28,7 +28,7 @@ public function test_add_supervisor_command_creates_new_supervisor_on_master_pro $this->assertCount(1, $master->supervisors); $this->assertSame( - 'exec '.$phpBinary.' artisan horizon:supervisor my-supervisor redis --workers-name=default --balance=off --max-processes=1 --min-processes=1 --nice=0 --balance-cooldown=3 --balance-max-shift=1 --parent-id=0 --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="default" --sleep=3 --rest=0 --timeout=60 --tries=0', + 'exec '.$phpBinary.' artisan horizon:supervisor my-supervisor redis --workers-name=default --balance=off --max-processes=1 --min-processes=1 --nice=0 --balance-cooldown=3 --balance-max-shift=1 --parent-id=0 --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="default" --sleep=3 --timeout=60 --tries=0 --rest=0', $master->supervisors->first()->process->getCommandLine() ); } diff --git a/tests/Feature/SupervisorTest.php b/tests/Feature/SupervisorTest.php index 11d7a8dc..28d8c146 100644 --- a/tests/Feature/SupervisorTest.php +++ b/tests/Feature/SupervisorTest.php @@ -67,7 +67,7 @@ public function test_supervisor_can_start_worker_process_with_given_options() $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="default" --sleep=3 --rest=0 --timeout=60 --tries=0', + 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="default" --sleep=3 --timeout=60 --tries=0 --rest=0', $supervisor->processes()[0]->getCommandLine() ); } @@ -85,12 +85,12 @@ public function test_supervisor_starts_multiple_pools_when_balancing() $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" --sleep=3 --rest=0 --timeout=60 --tries=0', + 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="first" --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="second" --sleep=3 --rest=0 --timeout=60 --tries=0', + 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="second" --sleep=3 --timeout=60 --tries=0 --rest=0', $supervisor->processes()[1]->getCommandLine() ); }