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