1 | <?php |
||
14 | class MonitorCollection extends Collection |
||
15 | { |
||
16 | public function checkUptime() |
||
39 | |||
40 | protected function getPromises(): Generator |
||
41 | { |
||
42 | $client = GuzzleFactory::make( |
||
43 | compact('headers'), |
||
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) |
||
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() |
||
81 | |||
82 | protected function getMonitorAtIndex(int $index): Monitor |
||
86 | |||
87 | /** |
||
88 | * @return static |
||
89 | */ |
||
90 | public function sortByHost() |
||
96 | } |
||
97 |