Skip to content

Commit

Permalink
Allow watching specified tubes on workers through the QManConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Van Peer Ilias committed Jan 7, 2016
1 parent 3fbc91c commit 37e22d4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/QManConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace QMan;


use Beanie\Beanie;

class QManConfig
{
/**
Expand Down Expand Up @@ -31,6 +33,11 @@ class QManConfig
*/
const DEFAULT_FAILURE_DELAY = 60;

/**
* Default: the default list of tubes to watch
*/
const DEFAULT_WATCHED_TUBE = Beanie::DEFAULT_TUBE;

/** @var int */
private $maxMemoryUsage = self::DEFAULT_MAX_MEMORY_USAGE;

Expand All @@ -49,6 +56,9 @@ class QManConfig
/** @var int */
private $defaultFailureDelay = self::DEFAULT_FAILURE_DELAY;

/** @var string[] */
private $watchedTubes = [self::DEFAULT_WATCHED_TUBE];

/**
* @return int
*/
Expand Down Expand Up @@ -142,6 +152,24 @@ public function setDefaultFailureDelay($defaultFailureDelay)
return $this;
}

/**
* @return string[]
*/
public function getWatchedTubes()
{
return $this->watchedTubes;
}

/**
* @param string[] $watchedTubes
* @return QManConfig
*/
public function setWatchedTubes($watchedTubes)
{
$this->checkLock()->watchedTubes = $watchedTubes;
return $this;
}

/**
* @return QManConfig
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public function handleJob(BeanieJob $beanieJob)
protected function registerWatchers($workers)
{
array_map(function (\Beanie\Worker $worker) {
$worker->getTubeStatus()->setWatchedTubes(
$this->config->getWatchedTubes()
);

$this->eventLoop->registerJobListener($worker);
}, $workers);

Expand Down
4 changes: 3 additions & 1 deletion tests/QMan/QManConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function testConstruct_assignsDefaults()
$this->assertEquals([QManConfig::DEFAULT_TERMINATION_SIGNAL], $config->getTerminationSignals());
$this->assertEquals(QManConfig::DEFAULT_MAX_TRIES, $config->getMaxTries());
$this->assertEquals(QManConfig::DEFAULT_FAILURE_DELAY, $config->getDefaultFailureDelay());
$this->assertEquals([QManConfig::DEFAULT_WATCHED_TUBE], $config->getWatchedTubes());
}

/**
Expand Down Expand Up @@ -73,7 +74,8 @@ public function propertyValueProvider()
['maxTimeAlive', 100],
['terminationSignals', [SIGUSR1]],
['maxTries', 321],
['defaultFailureDelay', 20]
['defaultFailureDelay', 20],
['watchedTubes', ['test', 'test2']]
];
}
}
22 changes: 20 additions & 2 deletions tests/QMan/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_once 'NativeFunctionStub_TestCase.php';

use Beanie\Beanie;
use Beanie\Tube\TubeStatus;

/**
* Class WorkerTest
Expand Down Expand Up @@ -49,27 +50,44 @@ public function testRun_registersListeners_runsEventLoop_quitsAllWorkers_ignores

$workerMockBuilder = $this
->getMockBuilder(\Beanie\Worker::class)
->setMethods(['quit'])
->setMethods(['getTubeStatus', 'quit'])
->disableOriginalConstructor();

$tubeStatusMock = $this
->getMockBuilder(TubeStatus::class)
->setMethods(['setWatchedTubes'])
->disableOriginalConstructor()
->getMock();

$workers = [
$workerMockBuilder->getMock(),
$workerMockBuilder->getMock(),
$workerMockBuilder->getMock(),
$workerMockBuilder->getMock()
];

$tubeStatusMock
->expects($this->exactly(count($workers)))
->method('setWatchedTubes')
->with([QManConfig::DEFAULT_WATCHED_TUBE])
->willReturn(true);

$beanieMock
->expects($this->once())
->method('workers')
->willReturn($workers);

array_map(function ($workerMock) {
array_map(function ($workerMock) use ($tubeStatusMock) {
/** @var \PHPUnit_Framework_MockObject_MockObject|\Beanie\Worker $workerMock */
$workerMock
->expects($this->once())
->method('quit')
->willThrowException(new \RuntimeException());

$workerMock
->expects($this->once())
->method('getTubeStatus')
->willReturn($tubeStatusMock);
}, $workers);

/** @var \PHPUnit_Framework_MockObject_MockObject|\QMan\EventLoop $eventLoopMock */
Expand Down

0 comments on commit 37e22d4

Please sign in to comment.