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

[develop] Support worker max-time and max-jobs #860

Merged
merged 2 commits into from
Jul 9, 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
5 changes: 4 additions & 1 deletion src/Console/SupervisorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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'),
Expand Down
2 changes: 2 additions & 0 deletions src/Console/WorkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
6 changes: 3 additions & 3 deletions src/QueueCommandString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions src/SupervisorOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand All @@ -133,6 +149,8 @@ public function __construct($name,
$workersName = 'default',
$balance = 'off',
$backoff = 0,
$maxTime = 0,
$maxJobs = 0,
$maxProcesses = 1,
$minProcesses = 1,
$memory = 128,
Expand All @@ -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;
Expand Down Expand Up @@ -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,
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 --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()
);
}
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 --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()
);
}
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 --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()
);
}
Expand Down