Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Adhoc task that performs asynchronous backups.
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
 
25
namespace core\task;
26
 
27
use async_helper;
28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
32
require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
33
 
34
/**
35
 * Adhoc task that performs asynchronous backups.
36
 *
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
40
 */
41
class asynchronous_backup_task extends adhoc_task {
42
 
43
    /**
44
     * Run the adhoc task and preform the backup.
45
     */
46
    public function execute() {
47
        global $DB;
48
        $started = time();
49
 
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);
53
 
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;
58
        }
59
        $bc = \backup_controller::load_controller($backupid);
60
        $bc->set_progress(new \core\progress\db_updater($backuprecord->id, 'backup_controllers', 'progress'));
61
 
62
        // Do some preflight checks on the backup.
63
        $status = $bc->get_status();
64
        $execution = $bc->get_execution();
65
 
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();
71
 
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();
77
            }
78
 
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.');
84
 
85
        }
86
 
87
        // Cleanup.
88
        $bc->destroy();
89
 
90
        $duration = time() - $started;
91
        mtrace('Backup completed in: ' . $duration . ' seconds');
92
    }
93
}