1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Models\Traits; |
4
|
|
|
|
5
|
|
|
use Spatie\UptimeMonitor\Events\SiteDown; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Spatie\UptimeMonitor\Events\SiteRestored; |
8
|
|
|
use Spatie\UptimeMonitor\Events\SiteUp; |
9
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
10
|
|
|
|
11
|
|
|
trait SupportsUptimeCheck |
12
|
|
|
{ |
13
|
|
|
public static function bootSupportsUptimeCheck() |
14
|
|
|
{ |
15
|
|
|
dd('omg it booted'); |
16
|
|
|
static::saving(function (Site $site) { |
17
|
|
|
if (is_null($site->uptime_status_last_change_date)) { |
18
|
|
|
$site->uptime_status_last_change_date = Carbon::now(); |
19
|
|
|
|
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if ($site->getOriginal('uptime_status') != $site->uptime_status) { |
24
|
|
|
$site->uptime_status_last_change_date = Carbon::now(); |
25
|
|
|
} |
26
|
|
|
}); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function shouldCheckUptime() : bool |
31
|
|
|
{ |
32
|
|
|
if (!$this->enabled) { |
|
|
|
|
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($this->uptime_status == UptimeStatus::NOT_YET_CHECKED) { |
|
|
|
|
37
|
|
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($this->uptime_status == UptimeStatus::DOWN) { |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (is_null($this->uptime_last_check_date)) { |
|
|
|
|
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->uptime_last_check_date->diffInMinutes() >= $this->uptime_check_interval_in_minutes; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function couldReachSite($responseHtml) |
52
|
|
|
{ |
53
|
|
|
if (!str_contains($responseHtml, $this->look_for_string)) { |
54
|
|
|
$this->siteIsDown("String `{$this->look_for_string}` was not found on the response."); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->siteIsUp(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function couldNotReachSite(string $reason) |
61
|
|
|
{ |
62
|
|
|
$this->siteIsDown($reason); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function siteIsUp() |
66
|
|
|
{ |
67
|
|
|
$this->uptime_status = UptimeStatus::UP; |
68
|
|
|
$this->uptime_failure_reason = ''; |
|
|
|
|
69
|
|
|
|
70
|
|
|
$wasFailing = !is_null($this->down_event_fired_on_date); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$this->uptime_check_times_failed_in_a_row = 0; |
|
|
|
|
73
|
|
|
$this->uptime_last_check_date = Carbon::now(); |
74
|
|
|
$this->down_event_fired_on_date = null; |
75
|
|
|
$this->save(); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$eventClass = ($wasFailing ? SiteRestored::class : SiteUp::class); |
78
|
|
|
|
79
|
|
|
event(new $eventClass($this)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function siteIsDown(string $reason) |
83
|
|
|
{ |
84
|
|
|
$this->uptime_status = UptimeStatus::DOWN; |
85
|
|
|
$this->uptime_check_times_failed_in_a_row++; |
86
|
|
|
$this->uptime_last_check_date = Carbon::now(); |
87
|
|
|
$this->uptime_failure_reason = $reason; |
88
|
|
|
$this->save(); |
|
|
|
|
89
|
|
|
|
90
|
|
|
if ($this->shouldFireDownEvent()) { |
91
|
|
|
|
92
|
|
|
$this->down_event_fired_on_date = Carbon::now(); |
93
|
|
|
$this->save(); |
|
|
|
|
94
|
|
|
|
95
|
|
|
event(new SiteDown($this)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function shouldFireDownEvent(): bool |
100
|
|
|
{ |
101
|
|
|
if ($this->uptime_check_times_failed_in_a_row === config('laravel-uptime-monitor.uptime_check.fire_down_event_after_consecutive_failures')) { |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (is_null($this->down_event_fired_on_date)) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (config('laravel-uptime-monitor.notifications.resend_down_notification_every_minutes') === 0) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($this->down_event_fired_on_date->diffInMinutes() >= config('laravel-uptime-monitor.notifications.resend_down_notification_every_minutes')) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: