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