1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Checker; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
7
|
|
|
use GuzzleHttp\Promise\EachPromise; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Spatie\UptimeMonitor\Helpers\ConsoleOutput; |
10
|
|
|
use Spatie\UptimeMonitor\MonitorCollection; |
11
|
|
|
|
12
|
|
|
class HTTPChecker extends Checker |
13
|
|
|
{ |
14
|
|
View Code Duplication |
public function check(MonitorCollection $monitors) |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$monitors->resetItemKeys(); |
17
|
|
|
$this->monitors = $monitors; |
18
|
|
|
(new EachPromise($this->getPromises($monitors), [ |
19
|
|
|
'concurrency' => config('laravel-uptime-monitor.uptime_check.concurrent_checks'), |
20
|
|
|
'fulfilled' => function (ResponseInterface $response, $index) { |
21
|
|
|
$monitor = $this->monitors->getMonitorAtIndex($index); |
22
|
|
|
|
23
|
|
|
ConsoleOutput::info("Could reach {$monitor->url}"); |
24
|
|
|
|
25
|
|
|
$monitor->uptimeRequestSucceeded($response); |
26
|
|
|
}, |
27
|
|
|
'rejected' => function (RequestException $exception, $index) { |
28
|
|
|
$monitor = $this->monitors->getMonitorAtIndex($index); |
29
|
|
|
ConsoleOutput::error("Could not reach {$monitor->url} error: `{$exception->getMessage()}`"); |
30
|
|
|
$monitor->uptimeRequestFailed($exception->getMessage()); |
31
|
|
|
}, |
32
|
|
|
]))->promise()->wait(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function getPromises(MonitorCollection $monitors): \Generator |
36
|
|
|
{ |
37
|
|
|
$client = new Client([ |
38
|
|
|
'headers' => [ |
39
|
|
|
'User-Agent' => config('laravel-uptime-monitor.uptime_check.user_agent'), |
40
|
|
|
], |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
foreach ($monitors as $monitor) { |
44
|
|
|
ConsoleOutput::info("checking {$monitor->url}"); |
45
|
|
|
$promise = $client->requestAsync( |
46
|
|
|
$monitor->uptime_check_method, |
47
|
|
|
$monitor->url, |
48
|
|
|
['connect_timeout' => config('laravel-uptime-monitor.uptime_check.timeout_per_site')] |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
yield $promise; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.