-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDatabaseStorageCommandController.php
More file actions
157 lines (134 loc) · 6.91 KB
/
DatabaseStorageCommandController.php
File metadata and controls
157 lines (134 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace Wegmeister\DatabaseStorage\Command;
use DateInterval;
use DateTime;
use Wegmeister\DatabaseStorage\Service\DatabaseStorageService;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
#[Flow\Scope("singleton")]
class DatabaseStorageCommandController extends CommandController
{
#[Flow\Inject]
protected DatabaseStorageService $databaseStorageService;
#[Flow\InjectConfiguration(path: "cleanup", package: "Wegmeister.DatabaseStorage")]
protected ?array $storageCleanupConfiguration;
/**
* Deletes entries of configured storages older than configured date interval
*/
public function cleanUpConfiguredStoragesCommand(): void
{
$this->outputFormatted('<b>Cleanup of configured storages</b>');
$this->outputLine('');
if (empty($this->storageCleanupConfiguration)) {
$this->outputFormatted('No cleanup configuration found.', [], 4);
$this->outputFormatted('Please configure the cleanup for Wegmeister.DatabaseStorage in Settings.yaml.', [], 4);
return;
}
$storageIdentifiers = array_keys($this->storageCleanupConfiguration);
$results = $this->getCleanupResultsForStorageIdentifier($storageIdentifiers);
$this->output->outputTable($results, ['storageIdentifier', 'messages'], 'Cleanup results');
}
/**
* Deletes entries of all storages older than given date interval.
* You can also skip the configured storages.
*/
public function cleanupAllStoragesCommand(string $dateInterval, bool $removeFiles = false, bool $includeConfiguredStorages = false): void
{
$this->outputFormatted('<b>Cleanup of all storages</b>');
$this->outputLine('');
// Check if the date interval is not empty and output an error message
if (empty($dateInterval)) {
$this->outputFormatted('Please provide a date interval.', [], 4);
$this->outputFormatted('Example: P1M (1 month), P1Y (1 year), P1D (1 day), P1W (1 week), P1Y1M1D (1 year, 1 month, 1 day)', [], 4);
return;
}
// Check if the date interval is valid
try {
$dateIntervalFromString = new DateInterval($dateInterval);
} catch (\Exception $exception) {
$this->outputFormatted(
'Invalid date interval value, please check the format (https://www.php.net/manual/de/class.dateinterval.php).'
);
return;
}
$daysToKeepData = $this->getDaysToKeepFromConfiguredInterval($dateIntervalFromString);
// Get the list af all storage identifiers and filter the optional excluded storages
$skippedStorages = [];
if ($includeConfiguredStorages === false) {
$skippedStorages = array_keys($this->storageCleanupConfiguration);
}
$storageIdentifiers = $this->databaseStorageService->getListOfStorageIdentifiers($skippedStorages);
$results = $this->getCleanupResultsForStorageIdentifier($storageIdentifiers, $dateIntervalFromString, $daysToKeepData, $removeFiles);
$this->output->outputTable($results, ['storageIdentifier', 'messages'], 'Cleanup results');
}
/**
* Get the cleanup results for the given storage identifiers.
* If no date interval is provided, the cleanup configuration will be used.
*/
protected function getCleanupResultsForStorageIdentifier(array $storageIdentifiers, ?DateInterval $dateInterval = null, int $daysToKeepData = -1, $removeFiles = false): array
{
$results = [];
foreach ($storageIdentifiers as $storageIdentifier) {
$results[$storageIdentifier] = ['storageIdentifier' => $storageIdentifier, 'messages' => ''];
$newDateInterval = $dateInterval;
// use the storage cleanup configuration if no date interval is provided
if ($newDateInterval === null) {
// Check if the date interval is valid
try {
$newDateInterval = $this->getDateIntervalFromConfiguration($storageIdentifier);
} catch (\Exception $exception) {
$results[$storageIdentifier]['messages'] .= $exception->getMessage() . PHP_EOL;
continue;
}
// Check if we have entries for the storage identifier
$daysToKeepData = $this->getDaysToKeepFromConfiguredInterval($newDateInterval);
// Use removeFiles from configuration if not provided
$removeFiles = filter_var(
$this->storageCleanupConfiguration[$storageIdentifier]['removeFiles'] ?? false,
FILTER_VALIDATE_BOOLEAN
);
}
$amountOfEntries = $this->databaseStorageService->getAmountOfEntriesByStorageIdentifier($storageIdentifier);
if ($amountOfEntries === 0) {
$results[$storageIdentifier]['messages'] .= sprintf('No entries found in storage "%s".', $storageIdentifier) . PHP_EOL;
continue;
}
// Cleanup the storage
$results[$storageIdentifier]['messages'] .= vsprintf('Removing entries from storage "%s" older than %s days.', [$storageIdentifier, $daysToKeepData]) . PHP_EOL;
$amountOfOutdatedEntries = $this->databaseStorageService->cleanupByStorageIdentifierAndDateInterval($storageIdentifier, $newDateInterval, $removeFiles);
$results[$storageIdentifier]['messages'] .= vsprintf('Removed %s entries from storage "%s" (%s entries in total).', [$amountOfOutdatedEntries, $storageIdentifier, $amountOfEntries]);
}
return $results;
}
/**
* Get the date interval from the configuration and ensure it is valid.
*/
protected function getDateIntervalFromConfiguration(string $storageIdentifier): DateInterval
{
$storageCleanupConfiguration = $this->storageCleanupConfiguration[$storageIdentifier] ?? null;
if (!isset($storageCleanupConfiguration['dateInterval'])) {
$errorMessage = vsprintf(
'No date interval configuration for storage "%s" has been found.',
[$storageIdentifier]
);
throw new \InvalidArgumentException($errorMessage, 1732462801);
}
try {
return new DateInterval($storageCleanupConfiguration['dateInterval']);
} catch (\Exception $exception) {
$errorMessage = vsprintf(
'Invalid date interval configuration for storage "%s".',
[$storageIdentifier]
);
throw new \InvalidArgumentException($errorMessage, 1732462753);
}
}
/**
* Get the amount of days to keep from the configured date interval
*/
protected function getDaysToKeepFromConfiguredInterval(DateInterval $dateInterval): int
{
$intervalDateTime = (new DateTime())->add($dateInterval);
return date_diff($intervalDateTime, new DateTime('now'))->days;
}
}