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
1 merge request!10Issue #3225753: Change UpdateReady form to use batch
......@@ -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
* Indicate that the batch API tasks were all completed successfully.
......@@ -100,10 +153,25 @@ class BatchProcessor {
* @param array $operations
* 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) {
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'])) {
foreach ($results['errors'] as $error) {
\Drupal::messenger()->addError($error);
......@@ -112,7 +180,6 @@ class BatchProcessor {
else {
\Drupal::messenger()->addError("Update error");
}
return NULL;
}
}
......@@ -3,7 +3,9 @@
namespace Drupal\automatic_updates\Form;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\BatchProcessor;
use Drupal\automatic_updates\Updater;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\State\StateInterface;
......@@ -106,15 +108,15 @@ class UpdateReady extends UpdateFormBase {
$this->state->set('system.maintenance_mode', TRUE);
// @todo unset after updater. After db update?
}
// @todo Should these operations be done in batch.
$this->updater->commit();
// Clean could be done in another page load or on cron to reduce page time.
$this->updater->clean();
$this->messenger->addMessage("Update complete!");
// @todo redirect to update.php?
$form_state->setRedirect('automatic_updates.update_form');
$batch = (new BatchBuilder())
->setTitle($this->t('Apply updates'))
->setInitMessage($this->t('Preparing to apply updates'))
->addOperation([BatchProcessor::class, 'commit'])
->addOperation([BatchProcessor::class, 'clean'])
->setFinishCallback([BatchProcessor::class, 'finishCommit'])
->toArray();
batch_set($batch);
}
}
......@@ -240,7 +240,7 @@ class UpdaterForm extends UpdateFormBase {
->addOperation([BatchProcessor::class, 'stageProjectVersions'], [
$form_state->getValue('update_version'),
])
->setFinishCallback([BatchProcessor::class, 'finish'])
->setFinishCallback([BatchProcessor::class, 'finishStage'])
->toArray();
batch_set($batch);
......
......@@ -122,6 +122,7 @@ class AttendedCoreUpdateTest extends AttendedUpdateTestBase {
$this->waitForBatchJob();
$assert_session->pageTextContains('Ready to update');
$page->pressButton('Continue');
$this->waitForBatchJob();
// @todo This message isn't showing up, for some reason. Figure out what the
// eff is going on.
// $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