/**
  * Runs a given task.
  *
  * @param TaskModel $task
  *
  * @return bool
  */
 public function runTask(TaskModel $task)
 {
     $error = null;
     try {
         $taskRecord = $this->_getTaskRecordById($task->id);
         $taskType = $task->getTaskType();
         if ($taskType) {
             // Figure out how many total steps there are.
             $task->totalSteps = $taskType->getTotalSteps();
             $task->status = TaskStatus::Running;
             Craft::Log('Starting task ' . $taskRecord->type . ' that has a total of ' . $task->totalSteps . ' steps.', LogLevel::Info, true);
             for ($step = 0; $step < $task->totalSteps; $step++) {
                 // Update the task
                 $task->currentStep = $step + 1;
                 $this->saveTask($task);
                 Craft::Log('Starting step ' . ($step + 1) . ' of ' . $task->totalSteps . ' total steps.', LogLevel::Info, true);
                 // Run it.
                 if (($result = $taskType->runStep($step)) !== true) {
                     // Did they give us an error to report?
                     if (is_string($result)) {
                         $error = $result;
                     } else {
                         $error = true;
                     }
                     break;
                 }
             }
         } else {
             $error = 'Could not find the task component type.';
         }
     } catch (\Exception $e) {
         $error = 'An exception was thrown: ' . $e->getMessage();
     }
     if ($task == $this->_nextPendingTask) {
         // Don't run this again
         $this->_nextPendingTask = null;
     }
     if ($error === null) {
         Craft::log('Finished task ' . $task->id . ' (' . $task->type . ').', LogLevel::Info, true);
         // We're done with this task, nuke it.
         $taskRecord->deleteNode();
         return true;
     } else {
         $this->fail($task, $error);
         return false;
     }
 }