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