2017 © Pedro Peláez
 

library queue

PHP bindings for Tarantool Queue.

image

tarantool/queue

PHP bindings for Tarantool Queue.

  • Thursday, April 19, 2018
  • by rybakit
  • Repository
  • 6 Watchers
  • 45 Stars
  • 35,839 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 14 % Grown

The README.md

Tarantool Queue

Quality Assurance Scrutinizer Code Quality Code Coverage Mentioned in Awesome PHP Telegram, (*1)

Tarantool is a NoSQL database running in a Lua application server. It integrates Lua modules, called LuaRocks. This package provides PHP bindings for Tarantool Queue LuaRock., (*2)

Table of contents

Installation

The recommended way to install the library is through Composer:, (*3)

composer require tarantool/queue

Before start

In order to use queue, you first need to make sure that your Tarantool instance is configured, up and running. The minimal required configuration might look like this:, (*4)

-- queues.lua

box.cfg {listen = 3301}

queue = require('queue')
queue.create_tube('foobar', 'fifottl', {if_not_exists = true})

You can read more about the box configuration in the official Tarantool documentation. More information on queue configuration can be found here., (*5)

To start the instance you need to copy (or symlink) queues.lua file into the /etc/tarantool/instances.enabled directory and run the following command:, (*6)

sudo tarantoolctl start queues

Working with queue

Once you have your instance running, you can start by creating a queue object with the queue (tube) name you defined in the Lua script:, (*7)

use Tarantool\Queue\Queue;

...

$queue = new Queue($client, 'foobar');

where $client is an instance of Tarantool\Client\Client from the tarantool/client package., (*8)

Data types

Under the hood Tarantool uses MessagePack binary format to serialize/deserialize data being stored in a queue. It can handle most of the PHP data types (except resources and closures) without any manual pre- or post-processing:, (*9)

$queue->put('foo');
$queue->put(true);
$queue->put(42);
$queue->put(4.2);
$queue->put(['foo' => ['bar' => ['baz' => null]]]);
$queue->put(new MyObject());

To learn more about object serialization, please follow this link., (*10)

Tasks

Most of the Queue API methods return a Task object containing the following getters:, (*11)

Task::getId()
Task::getState() // States::READY, States::TAKEN, States::DONE, States::BURY or States::DELAYED
Task::getData()

And some sugar methods:, (*12)

Task::isReady()
Task::isTaken()
Task::isDone()
Task::isBuried()
Task::isDelayed()

Producer API

As you've already seen, to insert a task into a queue you need to call put() method, which accepts two arguments: the data you want to process and optional array of task options, which this particular queue supports. For example, fifottl queue (which we defined earlier in our Lua config file), supports delay, ttl, ttr and pri options:, (*13)

use Tarantool\Queue\Options;

$queue->put('foo', [Options::DELAY => 30.0]);
$queue->put('bar', [Options::TTL => 5.0]);
$queue->put('baz', [Options::TTR => 10.0, Options::PRI => 42]);

See the full list of available options here., (*14)

Consumer API

To reserve a task for execution, call take() method. It accepts an optional timeout parameter. If a timeout value is supplied the call will wait timeout seconds until a READY task appears in the queue. The method returns either a Task object or null:, (*15)

$taskOrNull = $queue->take();

// wait 2 seconds
$taskOrNull = $queue->take(2.0);

// wait 100 milliseconds
$taskOrNull = $queue->take(.1);

After successful execution, a task can be marked as acknowledged (that will also delete the task from a queue):, (*16)

$data = $task->getData();

// process $data

$task = $queue->ack($task->getId());

Or put back into the queue in case it cannot be executed:, (*17)

$task = $queue->release($task->getId());

// for *ttl queues you can specify a delay
$task = $queue->release($task->getId(), [Options::DELAY => 30.0]);

To look at a task without changing its state, use:, (*18)

$task = $queue->peek($task->getId());

To bury (disable) a task:, (*19)

$task = $queue->bury($task->getId());

To reset buried task(s) back to READY state:, (*20)

$count = $queue->kick(3); // kick 3 buried tasks

To increase TTR and/or TTL of a running task (only for *ttl queues):, (*21)

$taskOrNull = $queue->touch($takenTask->getId(), 5.0); // increase ttr/ttl to 5 seconds

A task (in any state) can be deleted permanently with delete():, (*22)

$task = $queue->delete($task->getId());

To delete all tasks in a queue:, (*23)

$queue->truncate();

For a detailed API documentation, please read the section "Using the queue module" of the queue README., (*24)

Statistics

The stats() method provides access to the statistical information accumulated since a queue was created:, (*25)

$stats = $queue->stats();

The result of this call might look like this:, (*26)

