-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathRetry.php
More file actions
82 lines (69 loc) · 2.36 KB
/
Retry.php
File metadata and controls
82 lines (69 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
namespace Codeception\Step;
use Codeception\Lib\ModuleContainer;
use Codeception\Util\Template;
use Exception;
use function codecept_debug;
use function ucfirst;
use function usleep;
class Retry extends Assertion implements GeneratedStep
{
protected static string $methodTemplate = <<<EOF
/**
* [!] Method is generated.
*
* {{doc}}
*
* Retry number and interval set by \$I->retry();
*
* @see \{{module}}::{{method}}()
*/
public function {{action}}({{params}}) {
\$retryNum = isset(\$this->retryNum) ? \$this->retryNum : 1;
\$retryInterval = isset(\$this->retryInterval) ? \$this->retryInterval : 200;
return \$this->getScenario()->runStep(new \Codeception\Step\Retry('{{method}}', func_get_args(), \$retryNum, \$retryInterval));
}
EOF;
public function __construct($action, array $arguments, private int $retryNum, private int $retryInterval)
{
$this->action = $action;
$this->arguments = $arguments;
}
public function run(ModuleContainer $container = null)
{
$retry = 0;
$interval = $this->retryInterval;
while (true) {
try {
$this->isTry = $retry < $this->retryNum;
return parent::run($container);
} catch (Exception $e) {
++$retry;
if (!$this->isTry) {
throw $e;
}
codecept_debug("Retrying #{$retry} in {$interval}ms");
usleep($interval * 1000);
$interval *= 2;
}
}
}
public static function getTemplate(Template $template): ?Template
{
$action = $template->getVar('action');
if ((str_starts_with($action, 'have')) || (str_starts_with($action, 'am'))) {
return null; // dont retry conditions
}
if (str_starts_with($action, 'wait')) {
return null; // dont retry waiters
}
$doc = "* Executes {$action} and retries on failure.";
return (new Template(self::$methodTemplate))
->place('method', $template->getVar('method'))
->place('module', $template->getVar('module'))
->place('params', $template->getVar('params'))
->place('doc', $doc)
->place('action', 'retry' . ucfirst($action));
}
}