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

feat: cleanup task for directories #19

Open
wants to merge 25 commits into
base: 10.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
40 changes: 28 additions & 12 deletions bundles/CoreBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,36 @@ private function addMaintenanceNode(ArrayNodeDefinition $rootNode)
{
$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 @@ -88,9 +88,11 @@ public function loadInternal(array $config, ContainerBuilder $container)
// @deprecated since Pimcore 10.1, parameter will be removed in Pimcore 11
$container->setParameter('pimcore.response_exception_listener.render_error_document', $config['error_handling']['render_error_document']);

$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']);
$container->setParameter('pimcore.documents.web_to_print.default_controller_print_page', $config['documents']['web_to_print']['default_controller_print_page']);
$container->setParameter('pimcore.documents.web_to_print.default_controller_print_container', $config['documents']['web_to_print']['default_controller_print_container']);
Expand Down
8 changes: 7 additions & 1 deletion bundles/CoreBundle/Resources/config/maintenance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,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
65 changes: 65 additions & 0 deletions lib/Maintenance/Tasks/CleanupDirectoryTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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()
{
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()) && $file->getPath() !== $directory->getPath()) {
@rmdir($file->getPath());
}
}
}
}
4 changes: 1 addition & 3 deletions lib/Maintenance/Tasks/HousekeepingTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ class HousekeepingTask implements TaskInterface
protected $profilerTime;

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

Expand Down
Loading