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 * Adhoc task that performs asynchronous restores. 19 * 20 * @package core 21 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25namespace core\task; 26 27use async_helper; 28 29defined('MOODLE_INTERNAL') || die(); 30 31require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); 32 33/** 34 * Adhoc task that performs asynchronous restores. 35 * 36 * @package core 37 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40class asynchronous_restore_task extends adhoc_task { 41 42 /** 43 * Run the adhoc task and preform the restore. 44 */ 45 public function execute() { 46 global $DB; 47 $started = time(); 48 49 $restoreid = $this->get_custom_data()->backupid; 50 $restorerecordid = $DB->get_field('backup_controllers', 'id', array('backupid' => $restoreid), MUST_EXIST); 51 mtrace('Processing asynchronous restore for id: ' . $restoreid); 52 53 // Get the restore controller by backup id. 54 $rc = \restore_controller::load_controller($restoreid); 55 $rc->set_progress(new \core\progress\db_updater($restorerecordid, 'backup_controllers', 'progress')); 56 57 // Do some preflight checks on the restore. 58 $status = $rc->get_status(); 59 $execution = $rc->get_execution(); 60 61 // Check that the restore is in the correct status and 62 // that is set for asynchronous execution. 63 if ($status == \backup::STATUS_AWAITING && $execution == \backup::EXECUTION_DELAYED) { 64 // Execute the restore. 65 $rc->execute_plan(); 66 67 // Send message to user if enabled. 68 $messageenabled = (bool)get_config('backup', 'backup_async_message_users'); 69 if ($messageenabled && $rc->get_status() == \backup::STATUS_FINISHED_OK) { 70 $asynchelper = new async_helper('restore', $restoreid); 71 $asynchelper->send_message(); 72 } 73 74 } else { 75 // If status isn't 700, it means the process has failed. 76 // Retrying isn't going to fix it, so marked operation as failed. 77 $rc->set_status(\backup::STATUS_FINISHED_ERR); 78 mtrace('Bad backup controller status, is: ' . $status . ' should be 700, marking job as failed.'); 79 80 } 81 82 // Cleanup. 83 $rc->destroy(); 84 85 $duration = time() - $started; 86 mtrace('Restore completed in: ' . $duration . ' seconds'); 87 } 88} 89 90