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