Skip to content

Commit

Permalink
Rearranging namespaces to prep for factories
Browse files Browse the repository at this point in the history
  • Loading branch information
justinrainbow committed Jul 23, 2012
1 parent fe3393e commit 1d6d72c
Show file tree
Hide file tree
Showing 16 changed files with 251 additions and 122 deletions.
6 changes: 3 additions & 3 deletions src/Presque/Event/JobEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

namespace Presque\Event;

use Presque\JobInterface;
use Presque\QueueInterface;
use Presque\WorkerInterface;
use Presque\Job\JobInterface;
use Presque\Queue\QueueInterface;
use Presque\Worker\WorkerInterface;

class JobEvent extends Event
{
Expand Down
2 changes: 1 addition & 1 deletion src/Presque/Event/WorkerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Presque\Event;

use Presque\WorkerInterface;
use Presque\Worker\WorkerInterface;

class WorkerEvent extends Event
{
Expand Down
35 changes: 35 additions & 0 deletions src/Presque/EventListener/JobRetryListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Presque\Events;
use Presque\Event\JobEvent;

class JobRetryListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
Events::JOB_FINISHED => 'onJobFinished'
);
}

public function onJobFinished(JobEvent $event)
{
$job = $event->getJob();

if ($job->isError()) {
$event->getQueue()->enqueue($job);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Presque;
namespace Presque\Job;

use Presque\StatusInterface;

Expand Down
11 changes: 10 additions & 1 deletion src/Presque/Job.php → src/Presque/Job/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
* file that was distributed with this source code.
*/

namespace Presque;
namespace Presque\Job;

use Presque\Exception\InvalidArgumentException;
use Presque\StatusInterface;

class Job extends AbstractJob
{
Expand All @@ -33,6 +34,14 @@ public static function create($class, array $args = array())
return new static($class, $args);
}

/**
* {@inheritDoc}
*/
public static function recreate(array $payload)
{
return new static($payload['class'], $payload['args']);
}

/**
* @param string $class Class that will be performing
* @param array $args List of arguments to pass to the `$class->perform()` method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Presque;
namespace Presque\Job;

interface JobInterface
{
Expand All @@ -21,6 +21,17 @@ interface JobInterface
*/
static function create($class, array $args = array());

/**
* Recreates a Job instance from the payload data.
*
* @param array $payload
*
* @return JobInterface
*
* @throws InvalidArgumentException If `$payload` is not valid
*/
static function recreate(array $payload);

/**
* @return Boolean
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Presque/Queue/AbstractQueue.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\Queue;

use Presque\Storage\StorageInterface;
use Presque\Job\JobInterface;
use Presque\Job\Job;

abstract class AbstractQueue implements QueueInterface
{
}
26 changes: 23 additions & 3 deletions src/Presque/Queue.php → src/Presque/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
* file that was distributed with this source code.
*/

namespace Presque;
namespace Presque\Queue;

use Presque\Storage\StorageInterface;
use Presque\Job\JobInterface;
use Presque\Job\Job;

class Queue implements QueueInterface
class Queue extends AbstractQueue
{
protected $name;
protected $storage;
Expand All @@ -26,26 +28,41 @@ public function __construct($name, StorageInterface $storage = null, $timeout =
$this->timeout = $timeout;
}

/**
* {@inheritDoc}
*/
public function getName()
{
return $this->name;
}

/**
* {@inheritDoc}
*/
public function getTimeout()
{
return $this->timeout;
}

/**
* {@inheritDoc}
*/
public function getStorage()
{
return $this->storage;
}

/**
* {@inheritDoc}
*/
public function setStorage(StorageInterface $storage)
{
$this->storage = $storage;
}

/**
* {@inheritDoc}
*/
public function enqueue(JobInterface $job)
{
$this->storage->push($this->name, array(
Expand All @@ -54,6 +71,9 @@ public function enqueue(JobInterface $job)
));
}

/**
* {@inheritDoc}
*/
public function reserve()
{
$payload = $this->storage->pop($this->name, $this->getTimeout());
Expand All @@ -62,6 +82,6 @@ public function reserve()
return false;
}

return Job::create($payload['class'], $payload['args']);
return Job::recreate($payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
* file that was distributed with this source code.
*/

namespace Presque;
namespace Presque\Queue;

use Presque\Storage\StorageInterface;
use Presque\Job\JobInterface;

interface QueueInterface
{
Expand Down
123 changes: 123 additions & 0 deletions src/Presque/Worker/AbstractWorker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?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;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Presque\Event\JobEvent;
use Presque\Event\WorkerEvent;
use Presque\Log\LoggerAwareInterface;
use Presque\Log\LoggerInterface;
use Presque\Queue\QueueInterface;
use Presque\Job\JobInterface;
use Presque\StatusInterface;
use Presque\Events;

abstract class AbstractWorker implements WorkerInterface, LoggerAwareInterface
{
protected $id;
protected $status;
protected $queues;
protected $eventDispatcher;
protected $logger;

public function __construct($id = null)
{
if (null === $id) {
$id = gethostname() . ':' . getmypid();
}

$this->id = $id;
$this->queues = new \SplObjectStorage();
}

/**
* {@inheritDoc}
*/
public function getId()
{
return $this->id;
}

/**
* {@inheritDoc}
*/
public function setStatus($status)
{
$this->status = $status;
}

/**
* {@inheritDoc}
*/
public function getStatus()
{
return $this->status;
}

/**
* {@inheritDoc}
*/
public function addQueue(QueueInterface $queue)
{
$this->queues->attach($queue);
}

/**
* {@inheritDoc}
*/
public function removeQueue(QueueInterface $queue)
{
if ($this->queues->contains($queue)) {
$this->queues->detach($queue);
}
}

/**
* {@inheritDoc}
*/
public function getQueues()
{
return $this->queues;
}

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

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

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

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

0 comments on commit 1d6d72c

Please sign in to comment.