Skip to content
Permalink
Browse files

Merge branch 'master_MDL-28505_Asynchronous_backup_and_restore' of ht…

  • Loading branch information...
abgreeve committed Apr 9, 2019
2 parents 8f8b2ce + 2cd901a commit 29bc29ed4f34afeb68977accdd458fabc2101787
Showing with 2,458 additions and 114 deletions.
  1. +24 −0 admin/settings/courses.php
  2. +3 −0 admin/settings/subsystems.php
  3. +6 −0 backup/backup.class.php
  4. +128 −73 backup/backup.php
  5. +25 −9 backup/controller/backup_controller.class.php
  6. +22 −7 backup/controller/restore_controller.class.php
  7. +240 −0 backup/externallib.php
  8. +41 −7 backup/restore.php
  9. +10 −2 backup/restorefile.php
  10. +118 −0 backup/tests/async_backup_test.php
  11. +140 −0 backup/tests/async_restore_test.php
  12. +27 −0 backup/util/dbops/backup_controller_dbops.class.php
  13. +367 −0 backup/util/helper/async_helper.class.php
  14. +148 −0 backup/util/helper/tests/async_helper_test.php
  15. +1 −0 backup/util/includes/backup_includes.php
  16. +1 −0 backup/util/includes/restore_includes.php
  17. +1 −0 backup/util/ui/amd/build/async_backup.min.js
  18. +424 −0 backup/util/ui/amd/src/async_backup.js
  19. +104 −8 backup/util/ui/renderer.php
  20. +17 −6 course/delete.php
  21. +5 −0 course/view.php
  22. +42 −0 lang/en/backup.php
  23. +1 −0 lang/en/moodle.php
  24. +123 −0 lib/classes/progress/db_updater.php
  25. +90 −0 lib/classes/task/asynchronous_backup_task.php
  26. +89 −0 lib/classes/task/asynchronous_restore_task.php
  27. +2 −0 lib/db/install.xml
  28. +8 −0 lib/db/messages.php
  29. +27 −0 lib/db/services.php
  30. +18 −1 lib/db/upgrade.php
  31. +45 −0 lib/templates/async_backup_progress.mustache
  32. +52 −0 lib/templates/async_backup_progress_row.mustache
  33. +57 −0 lib/templates/async_backup_status.mustache
  34. +51 −0 lib/templates/async_restore_progress_row.mustache
  35. +1 −1 version.php
