Merge branch 'MDL-73076-master' of https://github.com/lameze/moodle
[moodle.git] / lib / classes / task / asynchronous_backup_task.php
blob3df9f51a31e450a0a0e9c2f950a105dd4af53320
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.
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/>.
17 /**
18 * Adhoc task that performs asynchronous backups.
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
25 namespace core\task;
27 use async_helper;
29 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
32 require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
34 /**
35 * Adhoc task that performs asynchronous backups.
37 * @package core
38 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class asynchronous_backup_task extends adhoc_task {
43 /**
44 * Run the adhoc task and preform the backup.
46 public function execute() {
47 global $DB;
48 $started = time();
50 $backupid = $this->get_custom_data()->backupid;
51 $backuprecord = $DB->get_record('backup_controllers', array('backupid' => $backupid), 'id, controller', MUST_EXIST);
52 mtrace('Processing asynchronous backup for backup: ' . $backupid);
54 // Get the backup controller by backup id. If controller is invalid, this task can never complete.
55 if ($backuprecord->controller === '') {
56 mtrace('Bad backup controller status, invalid controller, ending backup execution.');
57 return;
59 $bc = \backup_controller::load_controller($backupid);
60 $bc->set_progress(new \core\progress\db_updater($backuprecord->id, 'backup_controllers', 'progress'));
62 // Do some preflight checks on the backup.
63 $status = $bc->get_status();
64 $execution = $bc->get_execution();
66 // Check that the backup is in the correct status and
67 // that is set for asynchronous execution.
68 if ($status == \backup::STATUS_AWAITING && $execution == \backup::EXECUTION_DELAYED) {
69 // Execute the backup.
70 $bc->execute_plan();
72 // Send message to user if enabled.
73 $messageenabled = (bool)get_config('backup', 'backup_async_message_users');
74 if ($messageenabled && $bc->get_status() == \backup::STATUS_FINISHED_OK) {
75 $asynchelper = new async_helper('backup', $backupid);
76 $asynchelper->send_message();
79 } else {
80 // If status isn't 700, it means the process has failed.
81 // Retrying isn't going to fix it, so marked operation as failed.
82 $bc->set_status(\backup::STATUS_FINISHED_ERR);
83 mtrace('Bad backup controller status, is: ' . $status . ' should be 700, marking job as failed.');
87 // Cleanup.
88 $bc->destroy();
90 $duration = time() - $started;
91 mtrace('Backup completed in: ' . $duration . ' seconds');