Skip to content

Commit 1c80c9e

Browse files
committed
Add job options and retry strategy constants, add missing recurrence methods
1 parent bde70dc commit 1c80c9e

File tree

5 files changed

+85
-10
lines changed

5 files changed

+85
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ composer require tarantool/jobbuilder
1818
use Tarantool\JobQueue\JobBuilder\JobBuilder;
1919

2020
...
21+
2122
$task = JobBuilder::fromService('service_foo', ['bar', 'baz'])
2223
->withConstantBackoff()
2324
->withMaxRetries(3)
25+
->withRecurrenceInterval(60)
2426
->withTimeToExecute(5)
2527
->withTimeToRun(300)
2628
->withPriority(4)

src/JobBuilder.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class JobBuilder
2626
public static function fromService(string $serviceId, array $serviceArgs = []) : self
2727
{
2828
$self = new self();
29-
$self->payload['service'] = $serviceId;
30-
$self->payload['args'] = $serviceArgs;
29+
$self->payload[JobOptions::PAYLOAD_SERVICE_ID] = $serviceId;
30+
$self->payload[JobOptions::PAYLOAD_SERVICE_ARGS] = $serviceArgs;
3131

3232
return $self;
3333
}
@@ -45,48 +45,64 @@ public function withServiceArg($value, $key = null) : self
4545
$new = clone $this;
4646

4747
(null === $key)
48-
? $new->payload['args'][] = $value
49-
: $new->payload['args'][$key] = $value;
48+
? $new->payload[JobOptions::PAYLOAD_SERVICE_ARGS][] = $value
49+
: $new->payload[JobOptions::PAYLOAD_SERVICE_ARGS][$key] = $value;
5050

5151
return $new;
5252
}
5353

5454
public function withConstantBackoff() : self
5555
{
5656
$new = clone $this;
57-
$new->jobOptions['retry_strategy'] = 'constant';
57+
$new->jobOptions[JobOptions::RETRY_STRATEGY] = RetryStrategies::CONSTANT;
5858

5959
return $new;
6060
}
6161

6262
public function withExponentialBackoff() : self
6363
{
6464
$new = clone $this;
65-
$new->jobOptions['retry_strategy'] = 'exponential';
65+
$new->jobOptions[JobOptions::RETRY_STRATEGY] = RetryStrategies::EXPONENTIAL;
6666

6767
return $new;
6868
}
6969

7070
public function withLinearBackoff() : self
7171
{
7272
$new = clone $this;
73-
$new->jobOptions['retry_strategy'] = 'linear';
73+
$new->jobOptions[JobOptions::RETRY_STRATEGY] = RetryStrategies::LINEAR;
7474

7575
return $new;
7676
}
7777

7878
public function withMaxRetries(int $maxRetries) : self
7979
{
8080
$new = clone $this;
81-
$new->jobOptions['retry_limit'] = $maxRetries;
81+
$new->jobOptions[JobOptions::RETRY_LIMIT] = $maxRetries;
8282

8383
return $new;
8484
}
8585

8686
public function withDisabledRetries() : self
8787
{
8888
$new = clone $this;
89-
$new->jobOptions['retry_limit'] = 0;
89+
$new->jobOptions[JobOptions::RETRY_LIMIT] = 0;
90+
91+
return $new;
92+
}
93+
94+
public function withRecurrenceInterval(int $interval) : self
95+
{
96+
$new = clone $this;
97+
$new->jobOptions[JobOptions::RECURRENCE] = $interval;
98+
99+
return $new;
100+
}
101+
102+
public function withDisabledRecurrence() : self
103+
{
104+
$new = clone $this;
105+
unset($new->jobOptions[JobOptions::RECURRENCE]);
90106

91107
return $new;
92108
}
@@ -134,7 +150,7 @@ public function withTube(string $tube) : self
134150
public function build() : array
135151
{
136152
return [
137-
\array_merge(['payload' => $this->payload], $this->jobOptions),
153+
\array_merge([JobOptions::PAYLOAD => $this->payload], $this->jobOptions),
138154
$this->taskOptions,
139155
];
140156
}

src/JobOptions.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
final class JobOptions
17+
{
18+
public const PAYLOAD = 'payload';
19+
public const PAYLOAD_SERVICE_ID = 'service';
20+
public const PAYLOAD_SERVICE_METHOD = 'method';
21+
public const PAYLOAD_SERVICE_ARGS = 'args';
22+
public const RETRY_LIMIT = 'retry_limit';
23+
public const RETRY_ATTEMPT = 'retry_attempt';
24+
public const RETRY_STRATEGY = 'retry_strategy';
25+
public const RECURRENCE = 'recurrence';
26+
27+
private function __construct()
28+
{
29+
}
30+
}

src/RetryStrategies.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
final class RetryStrategies
17+
{
18+
public const CONSTANT = 'constant';
19+
public const EXPONENTIAL = 'exponential';
20+
public const LINEAR = 'linear';
21+
22+
private function __construct()
23+
{
24+
}
25+
}

tests/JobBuilderTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function testBuildFull() : void
3232
[$data, $taskOptions] = JobBuilder::fromService('service_foo', ['bar', 'baz'])
3333
->withConstantBackoff()
3434
->withMaxRetries(3)
35+
->withRecurrenceInterval(60)
3536
->withTimeToExecute(5)
3637
->withTimeToRun(300)
3738
->withPriority(4)
@@ -46,6 +47,7 @@ public function testBuildFull() : void
4647
],
4748
'retry_strategy' => 'constant',
4849
'retry_limit' => 3,
50+
'recurrence' => 60,
4951
], $data);
5052

5153
self::assertSame([

0 commit comments

Comments
 (0)