[
    'tasks' => [
        'taken'   => 1,
        'buried'  => 1,
        'ready'   => 1,
        'done'    => 0,
        'delayed' => 0,
        'total'   => 3,
    ],
    'calls' => [
        'bury' => 1,
        'put'  => 3,
        'take' => 1,
        ...
    ],
]

In addition, you can specify a key to return only a subset of the array:, (*27)

$calls = $queue->stats('calls');
$total = $queue->stats('tasks.total');

Custom methods

Thanks to flexible nature of the queue Lua module, you can easily create your own queue drivers or extend existing ones with an additional functionality. For example, suppose you added the put_many method to your foobar queue, which inserts multiple tasks atomically:, (*28)

-- queues.lua

...

queue.tube.foobar.put_many = function(self, items)
    local put = {}

    box.begin()
    for k, item in pairs(items) do
        put[k] = tube:put(unpack(item))
    end
    box.commit()

    return put
end

To invoke this method from php, use Queue::call():, (*29)

$result = $queue->call('put_many', [
    'foo' => ['foo', [Options::DELAY => 30.0]],
    'bar' => ['bar'],
]);

Testing

The easiest way to run tests is with Docker. First, build an image using the dockerfile.sh generator:, (*30)

./dockerfile.sh | docker build -t queue -

Then run a Tarantool instance (needed for integration tests):, (*31)

docker network create tarantool-php
docker run -d --net=tarantool-php -p 3301:3301 --name=tarantool \
    -v $(pwd)/tests/Integration/queues.lua:/queues.lua \
    tarantool/tarantool:2 tarantool /queues.lua

And then run both unit and integration tests:, (*32)

docker run --rm --net=tarantool-php -v $(pwd):/queue -w /queue queue

The library uses PHPUnit under the hood, and if needed, you can pass additional arguments and options to the phpunit command. For example, to run only unit tests, execute:, (*33)

docker run --rm --net=tarantool-php -v $(pwd):/queue -w /queue queue \
    vendor/bin/phpunit --testsuite=unit

License

The library is released under the MIT License. See the bundled LICENSE file for details., (*34)

The Versions

19/04 2018

dev-master

9999999-dev

PHP bindings for Tarantool Queue.

  Sources   Download

MIT

The Requires

  • php ^5.4|^7.0

 

The Development Requires

by Eugene Leonovich

task queue nosql job worker schedule priority ttl delayed tarantool ttr

06/02 2018

v0.6.1

0.6.1.0

PHP bindings for Tarantool Queue.

  Sources   Download

MIT

The Requires

  • php ^5.4|^7.0

 

The Development Requires

by Eugene Leonovich

task queue nosql job worker schedule priority ttl delayed tarantool ttr

05/02 2018

v0.6.0

0.6.0.0

PHP bindings for Tarantool Queue.

  Sources   Download

MIT

The Requires

  • php ^5.4|^7.0

 

The Development Requires

by Eugene Leonovich

task queue nosql job worker schedule priority ttl delayed tarantool ttr

17/07 2017

v0.5.0

0.5.0.0

PHP bindings for Tarantool Queue.

  Sources   Download

MIT

The Requires

  • php ^5.4|^7.0

 

The Development Requires

by Eugene Leonovich

task queue nosql job worker schedule priority ttl delayed tarantool ttr

05/04 2017

v0.4.0

0.4.0.0

PHP bindings for Tarantool Queue.

  Sources   Download

MIT

The Requires

  • php ^5.4|^7.0

 

The Development Requires

by Eugene Leonovich

task queue nosql job worker schedule priority ttl delayed tarantool ttr

12/10 2016

dev-handler

dev-handler

PHP bindings for Tarantool Queue.

  Sources   Download

MIT

The Requires

  • php ^5.4|^7.0

 

The Development Requires

by Eugene Leonovich

task queue job worker schedule priority ttl delayed tarantool ttr

30/06 2016

v0.3.0

0.3.0.0

PHP bindings for Tarantool Queue.

  Sources   Download

MIT

The Requires

  • php ^5.4|^7.0
  • ext-tarantool *

 

by Eugene Leonovich

task queue job worker schedule priority ttl delayed tarantool ttr

11/09 2015

v0.2.1

0.2.1.0

PHP bindings for Tarantool Queue.

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-tarantool *

 

by Eugene Leonovich

task queue job schedule priority ttl delayed tarantool ttr

06/07 2015

v0.2.0

0.2.0.0

PHP bindings for Tarantool Queue.

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-tarantool *

 

by Eugene Leonovich

task queue job schedule priority ttl delayed tarantool ttr

24/06 2015

v0.1.0

0.1.0.0

PHP bindings for Tarantool Queue.

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-tarantool *

 

by Eugene Leonovich

task queue job schedule priority ttl delayed tarantool ttr

22/06 2015

dev-travis

dev-travis

PHP bindings for Tarantool Queue.

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-tarantool *

 

by Eugene Leonovich

task queue job schedule priority ttl delayed tarantool ttr