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 ($this->shouldLookForStringOnResponse() && ! str_contains((string)$responseHtml, $this->look_for_string)) { |
53
|
|
|
$this->uptimeCheckFailed("String `{$this->look_for_string}` was not found on the response."); |
|
|
|
|
54
|
|
|
|
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->uptimeCheckSucceeded(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function shouldLookForStringOnResponse(): bool |
62
|
|
|
{ |
63
|
|
|
return ! empty($this->look_for_string); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function uptimeRequestFailed(string $reason) |
67
|
|
|
{ |
68
|
|
|
$this->uptimeCheckFailed($reason); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function uptimeCheckSucceeded() |
72
|
|
|
{ |
73
|
|
|
$this->uptime_status = UptimeStatus::UP; |
74
|
|
|
$this->uptime_check_failure_reason = ''; |
|
|
|
|
75
|
|
|
|
76
|
|
|
$wasFailing = ! is_null($this->uptime_check_failed_event_fired_on_date); |
|
|
|
|
77
|
|
|
$lastStatusChangeDate = $this->uptime_status_last_change_date ? clone $this->uptime_status_last_change_date : null; |
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->uptime_check_times_failed_in_a_row = 0; |
|
|
|
|
80
|
|
|
$this->uptime_last_check_date = Carbon::now(); |
81
|
|
|
$this->uptime_check_failed_event_fired_on_date = null; |
82
|
|
|
$this->save(); |
|
|
|
|
83
|
|
|
|
84
|
|
|
if ($wasFailing) { |
85
|
|
|
event(new UptimeCheckRecovered($this, $lastStatusChangeDate)); |
|
|
|
|
86
|
|
|
|
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
event(new UptimeCheckSucceeded($this)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function uptimeCheckFailed(string $reason) |
94
|
|
|
{ |
95
|
|
|
$this->uptime_status = UptimeStatus::DOWN; |
96
|
|
|
$this->uptime_check_times_failed_in_a_row++; |
97
|
|
|
$this->uptime_last_check_date = Carbon::now(); |
98
|
|
|
$this->uptime_check_failure_reason = $reason; |
99
|
|
|
$this->save(); |
|
|
|
|
100
|
|
|
|
101
|
|
|
if ($this->shouldFireUptimeCheckFailedEvent()) { |
102
|
|
|
$this->uptime_check_failed_event_fired_on_date = Carbon::now(); |
103
|
|
|
$this->save(); |
|
|
|
|
104
|
|
|
|
105
|
|
|
event(new UptimeCheckFailed($this)); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function shouldFireUptimeCheckFailedEvent(): bool |
110
|
|
|
{ |
111
|
|
|
if ($this->uptime_check_times_failed_in_a_row === config('laravel-uptime-monitor.uptime_check.fire_monitor_failed_event_after_consecutive_failures')) { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (is_null($this->uptime_check_failed_event_fired_on_date)) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (config('laravel-uptime-monitor.notifications.resend_uptime_check_failed_notification_every_minutes') === 0) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($this->uptime_check_failed_event_fired_on_date->diffInMinutes() >= config('laravel-uptime-monitor.notifications.resend_uptime_check_failed_notification_every_minutes')) { |
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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: