Skip to content

Commit 489239a

Browse files
authored
Add tarantool/client support (#9)
* Add "tarantool/client" support * Update tests, extend Travis build matrix * Update the "Data types" README section
1 parent 5d6a630 commit 489239a

File tree

11 files changed

+268
-194
lines changed

11 files changed

+268
-194
lines changed

.travis.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
sudo: required
22

3-
language: bash
3+
language: php
44

55
services:
66
- docker
77

88
env:
9-
- PHP_RUNTIME='php:5.4-cli'
10-
- PHP_RUNTIME='php:5.5-cli'
11-
- PHP_RUNTIME='php:5.6-cli' PHPUNIT_OPTS='--coverage-clover=coverage.clover'
9+
- IMAGE='php:5.4-cli' TNT_CLIENT=pecl
10+
- IMAGE='php:5.5-cli' TNT_CLIENT=pecl
11+
- IMAGE='php:5.6-cli' TNT_CLIENT=pecl
12+
- IMAGE='php:7.0-cli' TNT_CLIENT=pecl
13+
- IMAGE='php:7.1-cli' TNT_CLIENT=pecl
14+
- IMAGE='php:5.6-cli' TNT_CLIENT=pure
15+
- IMAGE='php:7.0-cli' TNT_CLIENT=pure
16+
- IMAGE='php:7.1-cli' TNT_CLIENT=pure PHPUNIT_OPTS='--coverage-clover=coverage.clover'
1217

1318
install:
14-
- docker build -t tarantool/tarantool github.com/tarantool/tarantool-docker#:1.7
1519
- ./dockerfile.sh | tee /dev/tty | docker build -t queue -
1620

1721
script:
18-
- docker run -d --name tarantool -v $(pwd):/queue tarantool/tarantool /queue/tests/Integration/queues.lua
19-
- docker run --rm --name queue --link tarantool -v $(pwd):/queue -w /queue -e PHPUNIT_OPTS="$PHPUNIT_OPTS" queue
22+
- docker network create tarantool-php
23+
- docker run -d --net=tarantool-php --name=tarantool -v `pwd`:/queue tarantool/tarantool:1.7 tarantool /queue/tests/Integration/queues.lua
24+
- docker run --rm --net=tarantool-php --name=queue -v `pwd`:/queue -w /queue -e PHPUNIT_OPTS="$PHPUNIT_OPTS" queue
2025

2126
after_script:
2227
- if [[ -f coverage.clover ]]; then

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,34 @@ in the Lua script:
5353
```php
5454
use Tarantool\Queue\Queue;
5555

56-
$tarantool = new Tarantool();
57-
$queue = new Queue($tarantool, 'foobar');
56+
...
57+
58+
$queue = new Queue($client, 'foobar');
5859
```
5960

61+
where `$client` is either an instance of the Tarantool class from the [pecl extension](https://github.com/tarantool/tarantool-php)
62+
or an instance of `Tarantool\Client\Client` from the [pure PHP package](https://github.com/tarantool-php/client).
63+
6064

6165
### Data types
6266

6367
Under the hood Tarantool uses [MessagePack](http://msgpack.org/) binary format to serialize/deserialize
64-
data being stored in a queue. This means that it's safe to use such data types as `null`, `bool`, `int`,
65-
`float`, `string`, `binary string` and `array` without any manual pre- or post-processing:
68+
data being stored in a queue. It can handle most of the PHP data types (except resources and closures) without
69+
any manual pre- or post-processing:
6670

6771
```php
6872
$queue->put('foo');
6973
$queue->put(true);
7074
$queue->put(42);
7175
$queue->put(4.2);
7276
$queue->put(['foo' => ['bar' => ['baz' => null]]]);
77+
$queue->put(new MyObject());
7378
```
7479

80+
> *Note*
81+
>
82+
> Object serialization is only supported when [tarantool/client](https://github.com/tarantool-php/client) is used.
83+
7584

7685
### Tasks
7786

@@ -230,21 +239,22 @@ $ ./dockerfile.sh | docker build -t queue -
230239
Then run Tarantool instance (needed for integration tests):
231240

232241
```sh
233-
$ docker run -d --name tarantool -v $(pwd):/queue tarantool/tarantool \
234-
/queue/tests/Integration/queues.lua
242+
$ docker network create tarantool-php
243+
$ docker run -d --net=tarantool-php --name=tarantool -v `pwd`:/queue \
244+
tarantool/tarantool:1.7 tarantool /queue/tests/Integration/queues.lua
235245
```
236246

237247
And then run both unit and integration tests:
238248

239249
```sh
240-
$ docker run --rm --name queue --link tarantool -v $(pwd):/queue -w /queue queue
250+
$ docker run --rm --net=tarantool-php --name=queue -v `pwd`:/queue -w /queue queue
241251
```
242252

243253
To run only integration or unit tests, set the `PHPUNIT_OPTS` environment variable
244254
to either `--testsuite Integration` or `--testsuite Unit` respectively, e.g.:
245255

246256
```sh
247-
$ docker run --rm --name queue -v $(pwd):/queue -w /queue \
257+
$ docker run --rm --net=tarantool-php --name=queue -v `pwd`:/queue -w /queue \
248258
-e PHPUNIT_OPTS='--testsuite Unit' queue
249259
```
250260

composer.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "tarantool/queue",
33
"description": "PHP bindings for Tarantool Queue.",
4-
"keywords": ["queue", "schedule", "delayed", "priority", "ttl", "ttr", "task", "job", "worker", "tarantool"],
4+
"keywords": ["queue", "schedule", "delayed", "priority", "ttl", "ttr", "task", "job", "worker", "tarantool", "nosql"],
55
"type": "library",
66
"license": "MIT",
77
"authors": [
@@ -11,8 +11,15 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^5.4|^7.0",
15-
"ext-tarantool": "*"
14+
"php": "^5.4|^7.0"
15+
},
16+
"require-dev": {
17+
"rybakit/msgpack": "@dev",
18+
"tarantool/client": "@dev"
19+
},
20+
"suggest": {
21+
"ext-tarantool": "For using pecl extension",
22+
"tarantool/client": "For using pure client"
1623
},
1724
"autoload": {
1825
"psr-4": {
@@ -23,10 +30,5 @@
2330
"psr-4": {
2431
"Tarantool\\Queue\\Tests\\": "tests/"
2532
}
26-
},
27-
"extra": {
28-
"branch-alias": {
29-
"dev-master": "1.0.x-dev"
30-
}
3133
}
3234
}

dockerfile.sh

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
#!/usr/bin/env bash
22

3-
if [[ -z "$PHP_RUNTIME" ]] ; then
4-
PHP_RUNTIME='php:5.6-cli'
3+
if [[ -z "$IMAGE" ]] ; then
4+
IMAGE='php:7.1-cli'
5+
fi
6+
7+
if [[ -z "$TNT_CLIENT" ]] ; then
8+
TNT_CLIENT='pure'
59
fi
610

711
RUN_CMDS=''
12+
RUN_POST_CMDS=''
813

9-
if [[ $PHP_RUNTIME == php* ]]; then
14+
if [[ $IMAGE == php* ]]; then
1015
RUN_CMDS="$RUN_CMDS && \\\\\n docker-php-ext-install zip"
11-
RUN_CMDS="$RUN_CMDS && \\\\\n git clone https://github.com/tarantool/tarantool-php.git /usr/src/php/ext/tarantool"
12-
RUN_CMDS="$RUN_CMDS && \\\\\n echo tarantool >> /usr/src/php-available-exts && docker-php-ext-install tarantool"
16+
if [[ $TNT_CLIENT == pure ]]; then
17+
RUN_POST_CMDS="$RUN_POST_CMDS && \\\\\n composer require tarantool/client:@dev"
18+
else
19+
RUN_CMDS="$RUN_CMDS && \\\\\n git clone https://github.com/tarantool/tarantool-php.git /usr/src/php/ext/tarantool"
20+
if [[ $IMAGE == php:7* ]]; then RUN_CMDS="$RUN_CMDS && \\\\\n git --git-dir=/usr/src/php/ext/tarantool/.git --work-tree=/usr/src/php/ext/tarantool checkout php7-v2"; fi
21+
RUN_CMDS="$RUN_CMDS && \\\\\n echo tarantool >> /usr/src/php-available-exts && docker-php-ext-install tarantool"
22+
fi
1323
fi
1424

1525
if [[ $PHPUNIT_OPTS =~ (^|[[:space:]])--coverage-[[:alpha:]] ]]; then
1626
RUN_CMDS="$RUN_CMDS && \\\\\n pecl install xdebug && docker-php-ext-enable xdebug"
1727
fi
1828

1929
echo -e "
20-
FROM $PHP_RUNTIME
30+
FROM $IMAGE
2131
2232
RUN apt-get update && \\
2333
apt-get install -y git curl zlib1g-dev${RUN_CMDS} && \\
2434
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \\
25-
composer global require 'phpunit/phpunit:^4.8|^5.0'
35+
composer global require 'phpunit/phpunit:^4.8|^5.0|^6.0'${RUN_POST_CMDS}
2636
2737
ENV PATH=~/.composer/vendor/bin:\$PATH
2838
ENV TARANTOOL_HOST=tarantool TARANTOOL_PORT=3301

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
stopOnFailure="false"
1111
syntaxCheck="false"
1212
verbose="true"
13-
bootstrap="vendor/autoload.php"
13+
bootstrap="tests/bootstrap.php"
1414
>
1515
<php>
1616
<ini name="date.timezone" value="UTC" />

src/ClientAdapter.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Tarantool Queue package.
5+
*
6+
* (c) Eugene Leonovich <gen.work@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Tarantool\Queue;
13+
14+
use Tarantool\Client\Client;
15+
16+
class ClientAdapter
17+
{
18+
private $client;
19+
20+
public function __construct(Client $client)
21+
{
22+
$this->client = $client;
23+
}
24+
25+
public function call($funcName, array $args = [])
26+
{
27+
$result = $this->client->call($funcName, $args);
28+
29+
return isset($result->getData()[0][0]) ? $result->getData() : [$result->getData()];
30+
}
31+
}

src/Queue.php

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,44 @@
1111

1212
namespace Tarantool\Queue;
1313

14+
use Tarantool\Client\Client;
15+
1416
class Queue
1517
{
1618
private $client;
1719
private $tubeName;
18-
private $prefix;
1920

20-
public function __construct(\Tarantool $client, $tubeName)
21+
/**
22+
* @param \Tarantool|\Tarantool\Client\Client $client
23+
* @param string $tubeName
24+
*
25+
* @throws \InvalidArgumentException
26+
*/
27+
public function __construct($client, $tubeName)
2128
{
29+
if ($client instanceof Client) {
30+
$client = new ClientAdapter($client);
31+
} else if (!$client instanceof \Tarantool) {
32+
throw new \InvalidArgumentException(sprintf(
33+
'%s() expects parameter 1 to be Tarantool or Tarantool\Client\Client, %s given.',
34+
__METHOD__, is_object($client) ? get_class($client) : gettype($client)
35+
));
36+
}
37+
2238
$this->client = $client;
2339
$this->tubeName = $tubeName;
24-
$this->prefix = "queue.tube.$tubeName:";
2540
}
2641

2742
/**
28-
* @param mixed $data
29-
* @param array|null $options
43+
* @param mixed $data
44+
* @param array $options
3045
*
3146
* @return Task
3247
*/
33-
public function put($data, array $options = null)
48+
public function put($data, array $options = [])
3449
{
3550
$args = $options ? [$data, $options] : [$data];
36-
$result = $this->client->call($this->prefix.'put', $args);
51+
$result = $this->client->call("queue.tube.$this->tubeName:put", $args);
3752

3853
return Task::createFromTuple($result[0]);
3954
}
@@ -46,7 +61,7 @@ public function put($data, array $options = null)
4661
public function take($timeout = null)
4762
{
4863
$args = null === $timeout ? [] : [$timeout];
49-
$result = $this->client->call($this->prefix.'take', $args);
64+
$result = $this->client->call("queue.tube.$this->tubeName:take", $args);
5065

5166
return empty($result[0]) ? null : Task::createFromTuple($result[0]);
5267
}
@@ -58,21 +73,21 @@ public function take($timeout = null)
5873
*/
5974
public function ack($taskId)
6075
{
61-
$result = $this->client->call($this->prefix.'ack', [$taskId]);
76+
$result = $this->client->call("queue.tube.$this->tubeName:ack", [$taskId]);
6277

6378
return Task::createFromTuple($result[0]);
6479
}
6580

6681
/**
67-
* @param int $taskId
68-
* @param array|null $options
82+
* @param int $taskId
83+
* @param array $options
6984
*
7085
* @return Task
7186
*/
72-
public function release($taskId, array $options = null)
87+
public function release($taskId, array $options = [])
7388
{
7489
$args = $options ? [$taskId, $options] : [$taskId];
75-
$result = $this->client->call($this->prefix.'release', $args);
90+
$result = $this->client->call("queue.tube.$this->tubeName:release", $args);
7691

7792
return Task::createFromTuple($result[0]);
7893
}
@@ -84,7 +99,7 @@ public function release($taskId, array $options = null)
8499
*/
85100
public function peek($taskId)
86101
{
87-
$result = $this->client->call($this->prefix.'peek', [$taskId]);
102+
$result = $this->client->call("queue.tube.$this->tubeName:peek", [$taskId]);
88103

89104
return Task::createFromTuple($result[0]);
90105
}
@@ -96,7 +111,7 @@ public function peek($taskId)
96111
*/
97112
public function bury($taskId)
98113
{
99-
$result = $this->client->call($this->prefix.'bury', [$taskId]);
114+
$result = $this->client->call("queue.tube.$this->tubeName:bury", [$taskId]);
100115

101116
return Task::createFromTuple($result[0]);
102117
}
@@ -108,7 +123,7 @@ public function bury($taskId)
108123
*/
109124
public function kick($count)
110125
{
111-
$result = $this->client->call($this->prefix.'kick', [$count]);
126+
$result = $this->client->call("queue.tube.$this->tubeName:kick", [$count]);
112127

113128
return $result[0][0];
114129
}
@@ -120,14 +135,14 @@ public function kick($count)
120135
*/
121136
public function delete($taskId)
122137
{
123-
$result = $this->client->call($this->prefix.'delete', [$taskId]);
138+
$result = $this->client->call("queue.tube.$this->tubeName:delete", [$taskId]);
124139

125140
return Task::createFromTuple($result[0]);
126141
}
127142

128143
public function truncate()
129144
{
130-
$this->client->call($this->prefix.'truncate');
145+
$this->client->call("queue.tube.$this->tubeName:truncate");
131146
}
132147

133148
/**

0 commit comments

Comments
 (0)