Skip to content

Commit

Permalink
Adding a really basic example implementation
Browse files Browse the repository at this point in the history
Finally have the (extremely) basic version working
  • Loading branch information
justinrainbow committed Jul 22, 2012
1 parent b482871 commit 5c2ffca
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 8 deletions.
15 changes: 15 additions & 0 deletions example/Presque/Example/SimpleJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Presque\Example;

class SimpleJob
{
public function perform($path)
{
file_put_contents(
$path,
sprintf('Run at %s!', date('Y-m-d H:i:s')) . PHP_EOL,
FILE_APPEND
);
}
}
20 changes: 20 additions & 0 deletions example/cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env php
<?php

$loader = require_once __DIR__.'/../vendor/autoload.php';
$loader->add('Presque\\Example', __DIR__);

$connection = new Predis\Client('tcp://maureen.local:6379');

$storage = new Presque\Storage\PredisStorage($connection, 'presque');

$queue = new Presque\Queue('example', $storage);

$worker = new Presque\Worker();
$worker->addQueue($queue);

if (count($argv) > 1) {
$queue->enqueue(Presque\Job::create('Presque\Example\SimpleJob', array($argv[1])));
} else {
$worker->start();
}
4 changes: 4 additions & 0 deletions src/Presque/Storage/PredisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function push($listName, $payload)
public function pop($listName, $waitTimeout = null)
{
if ($payload = $this->connection->blpop($this->getKey($listName), $waitTimeout)) {
if (is_array($payload)) {
return json_decode($payload[1], true);
}

return json_decode($payload, true);
}

Expand Down
49 changes: 48 additions & 1 deletion src/Presque/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class Worker implements WorkerInterface
{
private $id;
private $queues;
private $status;

public function __construct($id)
public function __construct($id = null)
{
$this->id = $id;
}
Expand All @@ -45,6 +46,31 @@ public function getQueues()
return $this->queues;
}

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

public function getEventDispatcher()
{
return $this->eventDispatcher;
}

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

public function setStatus($status)
{
$this->status = $status;
}

public function getStatus()
{
return $this->status;
}

public function isRunning()
{
return $this->getStatus() === StatusInterface::RUNNING;
Expand All @@ -55,6 +81,13 @@ public function isDying()
return $this->getStatus() === StatusInterface::DYING;
}

public function start()
{
$this->setStatus(StatusInterface::RUNNING);

$this->run();
}

public function run()
{
while ($this->isRunning()) {
Expand All @@ -70,6 +103,20 @@ public function run()

protected function process(QueueInterface $queue)
{
$job = $queue->reserve();

if (!$job instanceof JobInterface) {
return;
}

$job->perform();

if ($job->isSuccessful()) {
return;
}

if ($job->isError()) {

}
}
}
8 changes: 1 addition & 7 deletions src/Presque/WorkerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,5 @@ function isRunning();

function isDying();

function start();

function pause();

function stop();

function abort();
function run();
}

0 comments on commit 5c2ffca

Please sign in to comment.