1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Models\Traits; |
4
|
|
|
|
5
|
|
|
use Spatie\UptimeMonitor\Events\UptimeCheckFailed; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Spatie\UptimeMonitor\Events\UptimeCheckRecovered; |
8
|
|
|
use Spatie\UptimeMonitor\Events\UptimeCheckSucceeded; |
9
|
|
|
use Spatie\UptimeMonitor\Models\Monitor; |
10
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
11
|
|
|
|
12
|
|
|
trait SupportsUptimeCheck |
13
|
|
|
{ |
14
|
|
|
public static function bootSupportsUptimeCheck() |
15
|
|
|
{ |
16
|
|
|
static::saving(function (Monitor $monitor) { |
17
|
|
|
if (is_null($monitor->uptime_status_last_change_date)) { |
18
|
|
|
$monitor->uptime_status_last_change_date = Carbon::now(); |
19
|
|
|
|
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if ($monitor->getOriginal('uptime_status') != $monitor->uptime_status) { |
24
|
|
|
$monitor->uptime_status_last_change_date = Carbon::now(); |
25
|
|
|
} |
26
|
|
|
}); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function shouldCheckUptime() : bool |
30
|
|
|
{ |
31
|
|
|
if (!$this->uptime_check_enabled) { |
|
|
|
|
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ($this->uptime_status == UptimeStatus::NOT_YET_CHECKED) { |
|
|
|
|
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($this->uptime_status == UptimeStatus::DOWN) { |
40
|
|
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (is_null($this->uptime_last_check_date)) { |
|
|
|
|
44
|
|
|
return true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->uptime_last_check_date->diffInMinutes() >= $this->uptime_check_interval_in_minutes; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function uptimeRequestSucceeded($responseHtml) |
51
|
|
|
{ |
52
|
|
|
if (!str_contains($responseHtml, $this->look_for_string)) { |
53
|
|
|
$this->uptimeCheckFailed("String `{$this->look_for_string}` was not found on the response."); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->uptimeCheckSucceeded(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function uptimeRequestFailed(string $reason) |
60
|
|
|
{ |
61
|
|
|
$this->uptimeCheckFailed($reason); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function uptimeCheckSucceeded() |
65
|
|
|
{ |
66
|
|
|
$this->uptime_status = UptimeStatus::UP; |
67
|
|
|
$this->uptime_check_failure_reason = ''; |
|
|
|
|
68
|
|
|
|
69
|
|
|
$wasFailing = !is_null($this->uptime_check_failed_event_fired_on_date); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$this->uptime_check_times_failed_in_a_row = 0; |
|
|
|
|
72
|
|
|
$this->uptime_last_check_date = Carbon::now(); |
73
|
|
|
$this->uptime_check_failed_event_fired_on_date = null; |
74
|
|
|
$this->save(); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if ($wasFailing) { |
77
|
|
|
$this->fireRecoveredEvent(); |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
event(new UptimeCheckSucceeded($this)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function uptimeCheckFailed(string $reason) |
85
|
|
|
{ |
86
|
|
|
$this->uptime_status = UptimeStatus::DOWN; |
87
|
|
|
$this->uptime_check_times_failed_in_a_row++; |
88
|
|
|
$this->uptime_last_check_date = Carbon::now(); |
89
|
|
|
$this->uptime_check_failure_reason = $reason; |
90
|
|
|
$this->save(); |
|
|
|
|
91
|
|
|
|
92
|
|
|
if ($this->shouldFireUptimeCheckFailedEvent()) { |
93
|
|
|
$this->uptime_check_failed_event_fired_on_date = Carbon::now(); |
94
|
|
|
$this->save(); |
|
|
|
|
95
|
|
|
|
96
|
|
|
event(new UptimeCheckFailed($this)); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function fireRecoveredEvent() |
101
|
|
|
{ |
102
|
|
|
if ($this->uptime_status_last_change_date) { |
|
|
|
|
103
|
|
|
|
104
|
|
|
$uptimeCheckStartedFailingOnDate = clone $this->uptime_status_last_change_date; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
event(new UptimeCheckRecovered($this, $uptimeCheckStartedFailingOnDate)); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function shouldFireUptimeCheckFailedEvent(): bool |
111
|
|
|
{ |
112
|
|
|
if ($this->uptime_check_times_failed_in_a_row === config('laravel-uptime-monitor.uptime_check.fire_monitor_failed_event_after_consecutive_failures')) { |
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (is_null($this->uptime_check_failed_event_fired_on_date)) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (config('laravel-uptime-monitor.notifications.resend_uptime_check_failed_notification_every_minutes') === 0) { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($this->uptime_check_failed_event_fired_on_date->diffInMinutes() >= config('laravel-uptime-monitor.notifications.resend_uptime_check_failed_notification_every_minutes')) { |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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: