|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Services\PingMonitors; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\UptimeMonitor\Models\PingMonitor; |
|
6
|
|
|
use Cache; |
|
7
|
|
|
use Generator; |
|
8
|
|
|
use GuzzleHttp\Client; |
|
9
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
10
|
|
|
use GuzzleHttp\Promise\EachPromise; |
|
11
|
|
|
use GuzzleHttp\Promise\FulfilledPromise; |
|
12
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
|
13
|
|
|
use GuzzleHttp\Promise\RejectedPromise; |
|
14
|
|
|
use GuzzleHttp\Psr7\Request; |
|
15
|
|
|
use Illuminate\Support\Collection; |
|
16
|
|
|
use Log; |
|
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
18
|
|
|
use Spatie\UptimeMonitor\Models\UptimeMonitor; |
|
19
|
|
|
|
|
20
|
|
|
class UptimeMonitorCollection extends Collection |
|
21
|
|
|
{ |
|
22
|
|
|
public function check() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->resetItemKeys(); |
|
25
|
|
|
|
|
26
|
|
|
Log::info("Start checking for {$this->count()} monitors..."); |
|
27
|
|
|
|
|
28
|
|
|
(new EachPromise($this->getPromises(), [ |
|
29
|
|
|
'concurrency' => 100, |
|
30
|
|
|
'fulfilled' => function (ResponseInterface $response, $index) { |
|
31
|
|
|
$pingMonitor = $this->items[$index]; |
|
32
|
|
|
|
|
33
|
|
|
$this->log('fulfilled ping', $pingMonitor); |
|
34
|
|
|
|
|
35
|
|
|
$pingMonitor->pingSucceeded($response->getBody()); |
|
36
|
|
|
|
|
37
|
|
|
$this->cacheResponse($response, $pingMonitor); |
|
38
|
|
|
}, |
|
39
|
|
|
|
|
40
|
|
|
'rejected' => function (RequestException $exception, $index) { |
|
41
|
|
|
$pingMonitor = $this->items[$index]; |
|
42
|
|
|
|
|
43
|
|
|
$this->log("rejected ping because: {$exception->getMessage()}", $pingMonitor); |
|
44
|
|
|
|
|
45
|
|
|
$pingMonitor->pingFailed($exception->getMessage()); |
|
46
|
|
|
|
|
47
|
|
|
$this->cacheException($exception, $pingMonitor); |
|
48
|
|
|
}, |
|
49
|
|
|
]))->promise()->wait(); |
|
50
|
|
|
|
|
51
|
|
|
Log::info('Checking done!'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function getPromises() : Generator |
|
55
|
|
|
{ |
|
56
|
|
|
$client = new Client([ |
|
57
|
|
|
'headers' => [ |
|
58
|
|
|
'User-Agent' => 'spatie/laravel-uptime-monitor uptime checker', |
|
59
|
|
|
], |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
|
|
foreach ($this->items as $pingMonitor) { |
|
63
|
|
|
$this->log('checking', $pingMonitor); |
|
64
|
|
|
|
|
65
|
|
|
$promise = $this->getCachedResponse($pingMonitor); |
|
66
|
|
|
|
|
67
|
|
|
if (!$promise instanceof PromiseInterface) { |
|
68
|
|
|
$this->log('use cached response', $pingMonitor); |
|
69
|
|
|
|
|
70
|
|
|
$promise = $client->requestAsync( |
|
71
|
|
|
$pingMonitor->getPingRequestMethod(), |
|
72
|
|
|
$pingMonitor->url, |
|
73
|
|
|
['connect_timeout' => 10] |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
yield $promise; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param \Spatie\UptimeMonitor\Models\UptimeMonitor $uptimeMonitor |
|
83
|
|
|
* |
|
84
|
|
|
* @return bool|PromiseInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function getCachedResponse(UptimeMonitor $uptimeMonitor) |
|
87
|
|
|
{ |
|
88
|
|
|
$cachedResult = Cache::get($uptimeMonitor->getCacheKey()); |
|
89
|
|
|
|
|
90
|
|
|
if ($cachedResult instanceof ResponseInterface) { |
|
91
|
|
|
return new FulfilledPromise($cachedResult); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (is_string($cachedResult)) { |
|
95
|
|
|
return new RejectedPromise( |
|
96
|
|
|
new RequestException($cachedResult, new Request($uptimeMonitor->getPingRequestMethod(), $uptimeMonitor->url)) |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function cacheResponse(ResponseInterface $response, PingMonitor $pingMonitor) |
|
104
|
|
|
{ |
|
105
|
|
|
Cache::put($pingMonitor->getCacheKey(), $response, 1); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function cacheException(RequestException $exception, PingMonitor $pingMonitor) |
|
109
|
|
|
{ |
|
110
|
|
|
Cache::put($pingMonitor->getCacheKey(), $exception->getMessage(), 1); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Make sure the keys are in consecutive order without gaps. |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function resetItemKeys() |
|
117
|
|
|
{ |
|
118
|
|
|
$this->items = $this->values()->all(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function log($message, PingMonitor $pingMonitor) |
|
122
|
|
|
{ |
|
123
|
|
|
Log::info("$message (url: {$pingMonitor->url} id: {$pingMonitor->id})"); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|