diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 4f5ed2ae..4bf5b71c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -19,7 +19,7 @@ jobs: strategy: fail-fast: true matrix: - php: [7.2, 7.3, 7.4] + php: [7.3, 7.4] laravel: [^8.0] name: P${{ matrix.php }} - L${{ matrix.laravel }} diff --git a/src/Console/SupervisorCommand.php b/src/Console/SupervisorCommand.php index b59157a5..71a99d14 100644 --- a/src/Console/SupervisorCommand.php +++ b/src/Console/SupervisorCommand.php @@ -17,6 +17,8 @@ class SupervisorCommand extends Command protected $signature = 'horizon:supervisor {name : The name of supervisor} {connection : The name of the connection to work} + {--workers-name=default : The name of the workers} + {--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} @@ -108,6 +110,7 @@ protected function supervisorOptions() $this->argument('name'), $this->argument('connection'), $this->getQueue($this->argument('connection')), + $this->option('workers-name'), $this->option('balance'), $backoff, $this->option('max-processes'), diff --git a/src/QueueCommandString.php b/src/QueueCommandString.php index cf46b2e3..51036c0f 100644 --- a/src/QueueCommandString.php +++ b/src/QueueCommandString.php @@ -4,6 +4,39 @@ class QueueCommandString { + /** + * Get the additional option string for the worker command. + * + * @param \Laravel\Horizon\SupervisorOptions $options + * @return string + */ + public static function toWorkerOptionsString(SupervisorOptions $options) + { + return sprintf('--name=%s --supervisor=%s %s', + $options->workersName, + $options->name, + static::toOptionsString($options) + ); + } + + /** + * Get the additional option string for the supervisor command. + * + * @param \Laravel\Horizon\SupervisorOptions $options + * @return string + */ + public static function toSupervisorOptionsString(SupervisorOptions $options) + { + return sprintf('--workers-name=%s --balance=%s --max-processes=%s --min-processes=%s --nice=%s %s', + $options->workersName, + $options->balance, + $options->maxProcesses, + $options->minProcesses, + $options->nice, + static::toOptionsString($options) + ); + } + /** * Get the additional option string for the command. * diff --git a/src/SupervisorCommandString.php b/src/SupervisorCommandString.php index 213431b4..c37f31c0 100644 --- a/src/SupervisorCommandString.php +++ b/src/SupervisorCommandString.php @@ -36,10 +36,7 @@ public static function fromOptions(SupervisorOptions $options) */ public static function toOptionsString(SupervisorOptions $options) { - return sprintf('%s --balance=%s --max-processes=%s --min-processes=%s --nice=%s', - QueueCommandString::toOptionsString($options), $options->balance, - $options->maxProcesses, $options->minProcesses, $options->nice - ); + return QueueCommandString::toSupervisorOptionsString($options); } /** diff --git a/src/SupervisorOptions.php b/src/SupervisorOptions.php index a64d2d93..2efcc03c 100644 --- a/src/SupervisorOptions.php +++ b/src/SupervisorOptions.php @@ -2,9 +2,7 @@ namespace Laravel\Horizon; -use Illuminate\Queue\WorkerOptions; - -class SupervisorOptions extends WorkerOptions +class SupervisorOptions { /** * The name of the supervisor. @@ -13,6 +11,13 @@ class SupervisorOptions extends WorkerOptions */ public $name; + /** + * The name of the workers. + * + * @var string + */ + public $workersName; + /** * The queue connection that should be utilized. * @@ -62,12 +67,55 @@ class SupervisorOptions extends WorkerOptions */ public $directory; + /** + * The number of seconds to wait before retrying a job that encountered an uncaught exception. + * + * @var int + */ + public $backoff; + + /** + * The maximum amount of RAM the worker may consume. + * + * @var int + */ + public $memory; + + /** + * The maximum number of seconds a child worker may run. + * + * @var int + */ + public $timeout; + + /** + * The number of seconds to wait in between polling the queue. + * + * @var int + */ + public $sleep; + + /** + * The maximum amount of times a job may be attempted. + * + * @var int + */ + public $maxTries; + + /** + * Indicates if the worker should run in maintenance mode. + * + * @var bool + */ + public $force; + /** * Create a new worker options instance. * * @param string $name * @param string $connection * @param string $queue + * @param string $workersName * @param string $balance * @param int $backoff * @param int $maxProcesses @@ -79,19 +127,24 @@ class SupervisorOptions extends WorkerOptions * @param bool $force * @param int $nice */ - public function __construct($name, $connection, $queue = null, $balance = 'off', + public function __construct($name, $connection, $queue = null, $workersName = 'default', $balance = 'off', $backoff = 0, $maxProcesses = 1, $minProcesses = 1, $memory = 128, $timeout = 60, $sleep = 3, $maxTries = 0, $force = false, $nice = 0) { $this->name = $name; + $this->workersName = $workersName; $this->nice = $nice; $this->balance = $balance; $this->connection = $connection; $this->maxProcesses = $maxProcesses; $this->minProcesses = $minProcesses; $this->queue = $queue ?: config('queue.connections.'.$connection.'.queue'); - - parent::__construct($backoff, $memory, $timeout, $sleep, $maxTries, $force); + $this->backoff = $backoff; + $this->sleep = $sleep; + $this->force = $force; + $this->memory = $memory; + $this->timeout = $timeout; + $this->maxTries = $maxTries; } /** @@ -176,6 +229,7 @@ public function toArray() 'memory' => $this->memory, 'nice' => $this->nice, 'name' => $this->name, + 'workersName' => $this->workersName, 'sleep' => $this->sleep, 'timeout' => $this->timeout, ]; diff --git a/src/WorkerCommandString.php b/src/WorkerCommandString.php index aeee28af..9c1fb707 100644 --- a/src/WorkerCommandString.php +++ b/src/WorkerCommandString.php @@ -36,9 +36,7 @@ public static function fromOptions(SupervisorOptions $options) */ public static function toOptionsString(SupervisorOptions $options) { - return sprintf('%s --supervisor=%s', - QueueCommandString::toOptionsString($options), $options->name - ); + return QueueCommandString::toWorkerOptionsString($options); } /** diff --git a/tests/Feature/AddSupervisorTest.php b/tests/Feature/AddSupervisorTest.php index f4a878fe..e61c34b2 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 --backoff=0 --memory=128 --queue="default" --sleep=3 --timeout=60 --tries=0 --balance=off --max-processes=1 --min-processes=1 --nice=0', + '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', $master->supervisors->first()->process->getCommandLine() ); } diff --git a/tests/Feature/SupervisorTest.php b/tests/Feature/SupervisorTest.php index a8912c5e..7c5f3ae5 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 --backoff=0 --memory=128 --queue="default" --sleep=3 --timeout=60 --tries=0 --supervisor='.$host.':name', + 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=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 --backoff=0 --memory=128 --queue="first" --sleep=3 --timeout=60 --tries=0 --supervisor='.$host.':name', + 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=0 --memory=128 --queue="first" --sleep=3 --timeout=60 --tries=0', $supervisor->processes()[0]->getCommandLine() ); $this->assertEquals( - 'exec '.$this->phpBinary.' worker.php redis --backoff=0 --memory=128 --queue="second" --sleep=3 --timeout=60 --tries=0 --supervisor='.$host.':name', + 'exec '.$this->phpBinary.' worker.php redis --name=default --supervisor='.$host.':name --backoff=0 --memory=128 --queue="second" --sleep=3 --timeout=60 --tries=0', $supervisor->processes()[1]->getCommandLine() ); }