Skip to content

Commit d1d16ca

Browse files
author
Eugene Leonovich
committed
Update README.md
1 parent 2d7bb94 commit d1d16ca

File tree

1 file changed

+188
-4
lines changed

1 file changed

+188
-4
lines changed

README.md

Lines changed: 188 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/tarantool-php/queue/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/tarantool-php/queue/?branch=master)
55
[![Code Coverage](https://scrutinizer-ci.com/g/tarantool-php/queue/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/tarantool-php/queue/?branch=master)
66

7+
[Tarantool](http://tarantool.org/) is a NoSQL database running in a Lua application server. It integrates
8+
Lua modules, called [LuaRocks](https://luarocks.org/). This package provides PHP bindings for
9+
[Tarantool Queue LuaRock](https://github.com/tarantool/queue/blob/master/).
10+
711

812
## Installation
913

@@ -14,20 +18,198 @@ $ composer require tarantool/queue
1418
```
1519

1620

17-
## Usage example
21+
## Before start
22+
23+
In order to use queue, you first need to make sure that your Tarantool instance
24+
is configured, up and running. The minimal required configuration might look like this:
25+
26+
```lua
27+
-- queues.lua
28+
29+
box.cfg {}
30+
31+
queue = require('queue')
32+
queue.start()
33+
34+
local tube_name = 'foobar'
35+
if null == queue.tube[tube_name] then
36+
queue.create_tube(tube_name, 'fifottl')
37+
end
38+
```
39+
40+
> You can read more about the box configuration in the official [Tarantool documentation](http://tarantool.org/doc/book/configuration/index.html#initialization-file).
41+
> For more information about the queue configuration check out [queue's README](https://github.com/tarantool/queue/blob/master/README.md).
42+
43+
To start the instance you need to copy (or symlink) `queues.lua` file into the `/etc/tarantool/instances.enabled`
44+
directory and run the following command:
45+
46+
```sh
47+
$ sudo tarantoolctl start queues
48+
```
49+
50+
51+
## Working with queue
52+
53+
Once you have your instance running, you can start by creating a queue object with the queue (tube) name you defined
54+
in the Lua script:
1855

1956
```php
2057
use Tarantool\Queue\Queue;
2158

2259
$tarantool = new Tarantool();
23-
$queue = new Queue($tarantool, 'my_queue');
60+
$queue = new Queue($tarantool, 'foobar');
61+
```
62+
63+
64+
### Data types
65+
66+
Under the hood Tarantool uses [MessagePack](http://msgpack.org/) binary format to serialize/deserialize
67+
data being stored in a queue. This means that it's safe to use such data types as `null`, `bool`, `int`,
68+
`float`, `string`, `binary string` and `array` without any manual pre- or post-processing:
69+
70+
```php
71+
$queue->put('foo');
72+
$queue->put(true);
73+
$queue->put(42);
74+
$queue->put(4.2);
75+
$queue->put(['foo' => ['bar' => ['baz' => null]]]);
76+
```
2477

25-
$task = $queue->put('foo');
78+
79+
### Tasks
80+
81+
Almost every method of the [Queue API](src/Queue.php) (except for `kick()` and `statistics()`) returns back
82+
a lightweight [Task](src/Task.php) object containing the following getters:
83+
84+
```php
85+
Task::getId() // The task id
86+
Task::getState() // The task state (States::READY, States::TAKEN, States::DONE, States::BURY or States::DELAYED)
87+
Task::getData() // The task payload
88+
```
89+
90+
And some sugar methods:
91+
92+
```php
93+
Task::isReady()
94+
Task::isTaken()
95+
Task::isDone()
96+
Task::isBuried()
97+
Task::isDelayed()
98+
```
99+
100+
101+
#### Producer API
102+
103+
As you've already seen, to insert a task into a queue you need to call `put()` method, which accepts two arguments:
104+
the data you want to process and optional array of task options, which this particular queue supports.
105+
For example, `fifottl` queue (which we defined earlier in our Lua config file), supports `delay`,
106+
`ttl`, `ttr`, `pri` and `temporary` options:
107+
108+
```php
109+
$queue->put('foo', ['delay' => 30]);
110+
$queue->put('bar', ['ttl' => 5]);
111+
$queue->put('baz', ['ttr' => 10, 'pri' => 42]);
112+
```
113+
114+
> See the full list of available options [here](https://github.com/tarantool/queue#producer-api).
115+
116+
117+
#### Consumer API
118+
119+
To reserve a task for execution, call `take()` method. It accepts an optional `timeout` parameter.
120+
If a timeout value is supplied the call will wait `timeout` seconds until a `READY` task appears in the queue.
121+
The method returns either a [Task](#tasks) object or `null`:
122+
123+
```php
124+
$task = $queue->take();
125+
126+
// wait 2 seconds
127+
$task = $queue->take(2);
128+
129+
// wait 100 milliseconds
26130
$task = $queue->take(.1);
131+
```
27132

133+
After successful execution, a task can be marked as acknowledged (that will also delete the task from a queue):
134+
135+
```php
28136
$data = $task->getData();
29137

30-
$task = $queue->ack($task->getId());
138+
// process $data
139+
140+
$this->queue->ack($task->getId());
141+
```
142+
143+
Or put back into the queue in case it cannot be executed:
144+
145+
```php
146+
$this->queue->release($task->getId());
147+
// or
148+
$this->queue->release($task->getId(), ['delay' => 30]);
149+
```
150+
151+
To look at a task without changing its state, use:
152+
153+
```php
154+
$task = $this->queue->peek($task->getId());
155+
```
156+
157+
To bury (disable) a task:
158+
159+
```php
160+
$queue->bury($task->getId());
161+
```
162+
163+
To reset buried task(s) back to `READY` state:
164+
165+
```php
166+
$count = $queue->kick(3); // kick 3 buried tasks
167+
```
168+
169+
A task (in any state) can be deleted permanently with `delete()`:
170+
171+
```php
172+
$this->queue->delete($task->getId());
173+
```
174+
175+
> For a detailed API documentation, please read [API](https://github.com/tarantool/queue#api)
176+
section of the [queue's README](https://github.com/tarantool/queue/blob/master/README.md).
177+
178+
179+
### Statistics
180+
181+
The `statistics()` method provides access to the statistical information since the creation of a queue:
182+
183+
```php
184+
$stat = $queue->statistics();
185+
```
186+
187+
The result of this call might look like this:
188+
189+
```php
190+
[
191+
'tasks' => [
192+
'taken' => 1,
193+
'buried' => 1,
194+
'ready' => 1,
195+
'done' => 0,
196+
'delayed' => 0,
197+
'total' => 3,
198+
],
199+
'calls' => [
200+
'bury' => 1,
201+
'put' => 3,
202+
'take' => 1,
203+
...
204+
],
205+
]
206+
```
207+
208+
In addition, you can specify a key (or keys) to select only a subset of the resulting array:
209+
210+
```php
211+
$calls = $queue->statistics('calls');
212+
$total = $queue->statistics('tasks', 'total');
31213
```
32214

33215

@@ -45,6 +227,8 @@ To run integration tests:
45227
$ phpunit --testsuite Integration
46228
```
47229

230+
> Make sure that the [instance.lua](tests/Integration/instance.lua) instance is up and running.
231+
48232
To run all tests:
49233

50234
```sh

0 commit comments

Comments
 (0)