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\Strategies\CleanupStrategy */ |
18
|
|
|
protected $strategy; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param \Illuminate\Support\Collection $backupDestinations |
22
|
|
|
* @param \Spatie\Backup\Tasks\Cleanup\CleanupStrategy $strategy |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Collection $backupDestinations, CleanupStrategy $strategy) |
25
|
|
|
{ |
26
|
|
|
$this->backupDestinations = $backupDestinations; |
27
|
|
|
$this->strategy = $strategy; |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function run() |
31
|
|
|
{ |
32
|
|
|
$this->backupDestinations->each(function (BackupDestination $backupDestination) { |
33
|
|
|
|
34
|
|
|
try { |
35
|
|
|
if (!$backupDestination->isReachable()) { |
36
|
|
|
throw new Exception("Could not connect to {$backupDestination->getFilesystemType()} because: {$backupDestination->getConnectionError()}"); |
37
|
|
|
}; |
38
|
|
|
|
39
|
|
|
consoleOutput()->info("Cleaning backups of {$backupDestination->getBackupName()} on {$backupDestination->getFilesystemType()}-filesystem"); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$this->strategy->deleteOldBackups($backupDestination->getBackups()); |
42
|
|
|
event(new CleanupWasSuccessFul($backupDestination)); |
43
|
|
|
|
44
|
|
|
$usedStorage = Format::getHumanReadableSize($backupDestination->getUsedStorage()); |
45
|
|
|
consoleOutput()->info("Used storage after cleanup: {$usedStorage}"); |
|
|
|
|
46
|
|
|
} catch (Exception $exception) { |
47
|
|
|
consoleOutput()->error("Cleanup failed because: {$exception->getMessage()}"); |
|
|
|
|
48
|
|
|
|
49
|
|
|
event(new CleanupHasFailed($exception)); |
50
|
|
|
} |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..