1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\ApiHealth\Checkers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notification; |
6
|
|
|
use Illuminate\Queue\Events\JobProcessing; |
7
|
|
|
use Illuminate\Support\Facades\Queue; |
8
|
|
|
use Pbmedia\ApiHealth\Checkers\Checker; |
9
|
|
|
use Pbmedia\ApiHealth\Checkers\CheckerHasFailed; |
10
|
|
|
use Pbmedia\ApiHealth\Storage\CheckerState; |
11
|
|
|
|
12
|
|
|
class Executor |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The checker. |
16
|
|
|
* |
17
|
|
|
* @var \Pbmedia\ApiHealth\Checkers\Checker |
18
|
|
|
*/ |
19
|
|
|
private $checker; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The state of the checker. |
23
|
|
|
* |
24
|
|
|
* @var \Pbmedia\ApiHealth\Storage\CheckerState |
25
|
|
|
*/ |
26
|
|
|
private $state; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The caught exception if the checker fails. |
30
|
|
|
* |
31
|
|
|
* @var \Pbmedia\ApiHealth\Checkers\CheckerHasFailed |
32
|
|
|
*/ |
33
|
|
|
private $exception; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Boolean wether the checker has failed of not. |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
private $failed; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates an instance with the given checker |
44
|
|
|
* |
45
|
|
|
* @param \Pbmedia\ApiHealth\Checkers\Checker $checker |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Checker $checker) |
48
|
|
|
{ |
49
|
|
|
$this->checker = $checker; |
50
|
|
|
$this->state = new CheckerState($checker); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Shortcut for creating an instance for a checker class. |
55
|
|
|
* |
56
|
|
|
* @param string $checkerClass |
57
|
|
|
* @return \Pbmedia\ApiHealth\Checkers\Executor |
58
|
|
|
*/ |
59
|
|
|
public static function make(string $checkerClass) |
60
|
|
|
{ |
61
|
|
|
return new static($checkerClass::create()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns a boolean wether the checker passes. |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function passes(): bool |
70
|
|
|
{ |
71
|
|
|
if (is_null($this->failed)) { |
72
|
|
|
$this->handle(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return !$this->failed; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns a boolean wether the checker fails. |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function fails(): bool |
84
|
|
|
{ |
85
|
|
|
return !$this->passes(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the checker. |
90
|
|
|
* |
91
|
|
|
* @return \Pbmedia\ApiHealth\Checkers\Checker |
92
|
|
|
*/ |
93
|
|
|
public function getChecker(): Checker |
94
|
|
|
{ |
95
|
|
|
return $this->checker; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the caught exception. |
100
|
|
|
* |
101
|
|
|
* @return \Pbmedia\ApiHealth\Checkers\CheckerHasFailed |
102
|
|
|
*/ |
103
|
|
|
public function getException(): CheckerHasFailed |
104
|
|
|
{ |
105
|
|
|
return $this->exception; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Runs the checker, stores the state and lets events take |
110
|
|
|
* care of sending the notifications. |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function handle() |
115
|
|
|
{ |
116
|
|
|
$this->failed = false; |
117
|
|
|
|
118
|
|
|
try { |
119
|
|
|
$this->checker->run(); |
120
|
|
|
$this->state->setToPassing(); |
121
|
|
|
} catch (CheckerHasFailed $exception) { |
122
|
|
|
if ($this->state->retryIsAllowed()) { |
123
|
|
|
$this->handleAllowedRetry(); |
124
|
|
|
} else { |
125
|
|
|
$this->exception = $exception; |
126
|
|
|
$this->failed = true; |
127
|
|
|
$this->handleFailedChecker(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Handler for whenever the checker fails. Stores the state or adds a timestamp |
136
|
|
|
* to the state if the checker previously failed. |
137
|
|
|
* |
138
|
|
|
* @return null |
139
|
|
|
*/ |
140
|
|
|
private function handleFailedChecker() |
141
|
|
|
{ |
142
|
|
|
if ($this->state->exists() && $this->state->isFailing()) { |
143
|
|
|
return $this->state->addFailedTimestamp($this->exception); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->state->setToFailed($this->exception); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Adds a retry timestamp to the state of checker or dispaches |
151
|
|
|
* the retry job. |
152
|
|
|
* |
153
|
|
|
* @return null |
154
|
|
|
*/ |
155
|
|
|
private function handleAllowedRetry() |
156
|
|
|
{ |
157
|
|
|
if (!$this->state->exists()) { |
158
|
|
|
$this->state->setToPassing(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (!$jobClass = $this->checker->retryCheckerJob()) { |
162
|
|
|
return $this->state->addRetryTimestamp(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->addCallbackBeforeRetryCheckerJob($jobClass); |
166
|
|
|
|
167
|
|
|
$job = new $jobClass(get_class($this->checker)); |
168
|
|
|
|
169
|
|
|
if (method_exists($this->checker, 'withRetryJob')) { |
170
|
|
|
$this->checker->withRetryJob($job); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
dispatch($job); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Adds a callback before the retry job is executed which |
178
|
|
|
* will add a retry timestamp on the checker's state. |
179
|
|
|
* |
180
|
|
|
* @param string $job |
181
|
|
|
*/ |
182
|
|
|
private function addCallbackBeforeRetryCheckerJob(string $job) |
183
|
|
|
{ |
184
|
|
|
Queue::before(function (JobProcessing $event) use ($job) { |
185
|
|
|
$resolvedName = $event->job->resolveName(); |
186
|
|
|
|
187
|
|
|
$checkerClass = unserialize( |
188
|
|
|
$event->job->payload()['data']['command'] |
189
|
|
|
)->checkerClass; |
190
|
|
|
|
191
|
|
|
if ($resolvedName === $job && $checkerClass === get_class($this->checker)) { |
192
|
|
|
$this->state->addRetryTimestamp(); |
193
|
|
|
} |
194
|
|
|
}); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|