From bd4b695f74a815136f82a71cbac07f182e4f35a9 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Thu, 9 Jul 2020 07:55:23 +0200 Subject: [PATCH 1/2] suppor worker max-time and max-jobs --- src/Console/SupervisorCommand.php | 5 ++++- src/Console/WorkCommand.php | 2 ++ src/QueueCommandString.php | 6 +++--- src/SupervisorOptions.php | 22 ++++++++++++++++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Console/SupervisorCommand.php b/src/Console/SupervisorCommand.php index 676da3bc..5a722da4 100644 --- a/src/Console/SupervisorCommand.php +++ b/src/Console/SupervisorCommand.php @@ -18,9 +18,10 @@ class SupervisorCommand extends Command {name : The name of supervisor} {connection : The name of the connection to work} {--balance= : The balancing strategy the supervisor should apply} - {--balance= : The balancing strategy the supervisor should apply} {--delay=0 : The number of seconds to delay failed jobs (Deprecated)} {--backoff=0 : The number of seconds to wait before retrying a job that encountered an uncaught exception} + {--max-jobs=0 : The number of jobs to process before stopping a child process} + {--max-time=0 : The maximum number of seconds a child process should run} {--force : Force the worker to run even in maintenance mode} {--max-processes=1 : The maximum number of total workers to start} {--min-processes=1 : The minimum number of workers to assign per queue} @@ -113,6 +114,8 @@ protected function supervisorOptions() $this->option('workers-name'), $this->option('balance'), $backoff, + $this->option('max-time'), + $this->option('max-jobs'), $this->option('max-processes'), $this->option('min-processes'), $this->option('memory'), diff --git a/src/Console/WorkCommand.php b/src/Console/WorkCommand.php index a477eb5e..a88c6e13 100644 --- a/src/Console/WorkCommand.php +++ b/src/Console/WorkCommand.php @@ -16,6 +16,8 @@ class WorkCommand extends BaseWorkCommand {--name=default : The name of the worker} {--delay=0 : The number of seconds to delay failed jobs (Deprecated)} {--backoff=0 : The number of seconds to wait before retrying a job that encountered an uncaught exception} + {--max-jobs=0 : The number of jobs to process before stopping} + {--max-time=0 : The maximum number of seconds the worker should run} {--daemon : Run the worker in daemon mode (Deprecated)} {--force : Force the worker to run even in maintenance mode} {--memory=128 : The memory limit in megabytes} diff --git a/src/QueueCommandString.php b/src/QueueCommandString.php index 51036c0f..0734c61c 100644 --- a/src/QueueCommandString.php +++ b/src/QueueCommandString.php @@ -46,9 +46,9 @@ public static function toSupervisorOptionsString(SupervisorOptions $options) */ public static function toOptionsString(SupervisorOptions $options, $paused = false) { - $string = sprintf('--backoff=%s --memory=%s --queue="%s" --sleep=%s --timeout=%s --tries=%s', - $options->backoff, $options->memory, $options->queue, - $options->sleep, $options->timeout, $options->maxTries + $string = sprintf('--backoff=%s --max-time=%s --max-jobs=%s --memory=%s --queue="%s" --sleep=%s --timeout=%s --tries=%s', + $options->backoff, $options->maxTime, $options->maxJobs, $options->memory, + $options->queue, $options->sleep, $options->timeout, $options->maxTries ); if ($options->force) { diff --git a/src/SupervisorOptions.php b/src/SupervisorOptions.php index 2c9915dc..70a1c81d 100644 --- a/src/SupervisorOptions.php +++ b/src/SupervisorOptions.php @@ -74,6 +74,20 @@ class SupervisorOptions */ public $backoff; + /** + * The maximum number of jobs to run. + * + * @var int + */ + public $maxJobs; + + /** + * The maximum number of seconds a worker may live. + * + * @var int + */ + public $maxTime; + /** * The maximum amount of RAM the worker may consume. * @@ -118,6 +132,8 @@ class SupervisorOptions * @param string $workersName * @param string $balance * @param int $backoff + * @param int $maxTime + * @param int $maxJobs * @param int $maxProcesses * @param int $minProcesses * @param int $memory @@ -133,6 +149,8 @@ public function __construct($name, $workersName = 'default', $balance = 'off', $backoff = 0, + $maxTime = 0, + $maxJobs = 0, $maxProcesses = 1, $minProcesses = 1, $memory = 128, @@ -148,6 +166,8 @@ public function __construct($name, $this->workersName = $workersName; $this->balance = $balance; $this->backoff = $backoff; + $this->maxTime = $maxTime; + $this->maxJobs = $maxJobs; $this->maxProcesses = $maxProcesses; $this->minProcesses = $minProcesses; $this->memory = $memory; @@ -237,6 +257,8 @@ public function toArray() 'maxProcesses' => $this->maxProcesses, 'minProcesses' => $this->minProcesses, 'maxTries' => $this->maxTries, + 'maxTime' => $this->maxTime, + 'maxJobs' => $this->maxJobs, 'memory' => $this->memory, 'nice' => $this->nice, 'name' => $this->name, From 663da4ed254de9bca67e1cbafee5d6eb6eb8287b Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Thu, 9 Jul 2020 07:59:16 +0200 Subject: [PATCH 2/2] fix tests --- tests/Feature/AddSupervisorTest.php | 2 +- tests/Feature/SupervisorTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Feature/AddSupervisorTest.php b/tests/Feature/AddSupervisorTest.php index e61c34b2..8b404bab 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->assertEquals( - 'exec '.$phpBinary.' artisan horizon:supervisor my-supervisor redis --workers-name=default --balance=off --max-processes=1 --min-processes=1 --nice=0 --backoff=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 --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue="default" --sleep=3 --timeout=60 --tries=0', $master->supervisors->first()->process->getCommandLine() ); } diff --git a/tests/Feature/SupervisorTest.php b/tests/Feature/SupervisorTest.php index 7c5f3ae5..b9610c27 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->assertEquals( - 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=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 --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->assertEquals( - 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=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 --timeout=60 --tries=0', $supervisor->processes()[0]->getCommandLine() ); $this->assertEquals( - 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=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 --timeout=60 --tries=0', $supervisor->processes()[1]->getCommandLine() ); }