Skip to content

Commit

Permalink
The main Presque class is now used as a factory
Browse files Browse the repository at this point in the history
The Presque class can be used to create all the Worker / Job / Queue
instances.  There are factory objects for each of those 3 types.  Each
of the factory objects can be injected to the Presque constructor,
allowing for lots of possibilities.
  • Loading branch information
justinrainbow committed Jul 23, 2012
1 parent 1d6d72c commit 99b3e15
Show file tree
Hide file tree
Showing 11 changed files with 432 additions and 2 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ library, but does not aim to be completely compatible.

Presque is still under heavy development and is not ready for production use.

## Creating your first Job

```php
<?php

namespace Acme\Queue;

class WidgetJob
{
/**
* @param string $group
*/
public function perform($group)
{

}
}
```

Once the basic class has been created, all you need to do is add the job to
the queue.

```console
presque queue 'Acme\Queue\WidgetJob' 'mygroup'
```

## Roadmap

* Make the Workers fork-able - so Presque can be ran as a daemon
Expand Down
20 changes: 20 additions & 0 deletions src/Presque/Job/JobFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Job;

class JobFactory implements JobFactoryInterface
{
public function create($class, array $args = array())
{
return new Job($class, $args);
}
}
17 changes: 17 additions & 0 deletions src/Presque/Job/JobFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Job;

interface JobFactoryInterface
{
function create($class, array $args = array());
}
135 changes: 133 additions & 2 deletions src/Presque/Presque.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,138 @@

namespace Presque;

class Presque
use Presque\Worker\WorkerFactory;
use Presque\Worker\WorkerFactoryInterface;
use Presque\Queue\QueueFactory;
use Presque\Queue\QueueFactoryInterface;
use Presque\Job\JobFactory;
use Presque\Job\JobFactoryInterface;
use Presque\Event\EventDispatcherAwareInterface;
use Presque\Log\LoggerAwareInterface;
use Presque\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class Presque implements EventDispatcherAwareInterface, LoggerAwareInterface
{
const VERSION = "0.1.0";
const VERSION = "0.1.0";

private static $eventSubscribers = array(
'Presque\EventListener\JobRetryListener'
);

protected $workerFactory;
protected $queueFactory;
protected $jobFactory;
protected $dispatcher;
protected $logger;

public function __construct(
WorkerFactoryInterface $workerFactory = null,
QueueFactoryInterface $queueFactory = null,
JobFactoryInterface $jobFactory = null,
EventDispatcherInterface $dispatcher = null,
LoggerInterface $logger = null
)
{
if (null === $dispatcher) {
$dispatcher = new EventDispatcher();
foreach (static::$eventSubscribers as $class) {
$dispatcher->addSubscriber(new $class());
}
}

$this->workerFactory = $workerFactory ?: new WorkerFactory();
$this->queueFactory = $queueFactory ?: new QueueFactory();
$this->jobFactory = $jobFactory ?: new JobFactory();
$this->dispatcher = $dispatcher;
$this->logger = $logger;
}

/**
* @see Presque\Worker\WorkerFactoryInterface::create
*/
public function createWorker($id = null)
{
return $this->injectServices(
$this->workerFactory->create($id)
);
}

/**
* @see Presque\Queue\QueueFactoryInterface::create
*/
public function createQueue($name)
{
return $this->injectServices(
$this->queueFactory->create($name)
);
}

/**
* @see Presque\Job\JobFactoryInterface::create
*/
public function createJob($class, array $args = array())
{
return $this->injectServices(
$this->jobFactory->create($class, $args)
);
}

/**
* {@inheritDoc}
*/
public function setEventDispatcher(EventDispatcherInterface $dispatcher = null)
{
$this->dispatcher = $dispatcher;
}

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

/**
* {@inheritDoc}
*/
public function setLogger(LoggerInterface $logger = null)
{
$this->logger = $logger;
}

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

/**
* Injects instances of the EventDispatcher and Logger to objects that implement
* the EventDispatcherAwareInterface or the LoggerAwareInterface.
*
* @param mixed $object
*
* @return mixed $object injected with the services
*/
protected function injectServices($object)
{
if ($this->hasEventDispatcher()) {
if ($object instanceof EventDispatcherAwareInterface) {
$object->setEventDispatcher($this->dispatcher);
}
}

if ($this->hasLogger()) {
if ($object instanceof LoggerAwareInterface) {
$object->setLogger($this->logger);
}
}

return $object;
}
}
22 changes: 22 additions & 0 deletions src/Presque/Queue/QueueFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Queue;

use Presque\Storage\StorageInterface;

class QueueFactory implements QueueFactoryInterface
{
public function create($name, StorageInterface $storage = null, $timeout = 10)
{
return new Queue($name, $storage, $timeout);
}
}
17 changes: 17 additions & 0 deletions src/Presque/Queue/QueueFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Queue;

interface QueueFactoryInterface
{
function create($name);
}
20 changes: 20 additions & 0 deletions src/Presque/Worker/WorkerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Worker;

class WorkerFactory implements WorkerFactoryInterface
{
public function create($id = null)
{
return new Worker($id);
}
}
17 changes: 17 additions & 0 deletions src/Presque/Worker/WorkerFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Worker;

interface WorkerFactoryInterface
{
function create($id = null);
}
40 changes: 40 additions & 0 deletions tests/Presque/Tests/EventListener/JobRetryListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Tests\EventListener;

use Presque\Tests\TestCase;
use Presque\Worker\Worker;
use Presque\Events;
use Presque\Event\JobEvent;
use Presque\EventListener\JobRetryListener;
use Mockery as m;

class JobRetryListenerTest extends TestCase
{
public function testFailedJobIsAddedBackToQueue()
{
$worker = $this->createWorkerMock();

$job = $this->createJobMock();
$job
->shouldReceive('isError')->once()->andReturn(true);

$queue = $this->createQueueMock();
$queue
->shouldReceive('enqueue')->once()->with($job);

$event = new JobEvent($job, $queue, $worker);

$listener = new JobRetryListener();
$listener->onJobFinished($event);
}
}
Loading

0 comments on commit 99b3e15

Please sign in to comment.