MDL-38190 (2) Backup/restore: Progress tracking for tasks and steps

This commit is contained in:
sam marshall 2013-08-05 18:27:29 +01:00
parent 16cd708835
commit 2531ae2922
3 changed files with 41 additions and 0 deletions

View File

@ -157,6 +157,11 @@ class backup_final_task extends backup_task {
$this->built = true;
}
public function get_weight() {
// The final task takes ages, so give it 20 times the weight of a normal task.
return 20;
}
// Protected API starts here
/**

View File

@ -158,10 +158,23 @@ abstract class base_plan implements checksumable, executable {
if (!$this->built) {
throw new base_plan_exception('base_plan_not_built');
}
// Calculate the total weight of all tasks and start progress tracking.
$progress = $this->get_progress();
$totalweight = 0;
foreach ($this->tasks as $task) {
$totalweight += $task->get_weight();
}
$progress->start_progress($this->get_name(), $totalweight);
// Build and execute all tasks.
foreach ($this->tasks as $task) {
$task->build();
$task->execute();
}
// Finish progress tracking.
$progress->end_progress();
}
/**

View File

@ -67,6 +67,17 @@ abstract class base_task implements checksumable, executable, loggable {
return $this->settings;
}
/**
* Returns the weight of this task, an approximation of the amount of time
* it will take. By default this value is 1. It can be increased for longer
* tasks.
*
* @return int Weight
*/
public function get_weight() {
return 1;
}
public function get_setting($name) {
// First look in task settings
$result = null;
@ -159,6 +170,13 @@ abstract class base_task implements checksumable, executable, loggable {
if ($this->executed) {
throw new base_task_exception('base_task_already_executed', $this->name);
}
// Starts progress based on the weight of this task and number of steps.
$progress = $this->get_progress();
$progress->start_progress($this->get_name(), count($this->steps), $this->get_weight());
$done = 0;
// Execute all steps.
foreach ($this->steps as $step) {
$result = $step->execute();
// If step returns array, it will be forwarded to plan
@ -166,11 +184,16 @@ abstract class base_task implements checksumable, executable, loggable {
if (is_array($result) and !empty($result)) {
$this->add_result($result);
}
$done++;
$progress->progress($done);
}
// Mark as executed if any step has been executed
if (!empty($this->steps)) {
$this->executed = true;
}
// Finish progress for this task.
$progress->end_progress();
}
/**