1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Models; |
4
|
|
|
|
5
|
|
|
use App\Events\SiteDown; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use UrlSigner; |
13
|
|
|
|
14
|
|
|
class UptimeMonitor extends Model |
15
|
|
|
{ |
16
|
|
|
const STATUS_ONLINE = 'online'; |
17
|
|
|
const STATUS_OFFLINE = 'offline'; |
18
|
|
|
const STATUS_NEVER_CHECKED = 'never checked'; |
19
|
|
|
|
20
|
|
|
protected $guarded = []; |
21
|
|
|
|
22
|
|
|
protected $dates = ['last_checked_on']; |
23
|
|
|
|
24
|
|
|
public function shouldRun() : bool |
25
|
|
|
{ |
26
|
|
|
if (is_null($this->last_checked_on)) { |
27
|
|
|
return true; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($this->status === self::STATUS_OFFLINE) { |
31
|
|
|
return true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->last_checked_on->diffInMinutes() >= $this->ping_every_minutes; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function pingSucceeded($responseHtml) |
38
|
|
|
{ |
39
|
|
|
$this->status = self::STATUS_ONLINE; |
40
|
|
|
$this->last_failure_reason = ''; |
41
|
|
|
|
42
|
|
|
$wasFailing = $this->times_failed_in_a_row > 0; |
43
|
|
|
|
44
|
|
|
$this->times_failed_in_a_row = 0; |
45
|
|
|
$this->last_checked_on = Carbon::now(); |
46
|
|
|
|
47
|
|
|
$this->save(); |
48
|
|
|
|
49
|
|
|
$eventClass = 'App\\Events\\'.($wasFailing ? 'SiteRestored' : 'SiteUp'); |
50
|
|
|
|
51
|
|
|
event(new $eventClass($this)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function lookForStringPresentOnResponse(string $responseHtml = '') : bool |
55
|
|
|
{ |
56
|
|
|
if ($this->look_for_string == '') { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return str_contains($responseHtml, $this->look_for_string); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function pingFailed(string $reason) |
64
|
|
|
{ |
65
|
|
|
$this->status = self::STATUS_OFFLINE; |
66
|
|
|
|
67
|
|
|
$this->times_failed_in_a_row++; |
68
|
|
|
|
69
|
|
|
$this->last_checked_on = Carbon::now(); |
70
|
|
|
|
71
|
|
|
$this->last_failure_reason = $reason; |
72
|
|
|
|
73
|
|
|
$this->save(); |
74
|
|
|
|
75
|
|
|
event(new SiteDown($this)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getCacheKey() : string |
79
|
|
|
{ |
80
|
|
|
return "{$this->getPingRequestMethod()}:{$this->url}"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getPingRequestMethod() : string |
84
|
|
|
{ |
85
|
|
|
return $this->look_for_string == '' ? 'HEAD' : 'GET'; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|