Skip to content

Commit

Permalink
Merge 64306d9 into 5c81c61
Browse files Browse the repository at this point in the history
  • Loading branch information
rybakit committed Oct 25, 2018
2 parents 5c81c61 + 64306d9 commit cf10a28
Show file tree
Hide file tree
Showing 20 changed files with 440 additions and 341 deletions.
98 changes: 98 additions & 0 deletions .php_cs.dist
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Tarantool\Queue;

use PhpCsFixer\Config;
use PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
use PhpCsFixer\Tokenizer\Tokens;

final class FilterableFixer implements FixerInterface
{
private $fixer;
private $pathRegex;

public function __construct(FixerInterface $fixer, string $pathRegex)
{
$this->fixer = $fixer;
$this->pathRegex = $pathRegex;
}

public function isCandidate(Tokens $tokens) : bool
{
return $this->fixer->isCandidate($tokens);
}

public function isRisky() : bool
{
return $this->fixer->isRisky();
}

public function fix(\SplFileInfo $file, Tokens $tokens) : void
{
$this->fixer->fix($file, $tokens);
}

public function getName() : string
{
return (new \ReflectionClass($this))->getShortName().'/'.$this->fixer->getName();
}

public function getPriority() : int
{
return $this->fixer->getPriority();
}

public function supports(\SplFileInfo $file) : bool
{
if (1 !== preg_match($this->pathRegex, $file->getRealPath())) {
return false;
}

return $this->fixer->supports($file);
}
};

$header = <<<EOF
This file is part of the Tarantool Queue package.
(c) Eugene Leonovich <gen.work@gmail.com>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;

return Config::create()
->setUsingCache(false)
->setRiskyAllowed(true)
->registerCustomFixers([
new FilterableFixer(new NativeConstantInvocationFixer(), '/\bsrc\b/'),
new FilterableFixer(new NativeFunctionInvocationFixer(), '/\bsrc\b/'),
])
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => ['operators' => ['=' => null, '=>' => null]],
'declare_strict_types' => true,
'native_constant_invocation' => false,
'native_function_invocation' => false,
'FilterableFixer/native_constant_invocation' => true,
'FilterableFixer/native_function_invocation' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'phpdoc_order' => true,
'phpdoc_align' => false,
'return_type_declaration' => ['space_before' => 'one'],
'strict_comparison' => true,
'header_comment' => [
'header' => $header,
'location' => 'after_declare_strict',
'separate' => 'both',
],
])
;
11 changes: 4 additions & 7 deletions .travis.yml
Expand Up @@ -6,13 +6,10 @@ services:
- docker

env:
- IMAGE='php:5.6-cli' TNT_CLIENT=pecl
- IMAGE='php:7.0-cli' TNT_CLIENT=pecl
- IMAGE='php:7.1-cli' TNT_CLIENT=pecl
- IMAGE='php:5.6-cli' TNT_CLIENT=pure
- IMAGE='php:7.0-cli' TNT_CLIENT=pure
- IMAGE='php:7.1-cli' TNT_CLIENT=pure
- IMAGE='php:7.2-cli' TNT_CLIENT=pure PHPUNIT_OPTS='--coverage-clover=coverage.clover'
- IMAGE='php:7.1-cli' CHECK_CS=1
- IMAGE='php:7.2-cli' PHPUNIT_OPTS='--coverage-clover=coverage.clover'
- IMAGE='php:7.3-rc-cli'

