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