-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #467 from ramunasd/extension_niceness
[consumption] add process niceness extension
- Loading branch information
Showing
7 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Enqueue\Consumption\Extension; | ||
|
||
use Enqueue\Consumption\Context; | ||
use Enqueue\Consumption\EmptyExtensionTrait; | ||
use Enqueue\Consumption\ExtensionInterface; | ||
|
||
class NicenessExtension implements ExtensionInterface | ||
{ | ||
use EmptyExtensionTrait; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $niceness = 0; | ||
|
||
/** | ||
* @param int $niceness | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __construct($niceness) | ||
{ | ||
if (false === is_int($niceness)) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'Expected niceness value is int but got: "%s"', | ||
is_object($niceness) ? get_class($niceness) : gettype($niceness) | ||
)); | ||
} | ||
|
||
$this->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'] | ||
)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
pkg/enqueue/Tests/Consumption/Extension/NicenessExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Enqueue\Tests\Consumption\Extension; | ||
|
||
use Enqueue\Consumption\Context; | ||
use Enqueue\Consumption\Extension\NicenessExtension; | ||
use Interop\Queue\PsrConsumer; | ||
use Interop\Queue\PsrContext; | ||
use Interop\Queue\PsrProcessor; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class NicenessExtensionTest extends TestCase | ||
{ | ||
public function testCouldBeConstructedWithRequiredArguments() | ||
{ | ||
new NicenessExtension(0); | ||
} | ||
|
||
public function testShouldThrowExceptionOnInvalidArgument() | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters