diff --git a/components/ILIAS/BackgroundTasks/classes/class.ilBackgroundTaskGarbageCollection.php b/components/ILIAS/BackgroundTasks/classes/class.ilBackgroundTaskGarbageCollection.php new file mode 100644 index 000000000000..a1d5e540c63f --- /dev/null +++ b/components/ILIAS/BackgroundTasks/classes/class.ilBackgroundTaskGarbageCollection.php @@ -0,0 +1,96 @@ +logger = $DIC->logger()->bgtk(); + $this->lng = $DIC->language(); + $this->lng->loadLanguageModule('background_tasks'); + $this->persistence = $DIC->backgroundTasks()->persistence(); + + $this->result = new ilCronJobResult(); + } + + public function getTitle(): string + { + return $this->lng->txt('background_task_garbage_collection'); + } + + public function getDescription(): string + { + return $this->lng->txt('background_task_garbage_collection_info'); + } + + public function getId(): string + { + return 'background_task_garbage_collection'; + } + + public function hasAutoActivation(): bool + { + return false; + } + + public function hasFlexibleSchedule(): bool + { + return true; + } + + public function getDefaultScheduleType(): CronJobScheduleType + { + return CronJobScheduleType::SCHEDULE_TYPE_IN_HOURS; + } + + public function getDefaultScheduleValue(): ?int + { + return 1; + } + + public function run(): ilCronJobResult + { + $this->logger->debug('Starting background task garbage collector..'); + + try { + $this->persistence->garbageCollection(); + } catch (Exception $e) { + $this->result->setStatus(ilCronJobResult::STATUS_CRASHED); + $this->result->setMessage(mb_substr("Exc.: {$e->getMessage()} / {$e->getTraceAsString()}", 0, 400)); + $this->logger->error("Background-Task Garbage Collection execution failed with message: {$e->getMessage()}"); + $this->logger->error($e->getTraceAsString()); + return $this->result; + } + + $this->result->setStatus(ilCronJobResult::STATUS_OK); + return $this->result; + } +} diff --git a/components/ILIAS/BackgroundTasks/lang/background_tasks.pot b/components/ILIAS/BackgroundTasks/lang/background_tasks.pot index 1c09a0b62371..196537180afc 100644 --- a/components/ILIAS/BackgroundTasks/lang/background_tasks.pot +++ b/components/ILIAS/BackgroundTasks/lang/background_tasks.pot @@ -8,6 +8,12 @@ msgstr "" msgid "abort" msgstr "" +msgid "background_task_garbage_collection" +msgstr "" + +msgid "background_task_garbage_collection_info" +msgstr "" + msgid "background_tasks" msgstr "" diff --git a/components/ILIAS/BackgroundTasks/lang/background_tasks_de.po b/components/ILIAS/BackgroundTasks/lang/background_tasks_de.po index e636a7eac34b..1335af1bf678 100644 --- a/components/ILIAS/BackgroundTasks/lang/background_tasks_de.po +++ b/components/ILIAS/BackgroundTasks/lang/background_tasks_de.po @@ -11,6 +11,12 @@ msgstr "" msgid "abort" msgstr "Abbrechen" +msgid "background_task_garbage_collection" +msgstr "Background Tasks GC" + +msgid "background_task_garbage_collection_info" +msgstr "Dieser Job bereinigt veraltete Background Tasks und zugehörige Datenbanktabellen." + msgid "background_tasks" msgstr "Background Tasks" diff --git a/components/ILIAS/BackgroundTasks/lang/background_tasks_en.po b/components/ILIAS/BackgroundTasks/lang/background_tasks_en.po index 48fea49f259d..d983c0db1a15 100644 --- a/components/ILIAS/BackgroundTasks/lang/background_tasks_en.po +++ b/components/ILIAS/BackgroundTasks/lang/background_tasks_en.po @@ -11,6 +11,12 @@ msgstr "" msgid "abort" msgstr "Abort" +msgid "background_task_garbage_collection" +msgstr "Background Tasks GC" + +msgid "background_task_garbage_collection_info" +msgstr "This job cleans up stale tasks and related database tables." + msgid "background_tasks" msgstr "Background Tasks" diff --git a/components/ILIAS/BackgroundTasks_/service.xml b/components/ILIAS/BackgroundTasks/service.xml old mode 100755 new mode 100644 similarity index 70% rename from components/ILIAS/BackgroundTasks_/service.xml rename to components/ILIAS/BackgroundTasks/service.xml index c6bb4634e427..7b1d010dddd7 --- a/components/ILIAS/BackgroundTasks_/service.xml +++ b/components/ILIAS/BackgroundTasks/service.xml @@ -7,4 +7,9 @@ + + + + + diff --git a/components/ILIAS/BackgroundTasks/src/Implementation/Persistence/BasicPersistence.php b/components/ILIAS/BackgroundTasks/src/Implementation/Persistence/BasicPersistence.php index 0bc809cb82b1..a8441b33dc37 100755 --- a/components/ILIAS/BackgroundTasks/src/Implementation/Persistence/BasicPersistence.php +++ b/components/ILIAS/BackgroundTasks/src/Implementation/Persistence/BasicPersistence.php @@ -55,7 +55,7 @@ public function __construct(protected \ilDBInterface $db) $this->taskHashToTaskContainerId = new \SplObjectStorage(); } - protected function gc(): void + public function garbageCollection(): void { $atom = $this->db->buildAtomQuery(); @@ -155,11 +155,8 @@ public function updateBucket(Bucket $bucket): void */ public function getBucketIdsOfUser(int $user_id, string $order_by = "id", string $order_direction = "ASC"): array { - // Garbage Collection - $random = new \Random\Randomizer(); - - if ($random->getInt(1, 100) === 1) { - $this->gc(); + if ((new \Random\Randomizer())->getInt(0, 100) === 1) { + $this->garbageCollection(); } return BucketContainer::where(['user_id' => $user_id]) @@ -192,9 +189,10 @@ public function getBucketMetaOfUser(int $user_id): array */ public function getBucketIdsByState(int $state): array { - $buckets = BucketContainer::where(['state' => $state])->get(); - - return array_map(fn(BucketContainer $bucket_container): int => $bucket_container->getId(), $buckets); + return array_map( + static fn(BucketContainer $bucket_container): int => $bucket_container->getId(), + BucketContainer::where(['state' => $state])->get() + ); } /** diff --git a/components/ILIAS/BackgroundTasks/src/Persistence.php b/components/ILIAS/BackgroundTasks/src/Persistence.php index 43095254e13c..9c0fb8256dec 100755 --- a/components/ILIAS/BackgroundTasks/src/Persistence.php +++ b/components/ILIAS/BackgroundTasks/src/Persistence.php @@ -16,12 +16,7 @@ * *********************************************************************/ -/** - * Created by PhpStorm. - * User: otruffer - * Date: 05.05.17 - * Time: 10:14 - */ +declare(strict_types=1); namespace ILIAS\BackgroundTasks; @@ -33,9 +28,9 @@ interface Persistence public function saveBucketAndItsTasks(Bucket $bucket): void; /** - * @return \int[] Returns an array of bucket ids for the given user Id. + * @return int[] Returns an array of bucket ids for the given user Id. */ - public function getBucketIdsOfUser(int $user_id, string $order_by = "id", string $order_direction = "ASC"): array; + public function getBucketIdsOfUser(int $user_id, string $order_by = 'id', string $order_direction = 'ASC'): array; /** * @return int[] Returns a list of bucket ids for the given Observer State @@ -73,4 +68,6 @@ public function getBucketContainerId(Bucket $bucket): int; * @return BucketMeta[] */ public function getBucketMetaOfUser(int $user_id): array; + + public function garbageCollection(): void; }