1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Backup\Tasks\Cleanup; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\Backup\BackupDestination\BackupDestination; |
8
|
|
|
use Spatie\Backup\Events\CleanupHasFailed; |
9
|
|
|
use Spatie\Backup\Events\CleanupWasSuccessful; |
10
|
|
|
use Spatie\Backup\Helpers\Format; |
11
|
|
|
|
12
|
|
|
class CleanupJob |
13
|
|
|
{ |
14
|
|
|
/** @var \Illuminate\Support\Collection */ |
15
|
|
|
protected $backupDestinations; |
16
|
|
|
|
17
|
|
|
/** @var \Spatie\Backup\Tasks\Cleanup\CleanupStrategy */ |
18
|
|
|
protected $strategy; |
19
|
|
|
|
20
|
|
|
/** @var bool */ |
21
|
|
|
protected $sendNotifications = true; |
22
|
|
|
|
23
|
|
|
public function __construct(Collection $backupDestinations, CleanupStrategy $strategy, bool $disableNotifications = false) |
24
|
|
|
{ |
25
|
|
|
$this->backupDestinations = $backupDestinations; |
26
|
|
|
|
27
|
|
|
$this->strategy = $strategy; |
28
|
|
|
|
29
|
|
|
$this->sendNotifications = ! $disableNotifications; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function run() |
33
|
|
|
{ |
34
|
|
|
$this->backupDestinations->each(function (BackupDestination $backupDestination) { |
35
|
|
|
try { |
36
|
|
|
if (! $backupDestination->isReachable()) { |
37
|
|
|
throw new Exception("Could not connect to disk {$backupDestination->diskName()} because: {$backupDestination->connectionError()}"); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
consoleOutput()->info("Cleaning backups of {$backupDestination->backupName()} on disk {$backupDestination->diskName()}..."); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$this->strategy->deleteOldBackups($backupDestination->backups()); |
43
|
|
|
$this->sendNotification(new CleanupWasSuccessful($backupDestination)); |
44
|
|
|
|
45
|
|
|
$usedStorage = Format::humanReadableSize($backupDestination->fresh()->usedStorage()); |
46
|
|
|
consoleOutput()->info("Used storage after cleanup: {$usedStorage}."); |
|
|
|
|
47
|
|
|
} catch (Exception $exception) { |
48
|
|
|
consoleOutput()->error("Cleanup failed because: {$exception->getMessage()}."); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$this->sendNotification(new CleanupHasFailed($exception)); |
51
|
|
|
|
52
|
|
|
throw $exception; |
53
|
|
|
} |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function sendNotification($notification) |
58
|
|
|
{ |
59
|
|
|
if ($this->sendNotifications) { |
60
|
|
|
event($notification); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: