Skip to content

Commit d2a123f

Browse files
committed
Add files
0 parents  commit d2a123f

File tree

8 files changed

+509
-0
lines changed

8 files changed

+509
-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+
/.php_cs
3+
/phpunit.xml

.php_cs.dist

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tarantool\JobQueue\JobBuilder;
6+
7+
use PhpCsFixer\Config;
8+
use PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer;
9+
use PhpCsFixer\Fixer\FixerInterface;
10+
use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
11+
use PhpCsFixer\Tokenizer\Tokens;
12+
13+
final class FilterableFixer implements FixerInterface
14+
{
15+
private $fixer;
16+
private $pathRegex;
17+
18+
public function __construct(FixerInterface $fixer, string $pathRegex)
19+
{
20+
$this->fixer = $fixer;
21+
$this->pathRegex = $pathRegex;
22+
}
23+
24+
public function isCandidate(Tokens $tokens) : bool
25+
{
26+
return $this->fixer->isCandidate($tokens);
27+
}
28+
29+
public function isRisky() : bool
30+
{
31+
return $this->fixer->isRisky();
32+
}
33+
34+
public function fix(\SplFileInfo $file, Tokens $tokens) : void
35+
{
36+
$this->fixer->fix($file, $tokens);
37+
}
38+
39+
public function getName() : string
40+
{
41+
return (new \ReflectionClass($this))->getShortName().'/'.$this->fixer->getName();
42+
}
43+
44+
public function getPriority() : int
45+
{
46+
return $this->fixer->getPriority();
47+
}
48+
49+
public function supports(\SplFileInfo $file) : bool
50+
{
51+
if (1 !== preg_match($this->pathRegex, $file->getRealPath())) {
52+
return false;
53+
}
54+
55+
return $this->fixer->supports($file);
56+
}
57+
};
58+
59+
$header = <<<EOF
60+
This file is part of the Tarantool JobBuilder package.
61+
62+
(c) Eugene Leonovich <gen.work@gmail.com>
63+
64+
For the full copyright and license information, please view the LICENSE
65+
file that was distributed with this source code.
66+
EOF;
67+
68+
return Config::create()
69+
->setUsingCache(false)
70+
->setRiskyAllowed(true)
71+
->registerCustomFixers([
72+
new FilterableFixer(new NativeConstantInvocationFixer(), '/\bsrc\b/'),
73+
new FilterableFixer(new NativeFunctionInvocationFixer(), '/\bsrc\b/'),
74+
])
75+
->setRules([
76+
'@Symfony' => true,
77+
'@Symfony:risky' => true,
78+
'array_syntax' => ['syntax' => 'short'],
79+
'binary_operator_spaces' => ['operators' => ['=' => null, '=>' => null]],
80+
'declare_strict_types' => true,
81+
'native_constant_invocation' => false,
82+
'native_function_invocation' => false,
83+
'FilterableFixer/native_constant_invocation' => true,
84+
'FilterableFixer/native_function_invocation' => true,
85+
'no_useless_else' => true,
86+
'no_useless_return' => true,
87+
'ordered_imports' => true,
88+
'phpdoc_order' => true,
89+
'phpdoc_align' => false,
90+
'return_type_declaration' => ['space_before' => 'one'],
91+
'strict_comparison' => true,
92+
'header_comment' => [
93+
'header' => $header,
94+
'location' => 'after_declare_strict',
95+
'separate' => 'both',
96+
],
97+
])
98+
;

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) 2019 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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# JobBuilder
2+
3+
4+
## Installation
5+
6+
The recommended way to create a new application is through [Composer](http://getcomposer.org):
7+
8+
```sh
9+
composer require tarantool/jobbuilder
10+
```
11+
12+
13+
## Usage
14+
15+
```php
16+
use Tarantool\JobQueue\JobBuilder\JobBuilder;
17+
18+
...
19+
$task = JobBuilder::fromService('service_foo', ['bar', 'baz'])
20+
->withConstantBackoff()
21+
->withMaxRetries(3)
22+
->withTimeToExecute(5)
23+
->withTimeToRun(300)
24+
->withPriority(4)
25+
->withDelay(60)
26+
->withTube('foobar')
27+
->putTo($queue);
28+
```
29+
30+
31+
## Tests
32+
33+
```bash
34+
vendor/bin/phpunit
35+
```
36+
37+
38+
## License
39+
40+
The library is released under the MIT License. See the bundled [LICENSE](LICENSE) file for details.

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "tarantool/jobbuilder",
3+
"description": "Utility classes to help creating complex jobs for Tarantool JobQueue.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Eugene Leonovich",
9+
"email": "gen.work@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.1",
14+
"tarantool/queue": "^0.7.0"
15+
},
16+
"require-dev": {
17+
"friendsofphp/php-cs-fixer": "^2.13",
18+
"phpunit/phpunit": "^7.1"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"Tarantool\\JobQueue\\JobBuilder\\": "src/"
23+
}
24+
},
25+
"autoload-dev" : {
26+
"psr-4": {
27+
"Tarantool\\JobQueue\\JobBuilder\\Tests\\": "tests/"
28+
}
29+
},
30+
"config": {
31+
"optimize-autoloader": true,
32+
"preferred-install": {
33+
"*": "dist"
34+
},
35+
"sort-packages": true
36+
}
37+
}

