Skip to content

Commit 713bc5f

Browse files
authored
Merge pull request #5 from apacheborys/introduce-declarators
Introduce declarators
2 parents efb4367 + 9f34e50 commit 713bc5f

8 files changed

Lines changed: 128 additions & 45 deletions

File tree

README.md

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,49 @@ Config example:
2525

2626
```json
2727
{
28-
"test": { /* name of retry */
29-
"exception": "ApacheBorys\\Retry\\Tests\\Functional\\Exceptions\\Mock", /* what type of Exception we would like to retry */
30-
"maxRetries": 4, /* how many tries we should do */
28+
/**
29+
* here we can define declarator what should register exception handling callback function, if you are plan to use
30+
* standard php function set_exception_handler - you can ignore that section. StandardHandlerExceptionDeclarator is default
31+
**/
32+
"handlerExceptionDefiner": {
33+
"class": "ApacheBorys\\Retry\\HandlerExceptionDefiner\\StandardHandlerExceptionDeclarator",
34+
"arguments": []
35+
},
36+
"items": {
37+
"test": {
38+
/* name of retry */
39+
"exception": "ApacheBorys\\Retry\\Tests\\Functional\\Exceptions\\Mock",
40+
/* what type of Exception we would like to retry */
41+
"maxRetries": 4,
42+
/* how many tries we should do */
3143
/* here we are describing formula, how next execution time should be calculated. Calculated amount will be added to current time */
32-
"formula": [
33-
{
34-
"operator": "+", /* here available *, -, + and / operators */
35-
"argument": "QTY_TRIES" /* you can use QTY_TRIES operator or any integer value */
36-
},
37-
{
38-
"operator": "*",
39-
"argument": "5"
40-
}
44+
"formula": [
45+
{
46+
"operator": "+",
47+
/* here available *, -, + and / operators */
48+
"argument": "QTY_TRIES"
49+
/* you can use QTY_TRIES operator or any integer value */
50+
},
51+
{
52+
"operator": "*",
53+
"argument": "5"
54+
}
4155
],
42-
"transport": { /* here you should define, what kind of transport you would use to deliver re-try messages to worker */
43-
"class": "ApacheBorys\\Retry\\Tests\\Functional\\Transport\\FileTransportForTests",
44-
"arguments": [ /* each specific transport could have own arguments in constructor. Here you should define it */
45-
"tests\/transport.data"
46-
]
56+
"transport": {
57+
/* here you should define, what kind of transport you would use to deliver re-try messages to worker */
58+
"class": "ApacheBorys\\Retry\\Tests\\Functional\\Transport\\FileTransportForTests",
59+
"arguments": [
60+
/* each specific transport could have own arguments in constructor. Here you should define it */
61+
"tests\/transport.data"
62+
]
4763
},
48-
"executor": { /* here you should define, what kind of executor you would use to perform re-try action */
49-
"class": "ApacheBorys\\Retry\\Tests\\Functional\\Executor\\Runtime",
50-
"arguments": [] /* each specific executor could have own arguments in constructor. Here you should define it */
64+
"executor": {
65+
/* here you should define, what kind of executor you would use to perform re-try action */
66+
"class": "ApacheBorys\\Retry\\Tests\\Functional\\Executor\\Runtime",
67+
"arguments": []
68+
/* each specific executor could have own arguments in constructor. Here you should define it */
5169
}
70+
}
5271
}
5372
}
5473
```

psalm.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="2"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
>
9+
<projectFiles>
10+
<directory name="src" />
11+
<ignoreFiles>
12+
<directory name="vendor" />
13+
</ignoreFiles>
14+
</projectFiles>
15+
</psalm>

src/AbstractHandler.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
namespace ApacheBorys\Retry;
55

66
use ApacheBorys\Retry\Entity\Config;
7+
use ApacheBorys\Retry\HandlerExceptionDefiner\StandardHandlerExceptionDeclarator;
8+
use ApacheBorys\Retry\Interfaces\HandlerExceptionDeclaratorInterface;
79

