Skip to content

Commit

Permalink
Tests now have 100% code coverage
Browse files Browse the repository at this point in the history
More events are being added, too.
  • Loading branch information
justinrainbow committed Jul 22, 2012
1 parent 5f3c12e commit 1d9f7f5
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 53 deletions.
11 changes: 11 additions & 0 deletions src/Presque/AbstractJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public function getStatus()
return $this->status;
}

public function prepare()
{
$this->lastResult = $this->lastError = null;

$this->setStatus(StatusInterface::RUNNING);
}

public function complete()
{
}

/**
* {@inheritDoc}
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Presque/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,26 @@

class Event extends SymfonyEvent
{
protected $defaultPrevented = false;

public function halt()
{
$this->stopEvent();
}

public function stopEvent()
{
$this->stopPropagation();
$this->preventDefault();
}

public function preventDefault()
{
$this->defaultPrevented = true;
}

public function isCanceled()
{
return $this->defaultPrevented === true;
}
}
18 changes: 3 additions & 15 deletions src/Presque/Event/JobEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
class JobEvent extends Event
{
private $worker;
private $canceled;

public function __construct(JobInterface $job, QueueInterface $queue, WorkerInterface $worker)
{
$this->job = $job;
$this->queue = $queue;
$this->worker = $worker;
$this->canceled = false;
$this->job = $job;
$this->queue = $queue;
$this->worker = $worker;
}

public function getJob()
Expand All @@ -49,14 +47,4 @@ public function getWorker()
{
return $this->worker;
}

public function cancel()
{
$this->canceled = true;
}

public function isCanceled()
{
return $this->canceled;
}
}
12 changes: 0 additions & 12 deletions src/Presque/Event/WorkerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,14 @@
class WorkerEvent extends Event
{
private $worker;
private $canceled;

public function __construct(WorkerInterface $worker)
{
$this->worker = $worker;
$this->canceled = false;
}

public function getWorker()
{
return $this->worker;
}

public function cancel()
{
$this->canceled = true;
}

public function isCanceled()
{
return $this->canceled;
}
}
2 changes: 2 additions & 0 deletions src/Presque/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ final class Events
const WORK_STOPPED = 'presque.work.stopped';

const JOB_STARTED = 'presque.job.started';

const JOB_FINISHED = 'presque.job.finished';
}
9 changes: 5 additions & 4 deletions src/Presque/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ public function getInstance()
*/
public function perform()
{
$this->lastResult = $this->lastError = null;

$this->setStatus(StatusInterface::RUNNING);

try {
$this->lastResult = $this->reflMethod->invokeArgs(
$this->getInstance(),
Expand All @@ -136,4 +132,9 @@ public function perform()

return $this;
}

public function __toString()
{
return (string) $this->getClass();
}
}
4 changes: 4 additions & 0 deletions src/Presque/JobInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ function isActive();
*/
function perform();

function prepare();

function complete();

/**
* Returns an instance of the `class`
*
Expand Down
7 changes: 7 additions & 0 deletions src/Presque/Log/LoggerAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ interface LoggerAwareInterface
* @param LoggerInterface $logger
*/
function setLogger(LoggerInterface $logger = null);

/**
* Checks if a LoggerInterface is available.
*
* @return Boolean
*/
function hasLogger();
}
53 changes: 43 additions & 10 deletions src/Presque/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Worker implements WorkerInterface, LoggerAwareInterface
private $queues;
private $status;
private $eventDispatcher;
private $logger;

public function __construct($id = null)
{
Expand Down Expand Up @@ -92,6 +93,14 @@ public function setLogger(LoggerInterface $logger = null)
$this->logger = $logger;
}

/**
* {@inheritDoc}
*/
public function hasLogger()
{
return null !== $this->logger;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -141,9 +150,16 @@ public function start()

$this->run();

if ($this->hasEventDispatcher()) {
$this->eventDispatcher->dispatch(Events::WORK_STOPPED, new WorkerEvent($this));
}

$this->setStatus(StatusInterface::STOPPED);
}

/**
* Starts the shutdown process for this worker
*/
public function stop()
{
$this->setStatus(StatusInterface::STOPPING);
Expand All @@ -155,12 +171,22 @@ public function stop()
public function run()
{
while ($this->isRunning()) {
foreach ($this->getQueues() as $queue) {
$this->process($queue);
$this->runLoop();
}
}

if ($this->isDying() || !$this->isRunning()) {
return;
}
/**
* Runs a single pass through all the registered queues.
*
* Each QueueInterface is processed in the order it was added.
*/
public function runLoop()
{
foreach ($this->getQueues() as $queue) {
$this->process($queue);

if ($this->isDying() || !$this->isRunning()) {
break;
}
}
}
Expand All @@ -178,6 +204,15 @@ protected function process(QueueInterface $queue)
return;
}

if ($this->hasLogger()) {
$this->logger->log(
sprintf('Starting job "%s"', $job),
LOG_DEBUG
);
}

$job->prepare();

if ($this->hasEventDispatcher()) {
$event = $this->eventDispatcher->dispatch(Events::JOB_STARTED, new JobEvent($job, $queue, $this));

Expand All @@ -190,12 +225,10 @@ protected function process(QueueInterface $queue)

$job->perform();

if ($job->isSuccessful()) {
return;
if ($this->hasEventDispatcher()) {
$this->eventDispatcher->dispatch(Events::JOB_FINISHED, new JobEvent($job, $queue, $this));
}

if ($job->isError()) {

}
$job->complete();
}
}
22 changes: 22 additions & 0 deletions tests/Presque/Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@

namespace Presque\Tests;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Mockery as m;

abstract class TestCase extends \PHPUnit_Framework_TestCase
{
protected function createLoggerMock()
{
return m::mock('Presque\Log\LoggerInterface');
}

protected function createEventDispatcher()
{
return new EventDispatcher();
}

protected function createQueueMock()
{
return m::mock('Presque\QueueInterface');
}

protected function createJobMock()
{
return m::mock('Presque\Job');
}
}
Loading

0 comments on commit 1d9f7f5

Please sign in to comment.