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