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 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
 
25
namespace core\task;
26
 
27
use async_helper;
28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
require_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
 */
40
class 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
        $restorerecord = $DB->get_record('backup_controllers', array('backupid' => $restoreid), 'id, controller', IGNORE_MISSING);
51
        // If the record doesn't exist, the backup controller failed to create. Unable to proceed.
52
        if (empty($restorerecord)) {
53
            mtrace('Unable to find restore controller, ending restore execution.');
54
            return;
55
        }
56
 
57
        mtrace('Processing asynchronous restore for id: ' . $restoreid);
58
 
59
        // Get the backup controller by backup id. If controller is invalid, this task can never complete.
60
        if ($restorerecord->controller === '') {
61
            mtrace('Bad restore controller status, invalid controller, ending restore execution.');
62
            return;
63
        }
64
        $rc = \restore_controller::load_controller($restoreid);
65
        try {
66
            $rc->set_progress(new \core\progress\db_updater($restorerecord->id, 'backup_controllers', 'progress'));
67
 
68
            // Do some preflight checks on the restore.
69
            $status = $rc->get_status();
70
            $execution = $rc->get_execution();
71
 
72
            // Check that the restore is in the correct status and
73
            // that is set for asynchronous execution.
74
            if ($status == \backup::STATUS_AWAITING && $execution == \backup::EXECUTION_DELAYED) {
75
                // Execute the restore.
76
                $rc->execute_plan();
77
 
78
                // Send message to user if enabled.
79
                $messageenabled = (bool) get_config('backup', 'backup_async_message_users');
80
                if ($messageenabled && $rc->get_status() == \backup::STATUS_FINISHED_OK) {
81
                    $asynchelper = new async_helper('restore', $restoreid);
82
                    $asynchelper->send_message();
83
                }
84
 
85
            } else {
86
                // If status isn't 700, it means the process has failed.
87
                // Retrying isn't going to fix it, so marked operation as failed.
88
                $rc->set_status(\backup::STATUS_FINISHED_ERR);
89
                mtrace('Bad backup controller status, is: ' . $status . ' should be 700, marking job as failed.');
90
 
91
            }
92
 
93
            $duration = time() - $started;
94
            mtrace('Restore completed in: ' . $duration . ' seconds');
95
        } catch (\Exception $e) {
96
            // If an exception is thrown, mark the restore as failed.
97
            $rc->set_status(\backup::STATUS_FINISHED_ERR);
98
            mtrace('Exception thrown during restore execution, marking job as failed.');
99
            mtrace($e->getMessage());
100
        } finally {
101
            // Cleanup.
102
            // Always destroy the controller.
103
            $rc->destroy();
104
        }
105
    }
106
 
107
    /**
108
     * Sets attemptsavailable to false.
109
     *
110
     * @return boolean
111
     */
112
    public function retry_until_success(): bool {
113
        return false;
114
    }
115
}