Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup task for directories #18

Open
wants to merge 2 commits into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bundles/CoreBundle/config/maintenance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,17 @@ services:

Pimcore\Maintenance\Tasks\HousekeepingTask:
arguments:
- '%pimcore.maintenance.housekeeping.cleanup_tmp_files_atime_older_than%'
- '%pimcore.maintenance.housekeeping.cleanup_profiler_files_atime_older_than%'
tags:
- { name: pimcore.maintenance.task, type: housekeeping }

Pimcore\Maintenance\Tasks\CleanupDirectoryTask:
arguments:
- '%pimcore.maintenance.cleanup_directory.cleanup_files_atime_older_than%'
- '%pimcore.maintenance.cleanup_directory.directories%'
tags:
- { name: pimcore.maintenance.task, type: cleanupDirectory }

Pimcore\Maintenance\Tasks\LowQualityImagePreviewTask:
arguments:
- '@logger'
Expand Down
40 changes: 28 additions & 12 deletions bundles/CoreBundle/src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,36 @@ private function addMaintenanceNode(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
->arrayNode('maintenance')
->addDefaultsIfNotSet()
->children()
->arrayNode('housekeeping')
->arrayNode('maintenance')
->addDefaultsIfNotSet()
->children()
->integerNode('cleanup_tmp_files_atime_older_than')
->info('Integer value in seconds.')
->defaultValue(7_776_000) // 90 days
->end()
->integerNode('cleanup_profiler_files_atime_older_than')
->info('Integer value in seconds.')
->defaultValue(1800)
->children()
->arrayNode('housekeeping')
->addDefaultsIfNotSet()
->children()
->integerNode('cleanup_profiler_files_atime_older_than')
->info('Integer value in seconds.')
->defaultValue(1800)
->end()
->end()
->end()
->arrayNode('cleanup_directory')
->addDefaultsIfNotSet()
->children()
->integerNode('cleanup_files_atime_older_than')
->info('Integer value in seconds.')
->defaultValue(7_776_000) // 90 days
->end()
->arrayNode('directories')
->info('Directories to cleanup.')
->scalarPrototype()->end()
->defaultValue([PIMCORE_PRIVATE_VAR. '/tmp', PIMCORE_WEB_ROOT . '/var/tmp'])
->end()
->end()
->end()
->end()
->end()
->end()
->end()
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public function loadInternal(array $config, ContainerBuilder $container): void

$container->setParameter('pimcore.web_profiler.toolbar.excluded_routes', $config['web_profiler']['toolbar']['excluded_routes']);

$container->setParameter('pimcore.maintenance.housekeeping.cleanup_tmp_files_atime_older_than', $config['maintenance']['housekeeping']['cleanup_tmp_files_atime_older_than']);
$container->setParameter('pimcore.maintenance.housekeeping.cleanup_profiler_files_atime_older_than', $config['maintenance']['housekeeping']['cleanup_profiler_files_atime_older_than']);

$container->setParameter('pimcore.maintenance.cleanup_directory.cleanup_files_atime_older_than', $config['maintenance']['cleanup_directory']['cleanup_files_atime_older_than']);
$container->setParameter('pimcore.maintenance.cleanup_directory.directories', $config['maintenance']['cleanup_directory']['directories']);

$container->setParameter('pimcore.documents.default_controller', $config['documents']['default_controller']);

//twig security policy allowlist config
Expand Down
66 changes: 66 additions & 0 deletions lib/Maintenance/Tasks/CleanupDirectoryTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Pimcore\Maintenance\Tasks;

use Pimcore\Maintenance\TaskInterface;

class CleanupDirectoryTask implements TaskInterface
{
public function __construct(
protected int $tmpFileTime,
protected array $cleanupDirectories
) {
}


public function execute(): void
{
foreach ($this->cleanupDirectories as $directory) {
$this->deleteFilesInFolderOlderThanSeconds($directory, $this->tmpFileTime);
}
}

/**
* @param string $folder
* @param int $seconds
*/
private function deleteFilesInFolderOlderThanSeconds(string $folder, int $seconds): void
{
if (!is_dir($folder)) {
return;
}

$directory = new \RecursiveDirectoryIterator($folder);
$filter = new \RecursiveCallbackFilterIterator($directory, function (\SplFileInfo $current, $key, $iterator) use ($seconds) {
if (strpos($current->getFilename(), '-low-quality-preview.svg')) {
// do not delete low quality image previews
return false;
}

if ($current->isFile()) {
if ($current->getATime() && $current->getATime() < (time() - $seconds)) {
return true;
}
} else {
return true;
}

return false;
});

$iterator = new \RecursiveIteratorIterator($filter);

foreach ($iterator as $file) {
/**
* @var \SplFileInfo $file
*/
if ($file->isFile()) {
@unlink($file->getPathname());
}

if (is_dir_empty($file->getPath())) {
@rmdir($file->getPath());
}
}
}
}
5 changes: 1 addition & 4 deletions lib/Maintenance/Tasks/HousekeepingTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
*/
class HousekeepingTask implements TaskInterface
{
protected int $tmpFileTime;

protected int $profilerTime;

public function __construct(int $tmpFileTime, int $profilerTime)
public function __construct(int $profilerTime)
{
$this->tmpFileTime = $tmpFileTime;
$this->profilerTime = $profilerTime;
}

Expand Down