810
abstract class AbstractHandler
911
{
1012
/** @var Config[] */
1113
protected array $config;
1214

15+
protected HandlerExceptionDeclaratorInterface $declarator;
16+
1317
public function __construct(array $config = [])
1418
{
1519
$this->config = $this->initConfig($config);
@@ -19,12 +23,16 @@ public function __construct(array $config = [])
1923
* @param array $config
2024
* @return Config[]
2125
* @psalm-suppress ArgumentTypeCoercion
26+
* @psalm-suppress PropertyTypeCoercion
2227
*/
2328
private function initConfig(array $config): array
2429
{
2530
$result = [];
2631

27-
foreach ($config as $retryName => $configNode) {
32+
$definerClass = $config['handlerExceptionDeclarator']['class'] ?? StandardHandlerExceptionDeclarator::class;
33+
$this->declarator = new $definerClass(...$config['handlerExceptionDeclarator']['arguments'] ?? []);
34+
35+
foreach ($config['items'] as $retryName => $configNode) {
2836
$result[$retryName] = new Config(
2937
(string) $retryName,
3038
(string) $configNode['exception'],

src/ExceptionHandler.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace ApacheBorys\Retry;
55

6+
use ApacheBorys\Retry\Interfaces\HandlerExceptionDeclaratorInterface;
67
use ApacheBorys\Retry\Entity\{Config, FormulaItem, Message};
78
use ApacheBorys\Retry\Exceptions\WrongArgument;
89
use ApacheBorys\Retry\ValueObject\{ArgumentType, FormulaArgument};
@@ -14,9 +15,12 @@ class ExceptionHandler extends AbstractHandler
1415
*/
1516
public function initHandler(): void
1617
{
17-
set_exception_handler(
18-
$this->getHandling($this->config)
19-
);
18+
$this->declarator->initHandler($this->getHandling($this->config));
19+
}
20+
21+
public function getDeclarator(): HandlerExceptionDeclaratorInterface
22+
{
23+
return $this->declarator;
2024
}
2125

2226
/**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ApacheBorys\Retry\HandlerExceptionDefiner;
5+
6+
use ApacheBorys\Retry\Interfaces\HandlerExceptionDeclaratorInterface;
7+
8+
class StandardHandlerExceptionDeclarator implements HandlerExceptionDeclaratorInterface
9+
{
10+
public function initHandler(callable $callback): void
11+
{
12+
set_exception_handler($callback);
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ApacheBorys\Retry\Interfaces;
5+
6+
interface HandlerExceptionDeclaratorInterface
7+
{
8+
/**
9+
* Please initialise here, callback from argument as handler for exceptions. According to environment, it can be
10+
* different ways. For example: if we are in the framework, usually @see set_exception_handler already used by
11+
* core functionality. And it's not possible to replace it, because it will break framework. So, in this case, we
12+
* should implement event listener what can trigger that callback. That functionality, should be part of bridge
13+
* bundle for specific framework
14+
*/
15+
public function initHandler(callable $callback): void;
16+
}

tests/Functional/Transport/FileTransportForTests.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public function howManyTriesWasBefore(\Throwable $exception, Config $config): in
3939

4040
fclose($handle);
4141

42-
return count((array) $messages[getenv(self::ENV_VAR_FOR_CORRELATION_ID) ?? '']);
42+
return isset($messages[getenv(self::ENV_VAR_FOR_CORRELATION_ID) ?? '']) ?
43+
count((array) $messages[getenv(self::ENV_VAR_FOR_CORRELATION_ID) ?? '']) : 0;
4344
}
4445

4546
public function fetchUnprocessedMessages(int $batchSize = -1): ?iterable

tests/Functional/config.json

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
{
2-
"test": {
3-
"exception": "ApacheBorys\\Retry\\Tests\\Functional\\Exceptions\\Mock",
4-
"maxRetries": 4,
5-
"formula": [
6-
{
7-
"operator": "+",
8-
"argument": "QTY_TRIES"
2+
"handlerExceptionDefiner": {
3+
"class": "ApacheBorys\\Retry\\HandlerExceptionDefiner\\StandardHandlerExceptionDeclarator",
4+
"arguments": []
5+
},
6+
"items": {
7+
"test": {
8+
"exception": "ApacheBorys\\Retry\\Tests\\Functional\\Exceptions\\Mock",
9+
"maxRetries": 4,
10+
"formula": [
11+
{
12+
"operator": "+",
13+
"argument": "QTY_TRIES"
14+
},
15+
{
16+
"operator": "*",
17+
"argument": "5"
18+
}
19+
],
20+
"transport": {
21+
"class": "ApacheBorys\\Retry\\Tests\\Functional\\Transport\\FileTransportForTests",
22+
"arguments": [
23+
"tests\/transport.data"
24+
]
925
},
10-
{
11-
"operator": "*",
12-
"argument": "5"
26+
"executor": {
27+
"class": "ApacheBorys\\Retry\\Tests\\Functional\\Executor\\Runtime",
28+
"arguments": []
1329
}
14-
],
15-
"transport": {
16-
"class": "ApacheBorys\\Retry\\Tests\\Functional\\Transport\\FileTransportForTests",
17-
"arguments": [
18-
"tests\/transport.data"
19-
]
20-
},
21-
"executor": {
22-
"class": "ApacheBorys\\Retry\\Tests\\Functional\\Executor\\Runtime",
23-
"arguments": []
2430
}
2531
}
2632
}

0 commit comments

Comments
 (0)