Skip to content
Snippets Groups Projects
Commit f274d875 authored by Ted Bowman's avatar Ted Bowman
Browse files

Issue #3225753 by tedbow: Change UpdateReady form to use batch

parent 3bd9c292
No related branches found
No related tags found
No related merge requests found
...@@ -91,7 +91,60 @@ class BatchProcessor { ...@@ -91,7 +91,60 @@ class BatchProcessor {
} }
/** /**
* Finishes the batch job. * Calls the updater's commit() method.
*
* @param array $context
* The current context of the batch job.
*
* @see \Drupal\automatic_updates\Updater::commit()
*/
public static function commit(array &$context): void {
try {
static::getUpdater()->commit();
}
catch (\Throwable $e) {
static::handleException($e, $context);
}
}
/**
* Calls the updater's clean() method.
*
* @param array $context
* The current context of the batch job.
*
* @see \Drupal\automatic_updates\Updater::clean()
*/
public static function clean(array &$context): void {
try {
static::getUpdater()->clean();
}
catch (\Throwable $e) {
static::handleException($e, $context);
}
}
/**
* Finishes the stage batch job.
*
* @param bool $success
* Indicate that the batch API tasks were all completed successfully.
* @param array $results
* An array of all the results.
* @param array $operations
* A list of the operations that had not been completed by the batch API.
*/
public static function finishStage(bool $success, array $results, array $operations): ?RedirectResponse {
if ($success) {
return new RedirectResponse(Url::fromRoute('automatic_updates.confirmation_page', [],
['absolute' => TRUE])->toString());
}
static::handleBatchError($results);
return NULL;
}
/**
* Finishes the commit batch job.
* *
* @param bool $success * @param bool $success
* Indicate that the batch API tasks were all completed successfully. * Indicate that the batch API tasks were all completed successfully.
...@@ -100,10 +153,25 @@ class BatchProcessor { ...@@ -100,10 +153,25 @@ class BatchProcessor {
* @param array $operations * @param array $operations
* A list of the operations that had not been completed by the batch API. * A list of the operations that had not been completed by the batch API.
*/ */
public static function finish(bool $success, array $results, array $operations): ?RedirectResponse { public static function finishCommit(bool $success, array $results, array $operations): ?RedirectResponse {
if ($success) { if ($success) {
return new RedirectResponse(Url::fromRoute('automatic_updates.confirmation_page', [], ['absolute' => TRUE])->toString()); \Drupal::messenger()->addMessage('Update complete!');
// @todo redirect to update.php?
return new RedirectResponse(Url::fromRoute('automatic_updates.update_form', [],
['absolute' => TRUE])->toString());
} }
static::handleBatchError($results);
return NULL;
}
/**
* Handles a batch job that finished with errors.
*
* @param array $results
* The batch results.
*/
protected static function handleBatchError(array $results): void {
if (isset($results['errors'])) { if (isset($results['errors'])) {
foreach ($results['errors'] as $error) { foreach ($results['errors'] as $error) {
\Drupal::messenger()->addError($error); \Drupal::messenger()->addError($error);
...@@ -112,7 +180,6 @@ class BatchProcessor { ...@@ -112,7 +180,6 @@ class BatchProcessor {
else { else {
\Drupal::messenger()->addError("Update error"); \Drupal::messenger()->addError("Update error");
} }
return NULL;
} }
} }
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
namespace Drupal\automatic_updates\Form; namespace Drupal\automatic_updates\Form;
use Drupal\automatic_updates\AutomaticUpdatesEvents; use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\BatchProcessor;
use Drupal\automatic_updates\Updater; use Drupal\automatic_updates\Updater;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\State\StateInterface; use Drupal\Core\State\StateInterface;
...@@ -106,15 +108,15 @@ class UpdateReady extends UpdateFormBase { ...@@ -106,15 +108,15 @@ class UpdateReady extends UpdateFormBase {
$this->state->set('system.maintenance_mode', TRUE); $this->state->set('system.maintenance_mode', TRUE);
// @todo unset after updater. After db update? // @todo unset after updater. After db update?
} }
$batch = (new BatchBuilder())
// @todo Should these operations be done in batch. ->setTitle($this->t('Apply updates'))
$this->updater->commit(); ->setInitMessage($this->t('Preparing to apply updates'))
// Clean could be done in another page load or on cron to reduce page time. ->addOperation([BatchProcessor::class, 'commit'])
$this->updater->clean(); ->addOperation([BatchProcessor::class, 'clean'])
$this->messenger->addMessage("Update complete!"); ->setFinishCallback([BatchProcessor::class, 'finishCommit'])
->toArray();
// @todo redirect to update.php?
$form_state->setRedirect('automatic_updates.update_form'); batch_set($batch);
} }
} }
...@@ -240,7 +240,7 @@ class UpdaterForm extends UpdateFormBase { ...@@ -240,7 +240,7 @@ class UpdaterForm extends UpdateFormBase {
->addOperation([BatchProcessor::class, 'stageProjectVersions'], [ ->addOperation([BatchProcessor::class, 'stageProjectVersions'], [
$form_state->getValue('update_version'), $form_state->getValue('update_version'),
]) ])
->setFinishCallback([BatchProcessor::class, 'finish']) ->setFinishCallback([BatchProcessor::class, 'finishStage'])
->toArray(); ->toArray();
batch_set($batch); batch_set($batch);
......
...@@ -122,6 +122,7 @@ class AttendedCoreUpdateTest extends AttendedUpdateTestBase { ...@@ -122,6 +122,7 @@ class AttendedCoreUpdateTest extends AttendedUpdateTestBase {
$this->waitForBatchJob(); $this->waitForBatchJob();
$assert_session->pageTextContains('Ready to update'); $assert_session->pageTextContains('Ready to update');
$page->pressButton('Continue'); $page->pressButton('Continue');
$this->waitForBatchJob();
// @todo This message isn't showing up, for some reason. Figure out what the // @todo This message isn't showing up, for some reason. Figure out what the
// eff is going on. // eff is going on.
// $assert_session->pageTextContains('Update complete!'); // $assert_session->pageTextContains('Update complete!');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment