Skip to content

Commit

Permalink
[5.x] Add Supervisor Rest Option (#992)
Browse files Browse the repository at this point in the history
* Add Supervisor Rest Option

* Change argument order to prevent breaking change
  • Loading branch information
intrepidws authored Mar 30, 2021
1 parent b8beb3a commit 3d42f9a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/Console/SupervisorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class SupervisorCommand extends Command
{--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.
Expand Down Expand Up @@ -129,7 +130,8 @@ protected function supervisorOptions()
$this->option('nice'),
$this->option('balance-cooldown'),
$this->option('balance-max-shift'),
$this->option('parent-id')
$this->option('parent-id'),
$this->option('rest')
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/QueueCommandString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 --timeout=%s --tries=%s --rest=%s',
$options->backoff, $options->maxTime, $options->maxJobs, $options->memory,
$options->queue, $options->sleep, $options->timeout, $options->maxTries
$options->queue, $options->sleep, $options->timeout, $options->maxTries, $options->rest
);

if ($options->force) {
Expand Down
13 changes: 12 additions & 1 deletion src/SupervisorOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,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.
*
Expand All @@ -166,6 +173,7 @@ class SupervisorOptions
* @param int $balanceCooldown
* @param int $balanceMaxShift
* @param int $parentId
* @param int $rest
*/
public function __construct($name,
$connection,
Expand All @@ -185,7 +193,8 @@ public function __construct($name,
$nice = 0,
$balanceCooldown = 3,
$balanceMaxShift = 1,
$parentId = 0)
$parentId = 0,
$rest = 0)
{
$this->name = $name;
$this->connection = $connection;
Expand All @@ -206,6 +215,7 @@ public function __construct($name,
$this->balanceCooldown = $balanceCooldown;
$this->balanceMaxShift = $balanceMaxShift;
$this->parentId = $parentId;
$this->rest = $rest;
}

/**
Expand Down Expand Up @@ -298,6 +308,7 @@ public function toArray()
'balanceCooldown' => $this->balanceCooldown,
'balanceMaxShift' => $this->balanceMaxShift,
'parentId' => $this->parentId,
'rest' => $this->rest,
];
}

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->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 --timeout=60 --tries=0 --rest=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->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 --timeout=60 --tries=0 --rest=0',
$supervisor->processes()[0]->getCommandLine()
);
}
Expand All @@ -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 --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 --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()
);
}
Expand Down

0 comments on commit 3d42f9a

Please sign in to comment.