ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilFileSystemCleanTempDirCron.php
Go to the documentation of this file.
1<?php
2
25
32{
34
36
37 protected ilLogger $logger;
38
42 public function __construct()
43 {
47 global $DIC;
48 if ($DIC->offsetExists('lng')) {
49 $this->language = $DIC['lng'];
50 }
51 if ($DIC->offsetExists('filesystem')) {
52 $this->filesystem = $DIC->filesystem()->temp();
53 }
54 if ($DIC->offsetExists('ilLoggerFactory')) {
55 $this->logger = $DIC->logger()->root();
56 }
57 }
58
59 private function initDependencies(): void
60 {
61 }
62
63 public function getId(): string
64 {
65 return "file_system_clean_temp_dir";
66 }
67
68 public function getTitle(): string
69 {
70 return $this->language->txt('file_system_clean_temp_dir_cron');
71 }
72
73 public function getDescription(): string
74 {
75 return $this->language->txt("file_system_clean_temp_dir_cron_info");
76 }
77
78 public function hasAutoActivation(): bool
79 {
80 return false;
81 }
82
83 public function hasFlexibleSchedule(): bool
84 {
85 return true;
86 }
87
89 {
90 return JobScheduleType::DAILY;
91 }
92
93 public function getDefaultScheduleValue(): ?int
94 {
95 return 1;
96 }
97
98 public function run(): JobResult
99 {
100 $this->initDependencies();
101 // only delete files and folders older than ten days to prevent issues with ongoing processes (e.g. zipping a folder)
102 $date = "until 10 day ago";
103
104 // files are deleted before folders to prevent issues that would arise when trying to delete a (no longer existing) file in a deleted folder.
105 $files = $this->filesystem->finder()->in([""]);
106 $files = $files->files();
107 $files = $files->date($date);
108 $files = $files->getIterator();
109 $files->rewind();
110 $deleted_files = [];
111 while ($files->valid()) {
112 try {
113 $file_match = $files->current();
114 $path = $file_match->getPath();
115 if ($file_match->isFile()) {
116 $this->filesystem->delete($path);
117 $deleted_files[] = $path;
118 }
119 $files->next();
120 } catch (Throwable $t) {
121 $this->logger->error(
122 "Cron Job \"Clean temp directory\" could not delete " . $path
123 . "due to the following exception: " . $t->getMessage()
124 );
125 $files->next();
126 }
127 }
128
129 // the folders are sorted based on their path length to ensure that nested folders are deleted first
130 // thereby preventing any issues due to deletion attempts on no longer existing folders.
131 $folders = $this->filesystem->finder()->in([""]);
132 $folders = $folders->directories();
133 $folders = $folders->date($date);
134 $folders = $folders->sort(fn(
135 Metadata $a,
137 ): int => strlen($a->getPath()) - strlen($b->getPath()));
138 $folders = $folders->reverseSorting();
139 $folders = $folders->getIterator();
140
141 $deleted_folders = [];
142
143 $folders->rewind();
144 while ($folders->valid()) {
145 try {
146 $folder_match = $folders->current();
147 $path = $folder_match->getPath();
148 if ($folder_match->isDir()) {
149 $this->filesystem->deleteDir($path);
150 $deleted_folders[] = $path;
151 }
152 $folders->next();
153 } catch (Throwable $t) {
154 $this->logger->error(
155 "Cron Job \"Clean temp directory\" could not delete " . $path
156 . "due to the following exception: " . $t->getMessage()
157 );
158 $folders->next();
159 }
160 }
161
162 $num_folders = count($deleted_folders);
163 $num_files = count($deleted_files);
164
165 $result = new JobResult();
166 $result->setMessage($num_folders . " folders and " . $num_files . " files have been deleted.");
167 $result->setStatus(JobResult::STATUS_OK);
168 return $result;
169 }
170}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
This class holds all default metadata send by the filesystem adapters.
Definition: Metadata.php:33
Class ilFileSystemCleanTempDirCron.
hasAutoActivation()
Is to be activated on "installation", does only work for ILIAS core cron jobs.
language handling
Component logger with individual log levels by component id.
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
$path
Definition: ltiservices.php:30
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $DIC
Definition: shib_login.php:26