1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Darkilliant\ProcessBundle\ProcessNotifier; |
6
|
|
|
|
7
|
|
|
use Darkilliant\ProcessBundle\State\ProcessState; |
8
|
|
|
use Darkilliant\ProcessBundle\Step\StepInterface; |
9
|
|
|
|
10
|
|
|
class BreakerProcessNotifier extends AbstractProcessNotifier |
11
|
|
|
{ |
12
|
|
|
private $breaker = null; |
13
|
|
|
|
14
|
|
|
private $count = 0; |
15
|
|
|
private $startedAt; |
16
|
|
|
private $timeElasped = 0; |
17
|
|
|
|
18
|
1 |
|
public function onStartProcess(ProcessState $state, StepInterface $step) |
19
|
|
|
{ |
20
|
|
|
// TODO: Implement onStartProcess() method. |
21
|
1 |
|
} |
22
|
|
|
|
23
|
7 |
|
public function onStartIterableProcess(ProcessState $state, StepInterface $step) |
24
|
|
|
{ |
25
|
7 |
|
$this->breaker = null; |
26
|
|
|
|
27
|
7 |
|
if (!$state->getOptions()['breaker']) { |
28
|
1 |
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
6 |
|
$maxIteration = $state->getOptions()['breaker_max_iteration']; |
32
|
6 |
|
$maxTime = $state->getOptions()['breaker_max_time']; |
33
|
6 |
|
$sleepBetween = $state->getOptions()['breaker_sleep_between']; |
34
|
|
|
|
35
|
6 |
|
$this->breaker = ['max_time' => $maxTime, 'max_iteration' => $maxIteration, 'sleep_between' => $sleepBetween]; |
36
|
|
|
|
37
|
6 |
|
$state->info('breaker', $this->breaker); |
38
|
|
|
|
39
|
6 |
|
$this->count = 0; |
40
|
6 |
|
$this->startedAt = time(); |
41
|
6 |
|
$this->timeElasped = 0; |
42
|
6 |
|
} |
43
|
|
|
|
44
|
6 |
|
public function onUpdateIterableProcess(ProcessState $state, StepInterface $step) |
45
|
|
|
{ |
46
|
6 |
|
if (!$this->breaker) { |
47
|
1 |
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
$this->timeElasped = time() - $this->startedAt; |
51
|
5 |
|
++$this->count; |
52
|
5 |
|
sleep($this->breaker['sleep_between']); |
53
|
|
|
|
54
|
5 |
|
if (!$this->isValid()) { |
55
|
3 |
|
$state->info('breaker stop current loop'); |
56
|
3 |
|
$state->markBreak(); |
57
|
|
|
|
58
|
3 |
|
return; |
59
|
|
|
} |
60
|
2 |
|
} |
61
|
|
|
|
62
|
1 |
|
public function onEndProcess(ProcessState $state, StepInterface $step) |
63
|
|
|
{ |
64
|
|
|
// TODO: Implement onEndProcess() method. |
65
|
1 |
|
} |
66
|
|
|
|
67
|
1 |
|
public function onExecutedProcess(ProcessState $state, StepInterface $step) |
68
|
|
|
{ |
69
|
|
|
// TODO: Implement onExecutedProcess() method. |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
public function onFailedLoop(ProcessState $state, StepInterface $step) |
73
|
|
|
{ |
74
|
1 |
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function onSuccessLoop(ProcessState $state, StepInterface $step) |
78
|
|
|
{ |
79
|
1 |
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
private function isValid() |
83
|
|
|
{ |
84
|
5 |
|
if (null !== $this->breaker['max_time'] && $this->timeElasped >= $this->breaker['max_time']) { |
85
|
2 |
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
if (null !== $this->breaker['max_iteration'] && $this->count >= $this->breaker['max_iteration']) { |
89
|
1 |
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
return true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|