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 single automated course backup. 19 * 20 * @package core 21 * @copyright 2019 John Yao <johnyao@catalyst-au.net> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace core\task; 26 defined('MOODLE_INTERNAL') || die(); 27 28 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); 29 require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php'); 30 31 /** 32 * Adhoc task that performs single automated course backup. 33 * 34 * @package core 35 * @copyright 2019 John Yao <johnyao@catalyst-au.net> 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class course_backup_task extends \core\task\adhoc_task { 39 40 /** 41 * Run the adhoc task and preform the backup. 42 */ 43 public function execute() { 44 global $DB; 45 46 $lockfactory = \core\lock\lock_config::get_lock_factory('course_backup_adhoc'); 47 $courseid = $this->get_custom_data()->courseid; 48 49 try { 50 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); 51 } catch (\moodle_exception $e) { 52 mtrace('Invalid course id: ' . $courseid . ', task aborted.'); 53 return; 54 } 55 56 if (!$lock = $lockfactory->get_lock('course_backup_adhoc_task_' . $courseid, 10)) { 57 mtrace('Backup adhoc task for: ' . $course->fullname . 'is already running.'); 58 return; 59 } else { 60 mtrace('Processing automated backup for course: ' . $course->fullname); 61 } 62 63 try { 64 $backupcourse = $DB->get_record('backup_courses', array( 65 'courseid' => $courseid, 66 'laststatus' => \backup_cron_automated_helper::BACKUP_STATUS_QUEUED 67 ), '*', MUST_EXIST); 68 69 $adminid = $this->get_custom_data()->adminid; 70 $backupcourse->laststarttime = time(); 71 $backupcourse->laststatus = \backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED; 72 $DB->update_record('backup_courses', $backupcourse); 73 74 $backupcourse->laststatus = \backup_cron_automated_helper::launch_automated_backup($course, time(), $adminid); 75 if ($backupcourse->laststatus == \backup_cron_automated_helper::BACKUP_STATUS_ERROR || 76 $backupcourse->laststatus == \backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED) { 77 mtrace('Automated backup for course: ' . $course->fullname . ' failed.'); 78 // Reset unfinished to error. 79 $backupcourse->laststatus = \backup_cron_automated_helper::BACKUP_STATUS_ERROR; 80 } 81 82 // Remove excess backups. 83 $removedcount = \backup_cron_automated_helper::remove_excess_backups($course, time()); 84 $backupcourse->lastendtime = time(); 85 $backupcourse->nextstarttime = \backup_cron_automated_helper::calculate_next_automated_backup(null, time()); 86 $DB->update_record('backup_courses', $backupcourse); 87 } catch (\moodle_exception $e) { 88 mtrace('Automated backup for course: ' . $course->fullname . ' encounters an error.'); 89 mtrace('Exception: ' . $e->getMessage()); 90 mtrace('Debug: ' . $e->debuginfo); 91 } finally { 92 // Everything is finished release lock. 93 $lock->release(); 94 mtrace('Automated backup for course: ' . $course->fullname . ' completed.'); 95 } 96 } 97 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body