1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Symfony\Component\Process\Process; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
9
|
|
|
use Spatie\ServerMonitor\Events\CheckRestored; |
10
|
|
|
use Spatie\ServerMonitor\Models\Enums\CheckStatus; |
11
|
|
|
use Spatie\ServerMonitor\Models\Concerns\HasProcess; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
13
|
|
|
use Spatie\ServerMonitor\CheckDefinitions\CheckDefinition; |
14
|
|
|
use Spatie\ServerMonitor\Models\Presenters\CheckPresenter; |
15
|
|
|
use Spatie\ServerMonitor\Exceptions\InvalidCheckDefinition; |
16
|
|
|
use Spatie\ServerMonitor\Models\Concerns\HandlesCheckResult; |
17
|
|
|
use Spatie\ServerMonitor\Models\Concerns\HasCustomProperties; |
18
|
|
|
use Spatie\ServerMonitor\Models\Concerns\ThrottlesFailingNotifications; |
19
|
|
|
|
20
|
|
|
class Check extends Model |
21
|
|
|
{ |
22
|
|
|
use CheckPresenter, |
23
|
|
|
HasCustomProperties, |
24
|
|
|
ThrottlesFailingNotifications, |
25
|
|
|
HasProcess, |
26
|
|
|
HandlesCheckResult; |
27
|
|
|
|
28
|
|
|
public $guarded = []; |
29
|
|
|
|
30
|
|
|
public $casts = [ |
31
|
|
|
'custom_properties' => 'array', |
32
|
|
|
'last_run_output' => 'array', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
public $dates = [ |
36
|
|
|
'last_ran_at', 'next_check_at', 'started_throttling_failing_notifications_at', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
public function getTable(): String |
40
|
|
|
{ |
41
|
|
|
return config('server-monitor.checks_table'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function host(): BelongsTo |
45
|
|
|
{ |
46
|
|
|
return $this->belongsTo(Host::class); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function scopeHealthy($query) |
50
|
|
|
{ |
51
|
|
|
return $query->where('status', CheckStatus::SUCCESS); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function scopeUnhealthy($query) |
55
|
|
|
{ |
56
|
|
|
return $query->where('status', '!=', CheckStatus::SUCCESS); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function scopeEnabled(Builder $query) |
60
|
|
|
{ |
61
|
|
|
$query->where('enabled', 1); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function shouldRun(): bool |
65
|
|
|
{ |
66
|
|
|
if (! $this->enabled) { |
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (is_null($this->last_ran_at)) { |
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return ! $this->last_ran_at |
|
|
|
|
75
|
|
|
->addMinutes($this->next_run_in_minutes) |
|
|
|
|
76
|
|
|
->isFuture(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getDefinition(): CheckDefinition |
80
|
|
|
{ |
81
|
|
|
if (! $definitionClass = config("server-monitor.checks.{$this->type}")) { |
|
|
|
|
82
|
|
|
throw InvalidCheckDefinition::unknownCheckType($this); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (! class_exists($definitionClass)) { |
86
|
|
|
throw InvalidCheckDefinition::definitionClassDoesNotExist($this, $definitionClass); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return app($definitionClass)->setCheck($this); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function handleFinishedProcess() |
93
|
|
|
{ |
94
|
|
|
$originalStatus = $this->status; |
|
|
|
|
95
|
|
|
|
96
|
|
|
$this->getDefinition()->determineResult($this->getProcess()); |
97
|
|
|
|
98
|
|
|
$this->scheduleNextRun(); |
99
|
|
|
|
100
|
|
|
if ($this->shouldFireRestoredEvent($originalStatus, $this->status)) { |
|
|
|
|
101
|
|
|
event(new CheckRestored($this)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function shouldFireRestoredEvent(?string $originalStatus, ?string $newStatus) |
108
|
|
|
{ |
109
|
|
|
if (! in_array($originalStatus, [CheckStatus::FAILED, CheckStatus::WARNING])) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $newStatus === CheckStatus::SUCCESS; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function scheduleNextRun() |
117
|
|
|
{ |
118
|
|
|
$this->last_ran_at = Carbon::now(); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$this->next_run_in_minutes = $this->getDefinition()->performNextRunInMinutes(); |
|
|
|
|
121
|
|
|
$this->save(); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function hasStatus(string $status): bool |
127
|
|
|
{ |
128
|
|
|
return $this->status === $status; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function storeProcessOutput(Process $process) |
132
|
|
|
{ |
133
|
|
|
$this->last_run_output = [ |
|
|
|
|
134
|
|
|
'output' => $process->getOutput(), |
135
|
|
|
'error_output' => $process->getErrorOutput(), |
136
|
|
|
'exit_code' => $process->getExitCode(), |
137
|
|
|
'exit_code_text' => $process->getExitCodeText(), |
138
|
|
|
]; |
139
|
|
|
|
140
|
|
|
$this->save(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.