@@ -458,4 +458,28 @@
$ADMIN->add('backups', $temp);
// Create a page for asynchronous backup and restore configuration and defaults.
if (!empty($CFG->enableasyncbackup)) { // Only add settings if async mode is enable at site level.
$temp = new admin_settingpage('asyncgeneralsettings', new lang_string('asyncgeneralsettings', 'backup'));
$temp->add(new admin_setting_configcheckbox(
'backup/backup_async_message_users',
new lang_string('asyncemailenable', 'backup'),
new lang_string('asyncemailenabledetail', 'backup'), 0));
$temp->add(new admin_setting_configtext(
'backup/backup_async_message_subject',
new lang_string('asyncmessagesubject', 'backup'),
new lang_string('asyncmessagesubjectdetail', 'backup'),
new lang_string('asyncmessagesubjectdefault', 'backup')));
$temp->add(new admin_setting_confightmleditor(
'backup/backup_async_message',
new lang_string('asyncmessagebody', 'backup'),
new lang_string('asyncmessagebodydetail', 'backup'),
new lang_string('asyncmessagebodydefault', 'backup')));
$ADMIN->add('backups', $temp);
}
}
@@ -51,4 +51,7 @@
$optionalsubsystems->add(new admin_setting_configcheckbox('enablecoursepublishing',
new lang_string('enablecoursepublishing', 'hub'), new lang_string('enablecoursepublishing_help', 'hub'), 0));
$optionalsubsystems->add(new admin_setting_configcheckbox('enableasyncbackup', new lang_string('enableasyncbackup', 'backup'),
new lang_string('enableasyncbackup_help', 'backup'), 0, 1, 0));
}
@@ -74,6 +74,12 @@ abstract class backup implements checksumable {
const MODE_AUTOMATED = 50;
const MODE_CONVERTED = 60;
/**
* This mode is for asynchronous backups.
* These backups will run via adhoc scheduled tasks.
*/
const MODE_ASYNC = 70;
// Target (new/existing/current/adding/deleting)
const TARGET_CURRENT_DELETING = 0;
const TARGET_CURRENT_ADDING = 1;
@@ -30,16 +30,27 @@
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
// Backup of large courses requires extra memory. Use the amount configured
// in admin settings.
raise_memory_limit(MEMORY_EXTRA);
$courseid = required_param('id', PARAM_INT);
$sectionid = optional_param('section', null, PARAM_INT);
$cmid = optional_param('cm', null, PARAM_INT);
$cancel = optional_param('cancel', '', PARAM_ALPHA);
$previous = optional_param('previous', false, PARAM_BOOL);
/**
* Part of the forms in stages after initial, is POST never GET
*/
$backupid = optional_param('backup', false, PARAM_ALPHANUM);
// Determine if we are performing realtime for asynchronous backups.
$backupmode = backup::MODE_GENERAL;
if (async_helper::is_async_enabled()) {
$backupmode = backup::MODE_ASYNC;
}
$courseurl = new moodle_url('/course/view.php', array('id' => $courseid));
$url = new moodle_url('/backup/backup.php', array('id'=>$courseid));
if ($sectionid !== null) {
$url->param('section', $sectionid);
@@ -53,6 +64,8 @@
$id = $courseid;
$cm = null;
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
$coursecontext = context_course::instance($course->id);
$contextid = $coursecontext->id;
$type = backup::TYPE_1COURSE;
if (!is_null($sectionid)) {
$section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$sectionid), '*', MUST_EXIST);
@@ -68,11 +81,10 @@
switch ($type) {
case backup::TYPE_1COURSE :
require_capability('moodle/backup:backupcourse', context_course::instance($course->id));
require_capability('moodle/backup:backupcourse', $coursecontext);
$heading = get_string('backupcourse', 'backup', $course->shortname);
break;
case backup::TYPE_1SECTION :
$coursecontext = context_course::instance($course->id);
require_capability('moodle/backup:backupsection', $coursecontext);
if ((string)$section->name !== '') {
$sectionname = format_string($section->name, true, array('context' => $coursecontext));
@@ -84,102 +96,145 @@
}
break;
case backup::TYPE_1ACTIVITY :
require_capability('moodle/backup:backupactivity', context_module::instance($cm->id));
$activitycontext = context_module::instance($cm->id);
require_capability('moodle/backup:backupactivity', $activitycontext);
$contextid = $activitycontext->id;
$heading = get_string('backupactivity', 'backup', $cm->name);
break;
default :
print_error('unknownbackuptype');
}
// Backup of large courses requires extra memory. Use the amount configured
// in admin settings.
raise_memory_limit(MEMORY_EXTRA);
if (!($bc = backup_ui::load_controller($backupid))) {
$bc = new backup_controller($type, $id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id);
}
$backup = new backup_ui($bc);
$PAGE->set_title($heading);
$PAGE->set_heading($heading);
$renderer = $PAGE->get_renderer('core','backup');
if (empty($cancel)) {
// Do not print the header if user cancelled the process, as we are going to redirect the user.
echo $OUTPUT->header();
}
// Prepare a progress bar which can display optionally during long-running
// operations while setting up the UI.
$slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
// Only let user perform a backup if we aren't in async mode, or if we are
// and there are no pending backups for this item for this user.
if (!async_helper::is_async_pending($id, 'course', 'backup')) {
$previous = optional_param('previous', false, PARAM_BOOL);
if ($backup->get_stage() == backup_ui::STAGE_SCHEMA && !$previous) {
// After schema stage, we are probably going to get to the confirmation stage,
// The confirmation stage has 2 sets of progress, so this is needed to prevent
// it showing 2 progress bars.
$twobars = true;
$slowprogress->start_progress('', 2);
} else {
$twobars = false;
}
$backup->get_controller()->set_progress($slowprogress);
$backup->process();
// The mix of business logic and display elements below makes me sad.
// This needs to refactored into the renderer and seperated out.
if ($backup->enforce_changed_dependencies()) {
debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
}
if (!($bc = backup_ui::load_controller($backupid))) {
$bc = new backup_controller($type, $id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_YES, $backupmode, $USER->id);
}
$loghtml = '';
if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
// Display an extra backup step bar so that we can show the 'processing' step first.
echo html_writer::start_div('', array('id' => 'executionprogress'));
echo $renderer->progress_bar($backup->get_progress_bar());
$backup->get_controller()->set_progress(new \core\progress\display());
// Prepare logger and add to end of chain.
$logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
$backup->get_controller()->add_logger($logger);
// Carry out actual backup.
$backup->execute();
// Backup controller gets saved/loaded so the logger object changes and we
// have to retrieve it.
$logger = $backup->get_controller()->get_logger();
while (!is_a($logger, 'core_backup_html_logger')) {
$logger = $logger->get_next();
// Prepare a progress bar which can display optionally during long-running
// operations while setting up the UI.
$slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
$renderer = $PAGE->get_renderer('core', 'backup');
$backup = new backup_ui($bc);
if ($backup->get_stage() == backup_ui::STAGE_SCHEMA && !$previous) {
// After schema stage, we are probably going to get to the confirmation stage,
// The confirmation stage has 2 sets of progress, so this is needed to prevent
// it showing 2 progress bars.
$twobars = true;
$slowprogress->start_progress('', 2);
} else {
$twobars = false;
}
$backup->get_controller()->set_progress($slowprogress);
$backup->process();
// Get HTML from logger.
if ($CFG->debugdisplay) {
$loghtml = $logger->get_html();
if ($backup->enforce_changed_dependencies()) {
debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
}
// Hide the progress display and first backup step bar (the 'finished' step will show next).
echo html_writer::end_div();
echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
} else {
$backup->save_controller();
}
$loghtml = '';
if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
// Displaying UI can require progress reporting, so do it here before outputting
// the backup stage bar (as part of the existing progress bar, if required).
$ui = $backup->display($renderer);
if ($twobars) {
$slowprogress->end_progress();
}
if ($backupmode != backup::MODE_ASYNC) {
// Synchronous backup handling.
// Display an extra backup step bar so that we can show the 'processing' step first.
echo html_writer::start_div('', array('id' => 'executionprogress'));
echo $renderer->progress_bar($backup->get_progress_bar());
$backup->get_controller()->set_progress(new \core\progress\display());
// Prepare logger and add to end of chain.
$logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
$backup->get_controller()->add_logger($logger);
// Carry out actual backup.
$backup->execute();
// Backup controller gets saved/loaded so the logger object changes and we
// have to retrieve it.
$logger = $backup->get_controller()->get_logger();
while (!is_a($logger, 'core_backup_html_logger')) {
$logger = $logger->get_next();
}
// Get HTML from logger.
if ($CFG->debugdisplay) {
$loghtml = $logger->get_html();
}
// Hide the progress display and first backup step bar (the 'finished' step will show next).
echo html_writer::end_div();
echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
} else {
// Async backup handling.
$backup->get_controller()->finish_ui();
echo html_writer::start_div('', array('id' => 'executionprogress'));
echo $renderer->progress_bar($backup->get_progress_bar());
echo html_writer::end_div();
// Create adhoc task for backup.
$asynctask = new \core\task\asynchronous_backup_task();
$asynctask->set_blocking(false);
$asynctask->set_custom_data(array('backupid' => $backupid));
\core\task\manager::queue_adhoc_task($asynctask);
// Add ajax progress bar and initiate ajax via a template.
$restoreurl = new moodle_url('/backup/restorefile.php', array('contextid' => $contextid));
$progresssetup = array(
'backupid' => $backupid,
'contextid' => $contextid,
'courseurl' => $courseurl->out(),
'restoreurl' => $restoreurl->out(),
'headingident' => 'backup'
);
echo $renderer->render_from_template('core/async_backup_status', $progresssetup);
}
echo $renderer->progress_bar($backup->get_progress_bar());
} else {
$backup->save_controller();
}
if ($backup->get_stage() != backup_ui::STAGE_FINAL) {
// Displaying UI can require progress reporting, so do it here before outputting
// the backup stage bar (as part of the existing progress bar, if required).
$ui = $backup->display($renderer);
if ($twobars) {
$slowprogress->end_progress();
}
echo $renderer->progress_bar($backup->get_progress_bar());
echo $ui;
// Display log data if there was any.
if ($loghtml != '' && $backupmode != backup::MODE_ASYNC) {
echo $renderer->log_display($loghtml);
}
}
echo $ui;
$backup->destroy();
unset($backup);
$backup->destroy();
unset($backup);
// Display log data if there was any.
if ($loghtml != '') {
echo $renderer->log_display($loghtml);
} else { // User has a pending async operation.
echo $OUTPUT->notification(get_string('pendingasyncerror', 'backup'), 'error');
echo $OUTPUT->container(get_string('pendingasyncdetail', 'backup'));
echo $OUTPUT->continue_button($courseurl);
}
echo $OUTPUT->footer();

0 comments on commit 29bc29e

Please sign in to comment.
You can’t perform that action at this time.