From 70bfc63716be453ec363ff3d0162b6fcc7f597fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Dronga?= Date: Sun, 24 Jun 2018 13:42:39 +0300 Subject: [PATCH] [consumption] add process niceness extension --- .../Extension/NicenessExtension.php | 50 +++++++++++++++++++ .../LimitsExtensionsCommandTrait.php | 9 +++- .../Extension/NicenessExtensionTest.php | 47 +++++++++++++++++ .../Client/ConsumeMessagesCommandTest.php | 3 +- .../ConsumeMessagesCommandTest.php | 3 +- ...ntainerAwareConsumeMessagesCommandTest.php | 3 +- .../LimitsExtensionsCommandTraitTest.php | 21 +++++++- 7 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 pkg/enqueue/Consumption/Extension/NicenessExtension.php create mode 100644 pkg/enqueue/Tests/Consumption/Extension/NicenessExtensionTest.php diff --git a/pkg/enqueue/Consumption/Extension/NicenessExtension.php b/pkg/enqueue/Consumption/Extension/NicenessExtension.php new file mode 100644 index 000000000..01a5383a2 --- /dev/null +++ b/pkg/enqueue/Consumption/Extension/NicenessExtension.php @@ -0,0 +1,50 @@ +niceness = $niceness; + } + + /** + * {@inheritdoc} + */ + public function onStart(Context $context) + { + if (0 !== $this->niceness) { + $changed = @proc_nice($this->niceness); + if (!$changed) { + throw new \InvalidArgumentException(sprintf( + 'Cannot change process niceness, got warning: "%s"', + error_get_last()['message'] + )); + } + } + } +} diff --git a/pkg/enqueue/Symfony/Consumption/LimitsExtensionsCommandTrait.php b/pkg/enqueue/Symfony/Consumption/LimitsExtensionsCommandTrait.php index 34bc76d37..2a756180c 100644 --- a/pkg/enqueue/Symfony/Consumption/LimitsExtensionsCommandTrait.php +++ b/pkg/enqueue/Symfony/Consumption/LimitsExtensionsCommandTrait.php @@ -5,6 +5,7 @@ use Enqueue\Consumption\Extension\LimitConsumedMessagesExtension; use Enqueue\Consumption\Extension\LimitConsumerMemoryExtension; use Enqueue\Consumption\Extension\LimitConsumptionTimeExtension; +use Enqueue\Consumption\Extension\NicenessExtension; use Enqueue\Consumption\ExtensionInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -20,7 +21,8 @@ protected function configureLimitsExtensions() $this ->addOption('message-limit', null, InputOption::VALUE_REQUIRED, 'Consume n messages and exit') ->addOption('time-limit', null, InputOption::VALUE_REQUIRED, 'Consume messages during this time') - ->addOption('memory-limit', null, InputOption::VALUE_REQUIRED, 'Consume messages until process reaches this memory limit in MB'); + ->addOption('memory-limit', null, InputOption::VALUE_REQUIRED, 'Consume messages until process reaches this memory limit in MB') + ->addOption('niceness', null, InputOption::VALUE_REQUIRED, 'Set process niceness'); } /** @@ -58,6 +60,11 @@ protected function getLimitsExtensions(InputInterface $input, OutputInterface $o $extensions[] = new LimitConsumerMemoryExtension($memoryLimit); } + $niceness = $input->getOption('niceness'); + if ($niceness) { + $extensions[] = new NicenessExtension($niceness); + } + return $extensions; } } diff --git a/pkg/enqueue/Tests/Consumption/Extension/NicenessExtensionTest.php b/pkg/enqueue/Tests/Consumption/Extension/NicenessExtensionTest.php new file mode 100644 index 000000000..1cabdaff0 --- /dev/null +++ b/pkg/enqueue/Tests/Consumption/Extension/NicenessExtensionTest.php @@ -0,0 +1,47 @@ +expectException(\InvalidArgumentException::class); + new NicenessExtension('1'); + } + + public function testShouldThrowWarningOnInvalidArgument() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('proc_nice(): Only a super user may attempt to increase the priority of a process'); + + $extension = new NicenessExtension(-1); + $extension->onStart($this->createContext()); + } + + /** + * @return Context + */ + protected function createContext() + { + $context = new Context($this->createMock(PsrContext::class)); + $context->setLogger($this->createMock(LoggerInterface::class)); + $context->setPsrConsumer($this->createMock(PsrConsumer::class)); + $context->setPsrProcessor($this->createMock(PsrProcessor::class)); + + return $context; + } +} diff --git a/pkg/enqueue/Tests/Symfony/Client/ConsumeMessagesCommandTest.php b/pkg/enqueue/Tests/Symfony/Client/ConsumeMessagesCommandTest.php index e87f4df90..396417e2b 100644 --- a/pkg/enqueue/Tests/Symfony/Client/ConsumeMessagesCommandTest.php +++ b/pkg/enqueue/Tests/Symfony/Client/ConsumeMessagesCommandTest.php @@ -61,7 +61,7 @@ public function testShouldHaveExpectedOptions() $options = $command->getDefinition()->getOptions(); - $this->assertCount(7, $options); + $this->assertCount(8, $options); $this->assertArrayHasKey('memory-limit', $options); $this->assertArrayHasKey('message-limit', $options); $this->assertArrayHasKey('time-limit', $options); @@ -69,6 +69,7 @@ public function testShouldHaveExpectedOptions() $this->assertArrayHasKey('idle-timeout', $options); $this->assertArrayHasKey('receive-timeout', $options); $this->assertArrayHasKey('skip', $options); + $this->assertArrayHasKey('niceness', $options); } public function testShouldHaveExpectedArguments() diff --git a/pkg/enqueue/Tests/Symfony/Consumption/ConsumeMessagesCommandTest.php b/pkg/enqueue/Tests/Symfony/Consumption/ConsumeMessagesCommandTest.php index d83505a0c..aad0a293e 100644 --- a/pkg/enqueue/Tests/Symfony/Consumption/ConsumeMessagesCommandTest.php +++ b/pkg/enqueue/Tests/Symfony/Consumption/ConsumeMessagesCommandTest.php @@ -29,12 +29,13 @@ public function testShouldHaveExpectedOptions() $options = $command->getDefinition()->getOptions(); - $this->assertCount(5, $options); + $this->assertCount(6, $options); $this->assertArrayHasKey('memory-limit', $options); $this->assertArrayHasKey('message-limit', $options); $this->assertArrayHasKey('time-limit', $options); $this->assertArrayHasKey('idle-timeout', $options); $this->assertArrayHasKey('receive-timeout', $options); + $this->assertArrayHasKey('niceness', $options); } public function testShouldHaveExpectedAttributes() diff --git a/pkg/enqueue/Tests/Symfony/Consumption/ContainerAwareConsumeMessagesCommandTest.php b/pkg/enqueue/Tests/Symfony/Consumption/ContainerAwareConsumeMessagesCommandTest.php index b451dc712..78929e04a 100644 --- a/pkg/enqueue/Tests/Symfony/Consumption/ContainerAwareConsumeMessagesCommandTest.php +++ b/pkg/enqueue/Tests/Symfony/Consumption/ContainerAwareConsumeMessagesCommandTest.php @@ -33,13 +33,14 @@ public function testShouldHaveExpectedOptions() $options = $command->getDefinition()->getOptions(); - $this->assertCount(6, $options); + $this->assertCount(7, $options); $this->assertArrayHasKey('memory-limit', $options); $this->assertArrayHasKey('message-limit', $options); $this->assertArrayHasKey('time-limit', $options); $this->assertArrayHasKey('queue', $options); $this->assertArrayHasKey('idle-timeout', $options); $this->assertArrayHasKey('receive-timeout', $options); + $this->assertArrayHasKey('niceness', $options); } public function testShouldHaveExpectedAttributes() diff --git a/pkg/enqueue/Tests/Symfony/Consumption/LimitsExtensionsCommandTraitTest.php b/pkg/enqueue/Tests/Symfony/Consumption/LimitsExtensionsCommandTraitTest.php index d9704d360..54952749a 100644 --- a/pkg/enqueue/Tests/Symfony/Consumption/LimitsExtensionsCommandTraitTest.php +++ b/pkg/enqueue/Tests/Symfony/Consumption/LimitsExtensionsCommandTraitTest.php @@ -5,6 +5,7 @@ use Enqueue\Consumption\Extension\LimitConsumedMessagesExtension; use Enqueue\Consumption\Extension\LimitConsumerMemoryExtension; use Enqueue\Consumption\Extension\LimitConsumptionTimeExtension; +use Enqueue\Consumption\Extension\NicenessExtension; use Enqueue\Tests\Symfony\Consumption\Mock\LimitsExtensionsCommand; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Tester\CommandTester; @@ -17,10 +18,11 @@ public function testShouldAddExtensionsOptions() $options = $trait->getDefinition()->getOptions(); - $this->assertCount(3, $options); + $this->assertCount(4, $options); $this->assertArrayHasKey('memory-limit', $options); $this->assertArrayHasKey('message-limit', $options); $this->assertArrayHasKey('time-limit', $options); + $this->assertArrayHasKey('niceness', $options); } public function testShouldAddMessageLimitExtension() @@ -57,7 +59,8 @@ public function testShouldAddTimeLimitExtension() public function testShouldThrowExceptionIfTimeLimitExpressionIsNotValid() { - $this->setExpectedException(\Exception::class, 'Failed to parse time string (time is not valid) at position'); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Failed to parse time string (time is not valid) at position'); $command = new LimitsExtensionsCommand('name'); @@ -104,4 +107,18 @@ public function testShouldAddThreeLimitExtensions() $this->assertInstanceOf(LimitConsumptionTimeExtension::class, $result[1]); $this->assertInstanceOf(LimitConsumerMemoryExtension::class, $result[2]); } + + public function testShouldAddNicenessExtension() + { + $command = new LimitsExtensionsCommand('name'); + $tester = new CommandTester($command); + $tester->execute([ + '--niceness' => 1, + ]); + + $result = $command->getExtensions(); + $this->assertCount(1, $result); + + $this->assertInstanceOf(NicenessExtension::class, $result[0]); + } }