Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting workers name #840

Merged
merged 3 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
3 changes: 3 additions & 0 deletions src/Console/SupervisorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The balance= line is now duplicated, probably not intended to be

{--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}
Expand Down Expand Up @@ -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'),
Expand Down
33 changes: 33 additions & 0 deletions src/QueueCommandString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
5 changes: 1 addition & 4 deletions src/SupervisorCommandString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
66 changes: 60 additions & 6 deletions src/SupervisorOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Laravel\Horizon;

use Illuminate\Queue\WorkerOptions;

class SupervisorOptions extends WorkerOptions
class SupervisorOptions
{
/**
* The name of the supervisor.
Expand All @@ -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.
*
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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,
];
Expand Down
4 changes: 1 addition & 3 deletions src/WorkerCommandString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/AddSupervisorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/SupervisorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand All @@ -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()
);
}
Expand Down