install:
- ./dockerfile.sh | tee /dev/tty | docker build -t queue -
Expand All @@ -25,5 +22,5 @@ script:
after_script:
- if [[ -f coverage.clover ]]; then
curl -sSOL https://scrutinizer-ci.com/ocular.phar &&
docker run --rm -v $(pwd):/coverage -w /coverage queue php ocular.phar code-coverage:upload --format=php-clover coverage.clover;
docker run --rm -v $(pwd):/queue -w /queue queue php ocular.phar code-coverage:upload --format=php-clover coverage.clover;
fi
68 changes: 42 additions & 26 deletions README.md
Expand Up @@ -3,18 +3,34 @@
[![Build Status](https://travis-ci.org/tarantool-php/queue.svg?branch=master)](https://travis-ci.org/tarantool-php/queue)
[![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)
[![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)
[![Mentioned in Awesome PHP](https://awesome.re/mentioned-badge.svg)](https://github.com/ziadoz/awesome-php)

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


## Table of contents

* [Installation](#installation)
* [Before start](#before-start)
* [Working with queue](#working-with-queue)
* [Data types](#data-types)
* [Tasks](#tasks)
* [Producer API](#producer-api)
* [Consumer API](#consumer-api)
* [Statistics](#statistics)
* [Custom methods](#custom-methods)
* [Tests](#tests)
* [License](#license)


## Installation

The recommended way to install the library is through [Composer](http://getcomposer.org):

```sh
$ composer require tarantool/queue
```bash
composer require tarantool/queue
```


Expand All @@ -38,8 +54,8 @@ queue.create_tube('foobar', 'fifottl', {if_not_exists=true})
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:

```sh
$ sudo tarantoolctl start queues
```bash
sudo tarantoolctl start queues
```


Expand Down Expand Up @@ -104,15 +120,15 @@ Task::isDelayed()

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:
queue supports. For example, `fifottl` queue (which we defined [earlier](#before-start) in our Lua config
file), supports `delay`, `ttl`, `ttr` and `pri` options:

```php
use Tarantool\Queue\Options;

$queue->put('foo', [Options::DELAY => 30]);
$queue->put('bar', [Options::TTL => 5]);
$queue->put('baz', [Options::TTR => 10, Options::PRI => 42]);
$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](https://github.com/tarantool/queue#queue-types).*
Expand All @@ -128,7 +144,7 @@ The method returns either a [Task](#tasks) object or `null`:
$taskOrNull = $queue->take();

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

// wait 100 milliseconds
$taskOrNull = $queue->take(.1);
Expand All @@ -150,7 +166,7 @@ Or put back into the queue in case it cannot be executed:
$task = $queue->release($task->getId());

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

To look at a task without changing its state, use:
Expand All @@ -174,7 +190,7 @@ $count = $queue->kick(3); // kick 3 buried tasks
To increase TTR and/or TTL of a running task (only for *ttl queues):

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

A task (in any state) can be deleted permanently with `delete()`:
Expand Down Expand Up @@ -259,41 +275,41 @@ end
To call this method on a `$queue` object, use `Queue::call()`:

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


## Tests

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

```sh
$ ./dockerfile.sh | docker build -t queue -
```bash
./dockerfile.sh | docker build -t queue -
```

Then run Tarantool instance (needed for integration tests):

```sh
$ docker network create tarantool-php
$ docker run -d --net=tarantool-php --name=tarantool -v `pwd`:/queue \
```bash
docker network create tarantool-php
docker run -d --net=tarantool-php --name=tarantool -v `pwd`:/queue \
tarantool/tarantool:1 tarantool /queue/tests/Integration/queues.lua
```

And then run both unit and integration tests:

```sh
$ docker run --rm --net=tarantool-php --name=queue -v `pwd`:/queue -w /queue queue
```bash
docker run --rm --net=tarantool-php --name=queue -v `pwd`:/queue -w /queue queue
```

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

```sh
$ docker run --rm --net=tarantool-php --name=queue -v `pwd`:/queue -w /queue \
-e PHPUNIT_OPTS='--testsuite Unit' queue
```bash
docker run --rm --net=tarantool-php --name=queue -v `pwd`:/queue -w /queue \
-e PHPUNIT_OPTS='--testsuite unit' queue
```


Expand Down
10 changes: 9 additions & 1 deletion composer.json
Expand Up @@ -11,9 +11,11 @@
}
],
"require": {
"php": "^5.4|^7.0"
"php": "^7.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.13",
"phpunit/phpunit": "^7.4",
"rybakit/msgpack": "@dev",
"tarantool/client": "@dev"
},
Expand All @@ -30,5 +32,11 @@
"psr-4": {
"Tarantool\\Queue\\Tests\\": "tests/"
}
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
}
}
32 changes: 16 additions & 16 deletions dockerfile.sh
Expand Up @@ -8,34 +8,34 @@ if [[ -z "$TNT_CLIENT" ]] ; then
TNT_CLIENT='pure'
fi

RUN_CMDS=''
RUN_POST_CMDS=''

if [[ $IMAGE == php* ]]; then
RUN_CMDS="$RUN_CMDS && \\\\\n docker-php-ext-install zip"
if [[ $TNT_CLIENT == pure ]]; then
RUN_POST_CMDS="$RUN_POST_CMDS && \\\\\n composer require tarantool/client:@dev"
else
RUN_CMDS="$RUN_CMDS && \\\\\n git clone https://github.com/tarantool/tarantool-php.git /usr/src/php/ext/tarantool"
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
RUN_CMDS="$RUN_CMDS && \\\\\n echo tarantool >> /usr/src/php-available-exts && docker-php-ext-install tarantool"
fi
if [[ $TNT_CLIENT == pecl ]]; then
RUN_CMDS="$RUN_CMDS && \\\\\n git clone https://github.com/tarantool/tarantool-php.git /usr/src/php/ext/tarantool"
RUN_CMDS="$RUN_CMDS && \\\\\n git --git-dir=/usr/src/php/ext/tarantool/.git --work-tree=/usr/src/php/ext/tarantool checkout php7-v2"
RUN_CMDS="$RUN_CMDS && \\\\\n echo tarantool >> /usr/src/php-available-exts && docker-php-ext-install tarantool"
COMPOSER_REMOVE='tarantool/client'
fi

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

if [[ "1" != "$CHECK_CS" ]]; then
COMPOSER_REMOVE="$COMPOSER_REMOVE friendsofphp/php-cs-fixer"
fi

echo -e "
FROM $IMAGE
RUN apt-get update && \\
apt-get install -y git curl zlib1g-dev${RUN_CMDS} && \\
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \\
composer global require 'phpunit/phpunit'${RUN_POST_CMDS}
apt-get install -y git curl libzip-dev && \\
docker-php-ext-configure zip --with-libzip && \\
docker-php-ext-install zip${RUN_CMDS}
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
ENV PATH=~/.composer/vendor/bin:\$PATH
ENV TARANTOOL_HOST=tarantool TARANTOOL_PORT=3301
CMD if [ ! -f composer.lock ]; then composer install; fi && ~/.composer/vendor/bin/phpunit\${PHPUNIT_OPTS:+ }\$PHPUNIT_OPTS
CMD if [ ! -f composer.lock ]; then ${COMPOSER_REMOVE:+composer remove --dev --no-update }$COMPOSER_REMOVE${COMPOSER_REMOVE:+ && }composer install; fi && \\
vendor/bin/phpunit\${PHPUNIT_OPTS:+ }\$PHPUNIT_OPTS
"
19 changes: 6 additions & 13 deletions phpunit.xml.dist
@@ -1,16 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="true"
bootstrap="tests/bootstrap.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
verbose="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="date.timezone" value="UTC" />
Expand All @@ -23,11 +16,11 @@
</php>

<testsuites>
<testsuite name="Unit">
<testsuite name="unit">
<directory>tests/Unit</directory>
</testsuite>

<testsuite name="Integration">
<testsuite name="integration">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>
Expand Down

0 comments on commit cf10a28

Please sign in to comment.