Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

use ILIAS\BackgroundTasks\Persistence;
use ILIAS\Cron\Schedule\CronJobScheduleType;

/**
* Garbage collection for BackgroundTasks
*/
class ilBackgroundTaskGarbageCollection extends ilCronJob
{
private ilLogger $logger;
private ilLanguage $lng;
private ilCronJobResult $result;
private Persistence $persistence;

public function __construct()
{
global $DIC;

$this->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;
}
}
6 changes: 6 additions & 0 deletions components/ILIAS/BackgroundTasks/lang/background_tasks.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""

Expand Down
6 changes: 6 additions & 0 deletions components/ILIAS/BackgroundTasks/lang/background_tasks_de.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 6 additions & 0 deletions components/ILIAS/BackgroundTasks/lang/background_tasks_en.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
<gsproviders>
<gsprovider class_name="ilBTGSProvider" purpose="meta_bar_static"/>
</gsproviders>

<crons>
<cron id="background_task_garbage_collection" class="ilBackgroundTaskGarbageCollection" />
</crons>
<logging />
</service>
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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()
);
}

/**
Expand Down
13 changes: 5 additions & 8 deletions components/ILIAS/BackgroundTasks/src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
*
*********************************************************************/

/**
* Created by PhpStorm.
* User: otruffer
* Date: 05.05.17
* Time: 10:14
*/
declare(strict_types=1);

namespace ILIAS\BackgroundTasks;

Expand All @@ -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
Expand Down Expand Up @@ -73,4 +68,6 @@ public function getBucketContainerId(Bucket $bucket): int;
* @return BucketMeta[]
*/
public function getBucketMetaOfUser(int $user_id): array;

public function garbageCollection(): void;
}
Loading