Skip to content

Commit 8bc62fb

Browse files
author
Eugene Leonovich
committed
Initial commit
0 parents  commit 8bc62fb

18 files changed

+806
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml

.travis.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
8+
before_install:
9+
- curl http://tarantool.org/dist/public.key | sudo apt-key add -
10+
- echo "deb http://tarantool.org/dist/master/ubuntu `lsb_release -c -s` main" | sudo tee -a /etc/apt/sources.list.d/tarantool.list
11+
- sudo apt-get update
12+
13+
install:
14+
- sudo apt-get -y install tarantool php5-tarantool luarocks
15+
16+
- mkdir ~/.luarocks
17+
- echo "rocks_servers = {[[http://rocks.tarantool.org/]]}" >> ~/.luarocks/config.lua
18+
- luarocks install queue
19+
20+
- composer install
21+
22+
before_script:
23+
- sudo cp ./tests/Integration/instance.lua /etc/tarantool/instances.enabled
24+
- sudo tarantoolctl start instance
25+
- >
26+
for i in `seq 1 10`; do
27+
if sudo tarantoolctl status instance; then
28+
break;
29+
fi;
30+
sleep 1;
31+
done
32+
33+
script:
34+
- >
35+
bash -c 'if [[ 5.6 == $TRAVIS_PHP_VERSION ]]; then
36+
phpunit -v --coverage-clover coverage.clover;
37+
else
38+
phpunit -v;
39+
fi;'
40+
41+
after_script:
42+
# code-coverage for scrutinizer-ci
43+
- >
44+
bash -c 'if [[ 5.6 == $TRAVIS_PHP_VERSION ]]; then
45+
wget https://scrutinizer-ci.com/ocular.phar;
46+
php ocular.phar code-coverage:upload --format=php-clover coverage.clover;
47+
fi;'

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Eugene Leonovich
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Tarantool Queue
2+
3+
[![Build Status](https://travis-ci.org/tarantool-php/queue.svg?branch=master)](https://travis-ci.org/tarantool-php/queue)
4+
[![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)
5+
[![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)
6+
7+
8+
## Installation
9+
10+
The recommended way to install the library is through [Composer](http://getcomposer.org):
11+
12+
```sh
13+
$ composer require tarantool/queue:~1.0@dev
14+
```
15+
16+
17+
## Usage example
18+
19+
```php
20+
use Tarantool\Queue\Queue;
21+
22+
$tarantool = new Tarantool();
23+
$queue = new Queue($tarantool, 'my_queue');
24+
25+
$task = $queue->put('foo');
26+
$task = $queue->take(.1);
27+
28+
$data = $task->getData();
29+
30+
$task = $queue->ack($task->getId());
31+
```
32+
33+
34+
## Tests
35+
36+
To run unit tests:
37+
38+
```sh
39+
$ phpunit --testsuite Unit
40+
```
41+
42+
To run integration tests:
43+
44+
```sh
45+
$ phpunit --testsuite Integration
46+
```
47+
48+
To run all tests:
49+
50+
```sh
51+
$ phpunit
52+
53+
```
54+
55+
## License
56+
57+
The library is released under the MIT License. See the bundled [LICENSE](LICENSE) file for details.

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "tarantool/queue",
3+
"description": "PHP bindings for Tarantool Queue.",
4+
"keywords": ["queue", "schedule", "delayed", "priority", "ttl", "ttr", "task", "job", "tarantool"],
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Eugene Leonovich",
10+
"email": "gen.work@gmail.com"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.4.0",
15+
"ext-tarantool": "*"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Tarantool\\Queue\\": "src/"
20+
}
21+
},
22+
"autoload-dev" : {
23+
"psr-4": {
24+
"Tarantool\\Queue\\Tests\\": "tests/"
25+
}
26+
},
27+
"extra": {
28+
"branch-alias": {
29+
"dev-master": "1.0.x-dev"
30+
}
31+
}
32+
}

phpunit.xml.dist

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
verbose="true"
13+
bootstrap="vendor/autoload.php"
14+
>
15+
<php>
16+
<ini name="date.timezone" value="UTC" />
17+
18+
<env name="TNT_HOST" value="127.0.0.1" />
19+
<env name="TNT_PORT" value="3301" />
20+
</php>
21+
22+
<testsuites>
23+
<testsuite name="Unit">
24+
<directory>tests/Unit</directory>
25+
</testsuite>
26+
27+
<testsuite name="Integration">
28+
<directory>tests/Integration</directory>
29+
</testsuite>
30+
</testsuites>
31+
32+
<filter>
33+
<whitelist>
34+
<directory>src</directory>
35+
</whitelist>
36+
</filter>
37+
</phpunit>

src/Queue.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace Tarantool\Queue;
4+
5+
class Queue
6+
{
7+
private $client;
8+
private $prefix;
9+
10+
public function __construct(\Tarantool $client, $tubeName)
11+
{
12+
$this->client = $client;
13+
$this->prefix = "queue.tube.$tubeName:";
14+
}
15+
16+
/**
17+
* @param mixed $data
18+
* @param array|null $options
19+
*
20+
* @return Task
21+
*/
22+
public function put($data, array $options = null)
23+
{
24+
$result = $this->client->call($this->prefix.'put', [$data, (array) $options]);
25+
26+
return Task::createFromTuple($result[0]);
27+
}
28+
29+
/**
30+
* @param int|null $timeout
31+
*
32+
* @return Task|null
33+
*/
34+
public function take($timeout = null)
35+
{
36+
$options = null === $timeout ? [] : [(float) $timeout];
37+
$result = $this->client->call($this->prefix.'take', $options);
38+
39+
return empty($result[0]) ? null : Task::createFromTuple($result[0]);
40+
}
41+
42+
/**
43+
* @param int $taskId
44+
*
45+
* @return Task
46+
*/
47+
public function ack($taskId)
48+
{
49+
$result = $this->client->call($this->prefix.'ack', [$taskId]);
50+
51+
return Task::createFromTuple($result[0]);
52+
}
53+
54+
/**
55+
* @param int $taskId
56+
* @param array|null $options
57+
*
58+
* @return Task
59+
*/
60+
public function release($taskId, array $options = null)
61+
{
62+
$result = $this->client->call($this->prefix.'release', [$taskId, (array) $options]);
63+
64+
return Task::createFromTuple($result[0]);
65+
}
66+
67+
/**
68+
* @param int $taskId
69+
*
70+
* @return Task
71+
*/
72+
public function peek($taskId)
73+
{
74+
$result = $this->client->call($this->prefix.'peek', [$taskId]);
75+
76+
return Task::createFromTuple($result[0]);
77+
}
78+
79+
/**
80+
* @param int $taskId
81+
*
82+
* @return Task
83+
*/
84+
public function bury($taskId)
85+
{
86+
$result = $this->client->call($this->prefix.'bury', [$taskId]);
87+
88+
return Task::createFromTuple($result[0]);
89+
}
90+
91+
/**
92+
* @param int $count
93+
*
94+
* @return int
95+
*/
96+
public function kick($count)
97+
{
98+
$result = $this->client->call($this->prefix.'kick', [$count]);
99+
100+
return $result[0][0];
101+
}
102+
103+
/**
104+
* @param int $taskId
105+
*
106+
* @return Task
107+
*/
108+
public function delete($taskId)
109+
{
110+
$result = $this->client->call($this->prefix.'delete', [$taskId]);
111+
112+
return Task::createFromTuple($result[0]);
113+
}
114+
}

src/States.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Tarantool\Queue;
4+
5+
abstract class States
6+
{
7+
const READY = 'r';
8+
const TAKEN = 't';
9+
const DONE = '-';
10+
const BURIED = '!';
11+
const DELAYED = '~';
12+
}

src/Task.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Tarantool\Queue;
4+
5+
final class Task
6+
{
7+
private $id;
8+
private $type;
9+
private $data;
10+
11+
private function __construct($id, $type, $data = null)
12+
{
13+
$this->id = $id;
14+
$this->type = $type;
15+
$this->data = $data;
16+
}
17+
18+
public static function createFromTuple(array $tuple)
19+
{
20+
list($id, $state, $data) = $tuple + [2 => null];
21+
22+
return new self($id, $state, $data);
23+
}
24+
25+
public function getId()
26+
{
27+
return $this->id;
28+
}
29+
30+
public function getType()
31+
{
32+
return $this->type;
33+
}
34+
35+
public function getData()
36+
{
37+
return $this->data;
38+
}
39+
40+
public function isReady()
41+
{
42+
return States::READY === $this->type;
43+
}
44+
45+
public function isTaken()
46+
{
47+
return States::TAKEN === $this->type;
48+
}
49+
50+
public function isDone()
51+
{
52+
return States::DONE === $this->type;
53+
}
54+
55+
public function isBuried()
56+
{
57+
return States::BURIED === $this->type;
58+
}
59+
60+
public function isDelayed()
61+
{
62+
return States::DELAYED === $this->type;
63+
}
64+
}

0 commit comments

Comments
 (0)