From 34de2a20765573799da20d4eb3bb1ae6fd984fd1 Mon Sep 17 00:00:00 2001 From: Matheus Zych Date: Fri, 20 Feb 2026 14:31:58 +0100 Subject: [PATCH 1/2] BackgroundTasks: Add cron Garbage Collection for background tasks See: https://mantis.ilias.de/view.php?id=45133 Stale background-task data was cleaned only sporadically via a random trigger inside `getBucketIdsOfUser`. `BasicPersistence` now exposes `garbageCollection()` without that path, and new `ilBackgroundTaskGC` runs it on a configurable cron schedule declared in `service.xml`. English and German strings document the job. Minor tidy-up in `getBucketIdsByState` and a trailing-space fix on one unrelated language line. (cherry picked from commit 8266da46bd57267ea057ce0cbd5231cf2cc55ee3) --- .../BackgroundTasks/lang/background_tasks.pot | 6 ++ .../lang/background_tasks_de.po | 6 ++ .../lang/background_tasks_en.po | 6 ++ .../Persistence/BasicPersistence.php | 16 +--- ...lass.ilBackgroundTaskGarbageCollection.php | 96 +++++++++++++++++++ components/ILIAS/BackgroundTasks_/service.xml | 5 + 6 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 components/ILIAS/BackgroundTasks_/classes/class.ilBackgroundTaskGarbageCollection.php 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/src/Implementation/Persistence/BasicPersistence.php b/components/ILIAS/BackgroundTasks/src/Implementation/Persistence/BasicPersistence.php index 0bc809cb82b1..c1d8556d3b8f 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,13 +155,6 @@ 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(); - } - return BucketContainer::where(['user_id' => $user_id]) ->orderBy($order_by, $order_direction) ->getArray(null, 'id'); @@ -192,9 +185,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_/classes/class.ilBackgroundTaskGarbageCollection.php b/components/ILIAS/BackgroundTasks_/classes/class.ilBackgroundTaskGarbageCollection.php new file mode 100644 index 000000000000..089709933d4a --- /dev/null +++ b/components/ILIAS/BackgroundTasks_/classes/class.ilBackgroundTaskGarbageCollection.php @@ -0,0 +1,96 @@ +logger = $DIC->logger()->cal(); + $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_/service.xml b/components/ILIAS/BackgroundTasks_/service.xml index c6bb4634e427..7b1d010dddd7 100755 --- a/components/ILIAS/BackgroundTasks_/service.xml +++ b/components/ILIAS/BackgroundTasks_/service.xml @@ -7,4 +7,9 @@ + + + + + From 7e443428a80af0c0519d44e6b661e3a24067ff52 Mon Sep 17 00:00:00 2001 From: Matheus Zych Date: Wed, 28 Oct 2026 09:10:15 +0100 Subject: [PATCH 2/2] BackgroundTasks: Move GC Cron to BackgroundTasks See: https://mantis.ilias.de/view.php?id=45133 The garbage collection cron and `service.xml` (id `bgtk`) were introduced under BackgroundTasks_. They belong in BackgroundTasks while BackgroundTasks_ is retired. This moves `ilBackgroundTaskGarbageCollection` and the service definition into BackgroundTasks, adds `garbageCollection()` to the `Persistence` interface, and logs via the `bgtk` component logger. (cherry picked from commit ca1490a4543e2355aeb5a06c6c20b03f468507bb) --- .../class.ilBackgroundTaskGarbageCollection.php | 2 +- .../service.xml | 0 .../Implementation/Persistence/BasicPersistence.php | 4 ++++ .../ILIAS/BackgroundTasks/src/Persistence.php | 13 +++++-------- 4 files changed, 10 insertions(+), 9 deletions(-) rename components/ILIAS/{BackgroundTasks_ => BackgroundTasks}/classes/class.ilBackgroundTaskGarbageCollection.php (98%) rename components/ILIAS/{BackgroundTasks_ => BackgroundTasks}/service.xml (100%) mode change 100755 => 100644 diff --git a/components/ILIAS/BackgroundTasks_/classes/class.ilBackgroundTaskGarbageCollection.php b/components/ILIAS/BackgroundTasks/classes/class.ilBackgroundTaskGarbageCollection.php similarity index 98% rename from components/ILIAS/BackgroundTasks_/classes/class.ilBackgroundTaskGarbageCollection.php rename to components/ILIAS/BackgroundTasks/classes/class.ilBackgroundTaskGarbageCollection.php index 089709933d4a..a1d5e540c63f 100644 --- a/components/ILIAS/BackgroundTasks_/classes/class.ilBackgroundTaskGarbageCollection.php +++ b/components/ILIAS/BackgroundTasks/classes/class.ilBackgroundTaskGarbageCollection.php @@ -33,7 +33,7 @@ public function __construct() { global $DIC; - $this->logger = $DIC->logger()->cal(); + $this->logger = $DIC->logger()->bgtk(); $this->lng = $DIC->language(); $this->lng->loadLanguageModule('background_tasks'); $this->persistence = $DIC->backgroundTasks()->persistence(); diff --git a/components/ILIAS/BackgroundTasks_/service.xml b/components/ILIAS/BackgroundTasks/service.xml old mode 100755 new mode 100644 similarity index 100% rename from components/ILIAS/BackgroundTasks_/service.xml rename to components/ILIAS/BackgroundTasks/service.xml diff --git a/components/ILIAS/BackgroundTasks/src/Implementation/Persistence/BasicPersistence.php b/components/ILIAS/BackgroundTasks/src/Implementation/Persistence/BasicPersistence.php index c1d8556d3b8f..a8441b33dc37 100755 --- a/components/ILIAS/BackgroundTasks/src/Implementation/Persistence/BasicPersistence.php +++ b/components/ILIAS/BackgroundTasks/src/Implementation/Persistence/BasicPersistence.php @@ -155,6 +155,10 @@ public function updateBucket(Bucket $bucket): void */ public function getBucketIdsOfUser(int $user_id, string $order_by = "id", string $order_direction = "ASC"): array { + if ((new \Random\Randomizer())->getInt(0, 100) === 1) { + $this->garbageCollection(); + } + return BucketContainer::where(['user_id' => $user_id]) ->orderBy($order_by, $order_direction) ->getArray(null, 'id'); 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; }