|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor; |
|
4
|
|
|
|
|
5
|
|
|
use Generator; |
|
6
|
|
|
use GrahamCampbell\GuzzleFactory\GuzzleFactory; |
|
7
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
8
|
|
|
use GuzzleHttp\Promise\EachPromise; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Spatie\UptimeMonitor\Helpers\ConsoleOutput; |
|
12
|
|
|
use Spatie\UptimeMonitor\Models\Monitor; |
|
13
|
|
|
|
|
14
|
|
|
class MonitorCollection extends Collection |
|
15
|
|
|
{ |
|
16
|
|
|
public function checkUptime(): void |
|
17
|
|
|
{ |
|
18
|
|
|
$this->resetItemKeys(); |
|
19
|
|
|
|
|
20
|
|
|
(new EachPromise($this->getPromises(), [ |
|
21
|
|
|
'concurrency' => config('uptime-monitor.uptime_check.concurrent_checks'), |
|
22
|
|
|
'fulfilled' => function (ResponseInterface $response, $index) { |
|
23
|
|
|
$monitor = $this->getMonitorAtIndex($index); |
|
24
|
|
|
|
|
25
|
|
|
ConsoleOutput::info("Could reach {$monitor->url}"); |
|
26
|
|
|
|
|
27
|
|
|
$monitor->uptimeRequestSucceeded($response); |
|
28
|
|
|
}, |
|
29
|
|
|
|
|
30
|
|
|
'rejected' => function (RequestException $exception, $index) { |
|
31
|
|
|
$monitor = $this->getMonitorAtIndex($index); |
|
32
|
|
|
|
|
33
|
|
|
ConsoleOutput::error("Could not reach {$monitor->url} error: `{$exception->getMessage()}`"); |
|
34
|
|
|
|
|
35
|
|
|
$monitor->uptimeRequestFailed($exception->getMessage()); |
|
36
|
|
|
}, |
|
37
|
|
|
]))->promise()->wait(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function getPromises(): Generator |
|
41
|
|
|
{ |
|
42
|
|
|
$client = GuzzleFactory::make( |
|
43
|
|
|
config('uptime-monitor.uptime_check.guzzle_options', []), |
|
44
|
|
|
config('uptime-monitor.uptime-check.retry_connection_after_milliseconds', 100) |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
foreach ($this->items as $monitor) { |
|
48
|
|
|
ConsoleOutput::info("Checking {$monitor->url}"); |
|
49
|
|
|
|
|
50
|
|
|
$promise = $client->requestAsync( |
|
51
|
|
|
$monitor->uptime_check_method, |
|
52
|
|
|
$monitor->url, |
|
53
|
|
|
array_filter([ |
|
54
|
|
|
'connect_timeout' => config('uptime-monitor.uptime_check.timeout_per_site'), |
|
55
|
|
|
'headers' => $this->promiseHeaders($monitor), |
|
56
|
|
|
'body' => $monitor->uptime_check_payload, |
|
57
|
|
|
]) |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
yield $promise; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function promiseHeaders(Monitor $monitor): array |
|
65
|
|
|
{ |
|
66
|
|
|
return collect([]) |
|
67
|
|
|
->merge(['User-Agent' => config('uptime-monitor.uptime_check.user_agent')]) |
|
68
|
|
|
->merge(config('uptime-monitor.uptime_check.additional_headers') ?? []) |
|
69
|
|
|
->merge($monitor->uptime_check_additional_headers) |
|
70
|
|
|
->toArray(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* In order to make use of Guzzle promises we have to make sure the |
|
75
|
|
|
* keys of the collection are in a consecutive order without gaps. |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function resetItemKeys(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->items = $this->values()->all(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function getMonitorAtIndex(int $index): Monitor |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->items[$index]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function sortByHost(): self |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->sortBy(function (Monitor $monitor) { |
|
90
|
|
|
return $monitor->url->getHost(); |
|
91
|
|
|
}); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|