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

[3.0] Supervisor nice option #551

Merged
merged 2 commits into from
Mar 20, 2019
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
8 changes: 7 additions & 1 deletion src/Console/SupervisorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SupervisorCommand extends Command
{--max-processes=1 : The maximum number of total workers to start}
{--min-processes=1 : The minimum number of workers to assign per queue}
{--memory=128 : The memory limit in megabytes}
{--nice=0 : Increment to the process niceness}
{--paused : Start the supervisor in a paused state}
{--queue= : The names of the queues to work}
{--sleep=3 : Number of seconds to sleep when no job is available}
Expand Down Expand Up @@ -74,6 +75,10 @@ public function handle(SupervisorFactory $factory)
*/
protected function start($supervisor)
{
if ($supervisor->options->nice) {
proc_nice($supervisor->options->nice);
}

$supervisor->handleOutputUsing(function ($type, $line) {
$this->output->write($line);
});
Expand Down Expand Up @@ -106,7 +111,8 @@ protected function supervisorOptions()
$this->option('timeout'),
$this->option('sleep'),
$this->option('tries'),
$this->option('force')
$this->option('force'),
$this->option('nice')
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/QueueCommandString.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class QueueCommandString
*/
public static function toOptionsString(SupervisorOptions $options, $paused = false)
{
$string = sprintf('--delay=%s --memory=%s --queue="%s" --sleep=%s --timeout=%s --tries=%s',
$options->delay, $options->memory, $options->queue,
$string = sprintf('--delay=%s --memory=%s --nice=%s --queue="%s" --sleep=%s --timeout=%s --tries=%s',
$options->delay, $options->memory, $options->nice, $options->queue,
$options->sleep, $options->timeout, $options->maxTries
);

Expand Down
12 changes: 11 additions & 1 deletion src/SupervisorOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class SupervisorOptions extends WorkerOptions
*/
public $minProcesses = 1;

/**
* Increment to the process niceness.
*
* @var int
*/
public $nice = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is problematic:

  • a niceness of 0 is a valid value
  • the (linux) default for niceness is usually 10

IMHO the correct way is to use null here.


/**
* The working directories that new workers should be started from.
*
Expand All @@ -70,17 +77,19 @@ class SupervisorOptions extends WorkerOptions
* @param int $sleep
* @param int $maxTries
* @param bool $force
* @param int $nice
*/
public function __construct($name, $connection, $queue = null, $balance = 'off',
$delay = 0, $maxProcesses = 1, $minProcesses = 1, $memory = 128,
$timeout = 60, $sleep = 3, $maxTries = 0, $force = false)
$timeout = 60, $sleep = 3, $maxTries = 0, $force = false, $nice = 0)
{
$this->name = $name;
$this->balance = $balance;
$this->connection = $connection;
$this->maxProcesses = $maxProcesses;
$this->minProcesses = $minProcesses;
$this->queue = $queue ?: config('queue.connections.'.$connection.'.queue');
$this->nice = $nice;

parent::__construct($delay, $memory, $timeout, $sleep, $maxTries, $force);
}
Expand Down Expand Up @@ -165,6 +174,7 @@ public function toArray()
'minProcesses' => $this->minProcesses,
'maxTries' => $this->maxTries,
'memory' => $this->memory,
'nice' => $this->nice,
'name' => $this->name,
'sleep' => $this->sleep,
'timeout' => $this->timeout,
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 --delay=0 --memory=128 --queue="default" --sleep=3 --timeout=60 --tries=0 --balance=off --max-processes=1 --min-processes=1',
'exec '.$phpBinary.' artisan horizon:supervisor my-supervisor redis --delay=0 --memory=128 --nice=0 --queue="default" --sleep=3 --timeout=60 --tries=0 --balance=off --max-processes=1 --min-processes=1',
$master->supervisors->first()->process->getCommandLine()
);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/SupervisorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,23 @@ public function test_supervisor_command_can_start_paused_supervisors()

$this->assertFalse($factory->supervisor->working);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_supervisor_command_can_set_process_niceness()
{
$this->app->instance(SupervisorFactory::class, $factory = new FakeSupervisorFactory);
$this->artisan('horizon:supervisor', ['name' => 'foo', 'connection' => 'redis', '--nice' => 10]);

$this->assertEquals(10, $this->myNiceness());
}

private function myNiceness()
{
$pid = getmypid();

return (int) trim(`ps -p $pid -o nice=`);
}
}
11 changes: 7 additions & 4 deletions tests/Feature/SupervisorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public function test_supervisor_can_start_worker_process_with_given_options()
Queue::push(new Jobs\BasicJob);
$this->assertEquals(1, $this->recentJobs());

$this->supervisor = $supervisor = new Supervisor($options = $this->options());
$options = $this->options();
$options->nice = 10;

$this->supervisor = $supervisor = new Supervisor($options);

$supervisor->scale(1);
$supervisor->loop();
Expand All @@ -66,7 +69,7 @@ public function test_supervisor_can_start_worker_process_with_given_options()

$host = MasterSupervisor::name();
$this->assertEquals(
'exec '.$this->phpBinary.' worker.php redis --delay=0 --memory=128 --queue="default" --sleep=3 --timeout=60 --tries=0 --supervisor='.$host.':name',
'exec '.$this->phpBinary.' worker.php redis --delay=0 --memory=128 --nice=10 --queue="default" --sleep=3 --timeout=60 --tries=0 --supervisor='.$host.':name',
$supervisor->processes()[0]->getCommandLine()
);
}
Expand All @@ -84,12 +87,12 @@ public function test_supervisor_starts_multiple_pools_when_balancing()
$host = MasterSupervisor::name();

$this->assertEquals(
'exec '.$this->phpBinary.' worker.php redis --delay=0 --memory=128 --queue="first" --sleep=3 --timeout=60 --tries=0 --supervisor='.$host.':name',
'exec '.$this->phpBinary.' worker.php redis --delay=0 --memory=128 --nice=0 --queue="first" --sleep=3 --timeout=60 --tries=0 --supervisor='.$host.':name',
$supervisor->processes()[0]->getCommandLine()
);

$this->assertEquals(
'exec '.$this->phpBinary.' worker.php redis --delay=0 --memory=128 --queue="second" --sleep=3 --timeout=60 --tries=0 --supervisor='.$host.':name',
'exec '.$this->phpBinary.' worker.php redis --delay=0 --memory=128 --nice=0 --queue="second" --sleep=3 --timeout=60 --tries=0 --supervisor='.$host.':name',
$supervisor->processes()[1]->getCommandLine()
);
}
Expand Down