|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Backup\Tasks\Monitor; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use Spatie\Backup\Helpers\Format; |
|
8
|
|
|
use Spatie\Backup\BackupDestination\BackupDestination; |
|
9
|
|
|
|
|
10
|
|
|
class BackupDestinationStatus |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var \Spatie\Backup\BackupDestination\BackupDestination */ |
|
13
|
|
|
protected $backupDestination; |
|
14
|
|
|
|
|
15
|
|
|
/** @var int */ |
|
16
|
|
|
protected $maximumAgeOfNewestBackupInDays = 1; |
|
17
|
|
|
|
|
18
|
|
|
/** @var int */ |
|
19
|
|
|
protected $maximumStorageUsageInMegabytes = 5000; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $diskName; |
|
23
|
|
|
|
|
24
|
|
|
/** @var bool */ |
|
25
|
|
|
protected $reachable; |
|
26
|
|
|
|
|
27
|
|
|
/** @var array */ |
|
28
|
|
|
protected $inspections; |
|
29
|
|
|
|
|
30
|
|
|
/** @var HealthInspectionFailure|null */ |
|
31
|
|
|
protected $failedInspection; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(BackupDestination $backupDestination, string $diskName, array $inspections = []) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->backupDestination = $backupDestination; |
|
36
|
|
|
$this->diskName = $diskName; |
|
37
|
|
|
$this->inspections = $inspections; |
|
38
|
|
|
|
|
39
|
|
|
$this->reachable = $this->backupDestination->isReachable(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function setMaximumAgeOfNewestBackupInDays(int $days): self |
|
43
|
|
|
{ |
|
44
|
|
|
$this->maximumAgeOfNewestBackupInDays = $days; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function maximumAgeOfNewestBackupInDays(): int |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->maximumAgeOfNewestBackupInDays; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function setMaximumStorageUsageInMegabytes(float $megabytes): self |
|
55
|
|
|
{ |
|
56
|
|
|
$this->maximumStorageUsageInMegabytes = $megabytes; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function backupName(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->backupDestination->backupName(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function diskName(): string |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->diskName; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function amountOfBackups(): int |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->backupDestination->backups()->count(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function dateOfNewestBackup(): ?Carbon |
|
77
|
|
|
{ |
|
78
|
|
|
$newestBackup = $this->backupDestination->newestBackup(); |
|
79
|
|
|
|
|
80
|
|
|
if (is_null($newestBackup)) { |
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $newestBackup->date(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function newestBackupIsTooOld(): bool |
|
88
|
|
|
{ |
|
89
|
|
|
if (! count($this->backupDestination->backups())) { |
|
90
|
|
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$maximumDate = Carbon::now()->subDays($this->maximumAgeOfNewestBackupInDays); |
|
94
|
|
|
|
|
95
|
|
|
return ! $this->backupDestination->newestBackupIsOlderThan($maximumDate); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function usedStorage(): int |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->backupDestination->usedStorage(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function connectionError(): Exception |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->backupDestination->connectionError(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function isReachable(): bool |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->reachable; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function maximumAllowedUsageInBytes(): int |
|
114
|
|
|
{ |
|
115
|
|
|
return (int) ($this->maximumStorageUsageInMegabytes * 1024 * 1024); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function usesTooMuchStorage(): bool |
|
119
|
|
|
{ |
|
120
|
|
|
$maximumInBytes = $this->maximumAllowedUsageInBytes(); |
|
121
|
|
|
|
|
122
|
|
|
if ($maximumInBytes === 0) { |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $this->usedStorage() > $maximumInBytes; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function isHealthy(): bool |
|
130
|
|
|
{ |
|
131
|
|
|
if (! $this->isReachable()) { |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if ($this->usesTooMuchStorage()) { |
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if ($this->newestBackupIsTooOld()) { |
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ($this->failsInspections()) { |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function humanReadableAllowedStorage(): string |
|
151
|
|
|
{ |
|
152
|
|
|
$maximumInBytes = $this->maximumAllowedUsageInBytes(); |
|
153
|
|
|
|
|
154
|
|
|
if ($maximumInBytes === 0) { |
|
155
|
|
|
return 'unlimited'; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return Format::humanReadableSize($maximumInBytes); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function humanReadableUsedStorage(): string |
|
162
|
|
|
{ |
|
163
|
|
|
return Format::humanReadableSize($this->usedStorage()); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function backupDestination(): BackupDestination |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->backupDestination; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function getFailedInspection() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->failedInspection; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
protected function failsInspections() |
|
177
|
|
|
{ |
|
178
|
|
|
$this->runInspections(); |
|
179
|
|
|
|
|
180
|
|
|
return $this->getFailedInspection() !== null; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
protected function runInspections() |
|
184
|
|
|
{ |
|
185
|
|
|
$this->failedInspection = null; |
|
186
|
|
|
|
|
187
|
|
|
$currentInspection = null; |
|
188
|
|
|
|
|
189
|
|
|
try { |
|
190
|
|
|
collect($this->inspections)->each(function (HealthInspection $inspection) use (&$currentInspection) { |
|
191
|
|
|
$currentInspection = $inspection; |
|
192
|
|
|
$inspection->handle($this->backupDestination); |
|
193
|
|
|
}); |
|
194
|
|
|
} |
|
195
|
|
|
catch (\Exception $exception) { |
|
196
|
|
|
$this->failedInspection = new HealthInspectionFailure($currentInspection, $exception); |
|
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.