phpunit.xml.dist

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
colors="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
processIsolation="false"
12+
stopOnFailure="false"
13+
verbose="true"
14+
bootstrap="vendor/autoload.php"
15+
>
16+
<php>
17+
<ini name="date.timezone" value="UTC" />
18+
<ini name="display_errors" value="On" />
19+
<ini name="display_startup_errors" value="On" />
20+
<ini name="error_reporting" value="-1" />
21+
</php>
22+
23+
<testsuites>
24+
<testsuite name="Tarantool JobBuilder Test Suite">
25+
<directory>tests</directory>
26+
</testsuite>
27+
</testsuites>
28+
29+
<filter>
30+
<whitelist>
31+
<directory>src</directory>
32+
</whitelist>
33+
</filter>
34+
</phpunit>

src/JobBuilder.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Tarantool JobBuilder package.
7+
*
8+
* (c) Eugene Leonovich <gen.work@gmail.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Tarantool\JobQueue\JobBuilder;
15+
16+
use Tarantool\Queue\Options;
17+
use Tarantool\Queue\Queue;
18+
use Tarantool\Queue\Task;
19+
20+
class JobBuilder
21+
{
22+
private $payload;
23+
private $jobOptions = [];
24+
private $taskOptions = [];
25+
26+
public static function fromService(string $serviceId, array $serviceArgs = []) : self
27+
{
28+
$self = new self();
29+
$self->payload['service'] = $serviceId;
30+
$self->payload['args'] = $serviceArgs;
31+
32+
return $self;
33+
}
34+
35+
public static function fromPayload($payload) : self
36+
{
37+
$self = new self();
38+
$self->payload = $payload;
39+
40+
return $self;
41+
}
42+
43+
public function withServiceArg($value, $key = null) : self
44+
{
45+
$new = clone $this;
46+
47+
(null === $key)
48+
? $new->payload['args'][] = $value
49+
: $new->payload['args'][$key] = $value;
50+
51+
return $new;
52+
}
53+
54+
public function withConstantBackoff() : self
55+
{
56+
$new = clone $this;
57+
$new->jobOptions['retry_strategy'] = 'constant';
58+
59+
return $new;
60+
}
61+
62+
public function withExponentialBackoff() : self
63+
{
64+
$new = clone $this;
65+
$new->jobOptions['retry_strategy'] = 'exponential';
66+
67+
return $new;
68+
}
69+
70+
public function withLinearBackoff() : self
71+
{
72+
$new = clone $this;
73+
$new->jobOptions['retry_strategy'] = 'linear';
74+
75+
return $new;
76+
}
77+
78+
public function withMaxRetries(int $maxRetries) : self
79+
{
80+
$new = clone $this;
81+
$new->jobOptions['retry_limit'] = $maxRetries;
82+
83+
return $new;
84+
}
85+
86+
public function withDisabledRetries() : self
87+
{
88+
$new = clone $this;
89+
$new->jobOptions['retry_limit'] = 0;
90+
91+
return $new;
92+
}
93+
94+
public function withTimeToRun(int $ttl) : self
95+
{
96+
$new = clone $this;
97+
$new->taskOptions[Options::TTL] = $ttl;
98+
99+
return $new;
100+
}
101+
102+
public function withTimeToExecute(int $ttr) : self
103+
{
104+
$new = clone $this;
105+
$new->taskOptions[Options::TTR] = $ttr;
106+
107+
return $new;
108+
}
109+
110+
public function withPriority(int $priority) : self
111+
{
112+
$new = clone $this;
113+
$new->taskOptions[Options::PRI] = $priority;
114+
115+
return $new;
116+
}
117+
118+
public function withDelay(int $delay) : self
119+
{
120+
$new = clone $this;
121+
$new->taskOptions[Options::DELAY] = $delay;
122+
123+
return $new;
124+
}
125+
126+
public function withTube(string $tube) : self
127+
{
128+
$new = clone $this;
129+
$new->taskOptions[Options::UTUBE] = $tube;
130+
131+
return $new;
132+
}
133+
134+
public function build() : array
135+
{
136+
return [
137+
\array_merge(['payload' => $this->payload], $this->jobOptions),
138+
$this->taskOptions,
139+
];
140+
}
141+
142+
public function putTo(Queue $queue) : Task
143+
{
144+
return $queue->put(...$this->build());
145+
}
146+
}

0 commit comments

Comments
 (0)