Skip to content

Commit 0826069

Browse files
committed
MBS-10083: Fix cleanup task
1 parent c13b12d commit 0826069

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

classes/task/cleanup_task.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public function get_name(): string {
4646
#[\Override]
4747
public function execute(): void {
4848
global $DB;
49-
$cleanupduration = get_config('qbank_questiongen', 'cleanupduration');
49+
$cleanupdelay = get_config('qbank_questiongen', 'cleanupdelay');
5050
$idstocleanup = $DB->get_fieldset_select('qbank_questiongen', 'id', 'timemodified < ?',
51-
[$this->clock->time() - $cleanupduration]);
51+
[$this->clock->time() - $cleanupdelay]);
5252

5353
$chunks = array_chunk($idstocleanup, 100);
5454
$deleted = 0;
@@ -63,7 +63,7 @@ public function execute(): void {
6363

6464
/** @var adhoc_task $task */
6565
foreach ($tasks as $task) {
66-
if ($task->get_attempts_available() === 0 && $task->get_timestarted() < $this->clock->time() - $cleanupduration) {
66+
if ($task->get_attempts_available() === 0 && $task->get_timestarted() < $this->clock->time() - $cleanupdelay) {
6767
mtrace('Deleting old task ' . $task->get_id() . ' from task_adhoc table.');
6868
mtrace('Customdata: ' . json_encode($task->get_custom_data()));
6969
$DB->delete_records('task_adhoc', ['id' => $task->get_id()]);

tests/task/cleanup_task_test.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace qbank_questiongen\task;
18+
19+
use qbank_questiongen\form\story_form;
20+
use stdClass;
21+
22+
/**
23+
* Unit tests for the cleanup task.
24+
*
25+
* @package qbank_questiongen
26+
* @copyright 2025 ISB Bayern
27+
* @author Philipp Memmel
28+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29+
* @covers \local_ai_manager\local\observers
30+
*/
31+
final class cleanup_task_test extends \advanced_testcase {
32+
33+
/**
34+
* Tests the functionality of the cleanup task.
35+
*
36+
* @covers \qbank_questiongen\task\cleanup_task::execute
37+
*/
38+
public function test_execute(): void {
39+
global $DB;
40+
$this->resetAfterTest();
41+
42+
$user = $this->getDataGenerator()->create_user();
43+
44+
set_config('cleanupdelay', DAYSECS * 11, 'qbank_questiongen');
45+
// Set the clock to the past to simulate that a generate question task was created 10 days ago.
46+
$presenttime = time();
47+
$clock = $this->mock_clock_with_frozen($presenttime - DAYSECS * 10);
48+
49+
$record = new stdClass();
50+
$record->category = 15;
51+
$record->userid = $user->id;
52+
$record->mode = story_form::QUESTIONGEN_MODE_TOPIC;
53+
$record->story = 'French revolution';
54+
$record->numoftries = 3;
55+
$record->llmresponse = '';
56+
$record->success = 0;
57+
$record->timecreated = $clock->time();
58+
$record->timemodified = $clock->time();
59+
$DB->insert_record('qbank_questiongen', $record);
60+
61+
// Return to the present.
62+
$this->mock_clock_with_frozen($presenttime);
63+
64+
// Let's run the task. Because the task is supposed to only delete entries with timemodified older than 11 days
65+
// and our entry is only 10 days old, it should not be deleted.
66+
ob_start();
67+
$task = new cleanup_task();
68+
$task->execute();
69+
ob_end_clean();
70+
71+
$this->assertCount(1, $DB->get_records('qbank_questiongen'));
72+
73+
// Two more days have passed, so now the DB entry should be 12 days old and should be deleted.
74+
$this->mock_clock_with_frozen($presenttime + DAYSECS * 2);
75+
ob_start();
76+
$task = new cleanup_task();
77+
$task->execute();
78+
ob_end_clean();
79+
$this->assertCount(0, $DB->get_records('qbank_questiongen'));
80+
}
81+
}

0 commit comments

Comments
 (0)