Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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          $rc->set_progress(new \core\progress\db_updater($restorerecord->id, 'backup_controllers', 'progress'));
  66  
  67          // Do some preflight checks on the restore.
  68          $status = $rc->get_status();
  69          $execution = $rc->get_execution();
  70  
  71          // Check that the restore is in the correct status and
  72          // that is set for asynchronous execution.
  73          if ($status == \backup::STATUS_AWAITING && $execution == \backup::EXECUTION_DELAYED) {
  74              // Execute the restore.
  75              $rc->execute_plan();
  76  
  77              // Send message to user if enabled.
  78              $messageenabled = (bool)get_config('backup', 'backup_async_message_users');
  79              if ($messageenabled && $rc->get_status() == \backup::STATUS_FINISHED_OK) {
  80                  $asynchelper = new async_helper('restore', $restoreid);
  81                  $asynchelper->send_message();
  82              }
  83  
  84          } else {
  85              // If status isn't 700, it means the process has failed.
  86              // Retrying isn't going to fix it, so marked operation as failed.
  87              $rc->set_status(\backup::STATUS_FINISHED_ERR);
  88              mtrace('Bad backup controller status, is: ' . $status . ' should be 700, marking job as failed.');
  89  
  90          }
  91  
  92          // Cleanup.
  93          $rc->destroy();
  94  
  95          $duration = time() - $started;
  96          mtrace('Restore completed in: ' . $duration . ' seconds');
  97      }
  98  }