Skip to content

Commit

Permalink
MDL-69358 backup: Do not clean up records for incomplete adhoc tasks.
Browse files Browse the repository at this point in the history
Co-authored-by: Heena Agheda <heenaagheda@catalyst-au.net>
  • Loading branch information
golenkovm and hdagheda committed Aug 12, 2020
1 parent 24c6876 commit 7132e1a
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions lib/classes/task/backup_cleanup_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,27 @@ public function get_name() {
public function execute() {
global $DB;

$timenow = time();

// Delete old backup_controllers and logs.
$loglifetime = get_config('backup', 'loglifetime');
if (!empty($loglifetime)) { // Value in days.
$loglifetime = $timenow - ($loglifetime * 3600 * 24);
// Delete child records from backup_logs.
$DB->execute("DELETE FROM {backup_logs}
WHERE EXISTS (
SELECT 'x'
FROM {backup_controllers} bc
WHERE bc.backupid = {backup_logs}.backupid
AND bc.timecreated < ?)", array($loglifetime));
// Delete records from backup_controllers.
$DB->execute("DELETE FROM {backup_controllers}
WHERE timecreated < ?", array($loglifetime));

if (empty($loglifetime)) {
throw new coding_exception('The \'loglifetime\' config is not set. Can\'t proceed and delete old backup records.');
}

// First, get the list of all backupids older than loglifetime.
$timecreated = time() - ($loglifetime * DAYSECS);
$records = $DB->get_records_select('backup_controllers', 'timecreated < ?', array($timecreated), 'id', 'id, backupid');

foreach ($records as $record) {
// Check if there is no incomplete adhoc task relying on the given backupid.
$params = array('%' . $record->backupid . '%');
$select = $DB->sql_like('customdata', '?', false);
$count = $DB->count_records_select('task_adhoc', $select, $params);
if ($count === 0) {
// Looks like there is no adhoc task, so we can delete logs and controllers for this backupid.
$DB->delete_records('backup_logs', array('backupid' => $record->backupid));
$DB->delete_records('backup_controllers', array('backupid' => $record->backupid));
}
}
}

}

0 comments on commit 7132e1a

Please sign in to comment.