1 |
efrain |
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 |
/**
|
|
|
18 |
* Data cleanup. Supports cli and restore type.
|
|
|
19 |
* @package block_dedication
|
|
|
20 |
* @copyright 2022 University of Canterbury
|
|
|
21 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
namespace block_dedication\task;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Scheduled task to delete logs with origin cli and restore.
|
|
|
28 |
*/
|
|
|
29 |
class cleanup extends \core\task\scheduled_task {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Get a descriptive name for this task (shown to admins). *
|
|
|
33 |
* @return string
|
|
|
34 |
*/
|
|
|
35 |
public function get_name() {
|
|
|
36 |
return get_string('cleanuptask', 'block_dedication');
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Do the job.
|
|
|
41 |
* Throw exceptions on errors (the job will be retried).
|
|
|
42 |
*/
|
|
|
43 |
public function execute() {
|
|
|
44 |
global $DB;
|
|
|
45 |
$loglifetime = (int)get_config('block_dedication', 'allloglifetime');
|
|
|
46 |
|
|
|
47 |
if (empty($loglifetime) || $loglifetime < 0) {
|
|
|
48 |
return;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
$loglifetime = time() - ($loglifetime * 3600 * 24); // Value in days.
|
|
|
52 |
$lifetimep = array($loglifetime);
|
|
|
53 |
$start = time();
|
|
|
54 |
|
|
|
55 |
while ($min = $DB->get_field_select("block_dedication", "MIN(timestart)",
|
|
|
56 |
"timestart < ?", $lifetimep)) {
|
|
|
57 |
$params = array(min($min + 3600 * 24, $loglifetime));
|
|
|
58 |
// Delete cli and restore logs.
|
|
|
59 |
$DB->delete_records_select("block_dedication", "timestart < ? ", $params);
|
|
|
60 |
if (time() > $start + 1200) {
|
|
|
61 |
// Do not churn on log deletion for too long each run.
|
|
|
62 |
break;
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
mtrace(" Deleted old records from block_dedication table.");
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|