rybakit/phive-task-queue

A lightweight task queue on top of rybakit/phive-queue

Fund package maintenance!
rybakit

v0.3.0 2014-08-05 08:15 UTC

This package is auto-updated.

Last update: 2024-10-10 12:43:04 UTC


README

Build Status

A job/task queue on top of the Phive Queue.

Installation

The recommended way to install Phive Task Queue is through Composer:

$ composer require rybakit/phive-task-queue:~1.0@dev

Usage example

// worker.php

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Phive\Queue\SysVQueue;
use Phive\TaskQueue\ExecutionContext;
use Phive\TaskQueue\Executor;
use Phive\TaskQueue\ExecutorAdapter\CallableExecutorAdapter;

// create a queue
// see a list of available queues: https://github.com/rybakit/phive-queue#queues
$queue = new SysVQueue(0xDEADBEAF, true);

// create a logger
// can be any PSR-3 compliant logger
$logger = new Logger('worker');
$logger->pushHandler(new StreamHandler(STDOUT, Logger::INFO));

// create a callback
$callback = function ($payload, LoggerInterface $logger) {
    $logger->info(strrev($payload));
};

$adapter = new CallbackExecutorAdapter(new DirectCallbackResolver($callback));
$context = new ExecutionContext($queue, $logger);
$executor = new Executor($adapter, $context);

// main loop
while (true) {
    if (!$executor->execute()) {
        sleep(1);
    }
}
// client.php

use Phive\Queue\SysVQueue;

$queue = new SysVQueue(0xDEADBEAF, true);

// send a payload object to the queue and delay execution for 5 seconds
// see supported item types: https://github.com/rybakit/phive-queue#item-types
$queue->push('Hello world!', '+5 seconds');

License

Phive Task Queue is released under the MIT License. See the bundled LICENSE file for details.