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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
<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;
}
2 changes: 2 additions & 0 deletions lang/ilias_de.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,8 @@ awrn#:#user_awrn_default#:#Standard
awrn#:#user_awrn_hide#:#Nicht anzeigen
awrn#:#user_awrn_show#:#Anzeigen
background_tasks#:#abort#:#Abbrechen
background_tasks#:#background_task_garbage_collection#:#Background Tasks GC
background_tasks#:#background_task_garbage_collection_info#:#Dieser Job bereinigt veraltete Background Tasks und zugehörige Datenbanktabellen.
background_tasks#:#background_tasks#:#Background Tasks
background_tasks#:#background_tasks_running#:#Background Tasks
background_tasks#:#completed#:#Abgeschlossen
Expand Down
2 changes: 2 additions & 0 deletions lang/ilias_en.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,8 @@ awrn#:#user_awrn_default#:#Default
awrn#:#user_awrn_hide#:#Hide my status
awrn#:#user_awrn_show#:#Show my status
background_tasks#:#abort#:#Abort
background_tasks#:#background_task_garbage_collection#:#Background Tasks GC
background_tasks#:#background_task_garbage_collection_info#:#This job cleans up stale tasks and related database tables.
background_tasks#:#background_tasks#:#Background Tasks
background_tasks#:#background_tasks_running#:#Background Tasks
background_tasks#:#completed#:#Completed
Expand Down
Loading