Skip to content

Commit

Permalink
Merge pull request #5 from Teknasyon-Teknoloji/master
Browse files Browse the repository at this point in the history
Added isMyJob method to job handler interface - BC Break
  • Loading branch information
javibravo committed Oct 26, 2017
2 parents ede06a2 + 0b66ee7 commit 3e3508a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/
composer.lock
composer.phar
vendor/
logs/
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ The job interface is used to manage the job received in the queue. It must manag
business logic and **define the STOP job**.

The job is abstracted form the queue system, so the same job definition is able to work with
different queues interfaces. The job always receive the message body from the queue,
different queues interfaces. The job always receive the message body from the queue.

If you have different job types ( send mail, crop images, etc. ) and you use one queue, you can define **isMyJob**.
If job is not expected type, you can send back job to queue.

Install
-------
Expand Down Expand Up @@ -135,6 +138,11 @@ class MyJob implements Job {
return FALSE;
}

public function isMyJob($job) {
if ( ... )
return TRUE;
return FALSE;
}
...

}
Expand Down
1 change: 1 addition & 0 deletions src/Simpleue/Job/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
interface Job {
public function manage($job);
public function isStopJob($job);
public function isMyJob($job);
}
2 changes: 1 addition & 1 deletion src/Simpleue/Worker/QueueWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function start()
$this->queueHandler->error(false, $exception);
continue;
}
if ($this->isValidJob($job)) {
if ($this->isValidJob($job) && $this->jobHandler->isMyJob($this->queueHandler->getMessageBody($job))) {
if ($this->jobHandler->isStopJob($this->queueHandler->getMessageBody($job))) {
$this->queueHandler->stopped($job);
$this->log('debug', 'STOP instruction received.');
Expand Down
4 changes: 4 additions & 0 deletions tests/Simpleue/Mocks/JobSpy.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function isStopJob($job) {
return ($job === 'STOP');
}

public function isMyJob($job) {
return ($job !== false);
}

public function setQuitCount($num) {
$this->quitCount = $num;
}
Expand Down
23 changes: 14 additions & 9 deletions tests/Simpleue/Unitary/Worker/QueueWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testRunManageSuccessfulJob() {
$this->assertEquals(0, $this->sourceQueueMock->errorCounter, 'Error counter');
$this->assertEquals(0, $this->sourceQueueMock->nothingToDoCounter, 'Nothing to do counter');
$this->assertEquals(0, $this->sourceQueueMock->stoppedCounter, 'Stop inst. management counter');
$this->assertEquals(20, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
$this->assertEquals(30, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
}

public function testStopInstruction() {
Expand All @@ -62,7 +62,7 @@ public function testStopInstruction() {
$this->assertEquals(0, $this->sourceQueueMock->errorCounter, 'Error counter');
$this->assertEquals(0, $this->sourceQueueMock->nothingToDoCounter, 'Nothing to do counter');
$this->assertEquals(1, $this->sourceQueueMock->stoppedCounter, 'Stop inst. management counter');
$this->assertEquals(5, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
$this->assertEquals(8, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
}

public function testNothingToDo() {
Expand All @@ -73,16 +73,21 @@ public function testNothingToDo() {
$this->sourceQueueMock->expects($this->at(3))->method('getNext')->willReturn(0);
$this->sourceQueueMock->expects($this->at(4))->method('getNext')->willReturn('');

$this->jobHandlerMock = $this->getMock('Simpleue\Mocks\JobSpy', array('isMyJob'));
$this->jobHandlerMock->expects($this->at(0))->method('isMyJob')->willReturn(true);
$this->jobHandlerMock->expects($this->at(1))->method('isMyJob')->willReturn(false);
$this->jobHandlerMock->expects($this->at(2))->method('isMyJob')->willReturn(true);

$this->queueWorkerSpy = new QueueWorkerSpy($this->sourceQueueMock, $this->jobHandlerMock);
$this->queueWorkerSpy->setMaxIterations(5);
$this->queueWorkerSpy->start();
$this->assertEquals(5, $this->queueWorkerSpy->getIterations());
$this->assertEquals(3, $this->sourceQueueMock->successfulCounter, 'Successful counter');
$this->assertEquals(2, $this->sourceQueueMock->successfulCounter, 'Successful counter');
$this->assertEquals(0, $this->sourceQueueMock->failedCounter, 'Failed counter');
$this->assertEquals(0, $this->sourceQueueMock->errorCounter, 'Error counter');
$this->assertEquals(2, $this->sourceQueueMock->nothingToDoCounter, 'Nothing to do counter');
$this->assertEquals(3, $this->sourceQueueMock->nothingToDoCounter, 'Nothing to do counter');
$this->assertEquals(0, $this->sourceQueueMock->stoppedCounter, 'Stop inst. management counter');
$this->assertEquals(6, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
$this->assertEquals(7, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
}

public function testRunManagedFailedJobs() {
Expand All @@ -102,7 +107,7 @@ public function testRunManagedFailedJobs() {
$this->assertEquals(0, $this->sourceQueueMock->errorCounter, 'Error counter');
$this->assertEquals(0, $this->sourceQueueMock->nothingToDoCounter, 'Nothing to do counter');
$this->assertEquals(0, $this->sourceQueueMock->stoppedCounter, 'Stop inst. management counter');
$this->assertEquals(10, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
$this->assertEquals(15, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
}

public function testHandlerManageExceptions() {
Expand All @@ -121,7 +126,7 @@ public function testHandlerManageExceptions() {
$this->assertEquals(2, $this->sourceQueueMock->errorCounter, 'Error counter');
$this->assertEquals(0, $this->sourceQueueMock->nothingToDoCounter, 'Nothing to do counter');
$this->assertEquals(0, $this->sourceQueueMock->stoppedCounter, 'Stop inst. management counter');
$this->assertEquals(8, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
$this->assertEquals(12, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
}

public function testSourceQueueGetNextExceptions() {
Expand All @@ -139,7 +144,7 @@ public function testSourceQueueGetNextExceptions() {
$this->assertEquals(2, $this->sourceQueueMock->errorCounter, 'Error counter');
$this->assertEquals(0, $this->sourceQueueMock->nothingToDoCounter, 'Nothing to do counter');
$this->assertEquals(0, $this->sourceQueueMock->stoppedCounter, 'Stop inst. management');
$this->assertEquals(2, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
$this->assertEquals(3, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
}

public function testSourceQueueSuccessfulAndFailedExceptions() {
Expand All @@ -161,7 +166,7 @@ public function testSourceQueueSuccessfulAndFailedExceptions() {
$this->assertEquals(2, $this->sourceQueueMock->errorCounter, 'Error counter');
$this->assertEquals(0, $this->sourceQueueMock->nothingToDoCounter, 'Nothing to do counter');
$this->assertEquals(0, $this->sourceQueueMock->stoppedCounter, 'Stop inst. management counter');
$this->assertEquals(8, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
$this->assertEquals(12, $this->sourceQueueMock->getMessageBodyCounter, 'Message body counter');
}

public function testWorkerExitsGracefullyOnSigINT() {
Expand Down

0 comments on commit 3e3508a

Please sign in to comment.