Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove DrushBatchContext magic wrapper for batch . #5353

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 12 additions & 36 deletions includes/batch.inc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @file
* Drush batch API.
Expand Down Expand Up @@ -27,28 +28,6 @@
use Drupal\Core\Database\Database;
use Drush\Drush;

/**
* Class extending ArrayObject to allow the batch API to perform logging when
* some keys of the array change.
*
* It is used to wrap batch's $context array and set log messages when values
* are assigned to keys 'message' or 'error_message'.
*
* @see _drush_batch_worker().
*/
class DrushBatchContext extends ArrayObject {
#[\ReturnTypeWillChange]
function offsetSet($name, $value) {
if ($name == 'message') {
Drush::logger()->notice(strip_tags($value));
}
elseif ($name == 'error_message') {
Drush::logger()->error(strip_tags($value));
}
parent::offsetSet($name, $value);
}
}

/**
* Process a Drupal batch by spawning multiple Drush processes.
*
Expand Down Expand Up @@ -111,7 +90,7 @@ function drush_batch_command($id) {
function _drush_backend_batch_process($command = 'batch-process', $args = [], $options = []) {
$result = NULL;

$batch =& batch_get();
$batch = &batch_get();

if (isset($batch)) {
$process_info = [
Expand Down Expand Up @@ -176,7 +155,7 @@ function _drush_backend_batch_process($command = 'batch-process', $args = [], $o
* A results array.
*/
function _drush_batch_command($id) {
$batch =& batch_get();
$batch = &batch_get();

$data = Database::getConnection()->select('batch', 'b')
->fields('b', ['batch'])
Expand Down Expand Up @@ -215,8 +194,8 @@ function _drush_batch_command($id) {
* reached.
*/
function _drush_batch_worker() {
$batch =& batch_get();
$current_set =& _batch_current_set();
$batch = &batch_get();
$current_set = &_batch_current_set();
$set_changed = TRUE;

if (empty($current_set['start'])) {
Expand All @@ -237,7 +216,7 @@ function _drush_batch_worker() {
$finished = 1;

if ($item = $queue->claimItem()) {
list($function, $args) = $item->data;
[$callback, $args] = $item->data;

// Build the 'context' array and execute the function call.
$batch_context = [
Expand All @@ -246,20 +225,17 @@ function _drush_batch_worker() {
'finished' => &$finished,
'message' => &$task_message,
];
// Magic wrap to catch changes to 'message' key.
$batch_context = new DrushBatchContext($batch_context);

// Tolerate recoverable errors.
// See https://github.com/drush-ops/drush/issues/1930
$halt_on_error = \Drush\Drush::config()->get('runtime.php.halt-on-error', TRUE);
\Drush\Drush::config()->set('runtime.php.halt-on-error', FALSE);
$message = call_user_func_array($function, array_merge($args, [&$batch_context]));
if (!empty($message)) {
Drush::logger()->notice($message);
call_user_func_array($callback, array_merge($args, [&$batch_context]));
if (!empty($task_message)) {
Drush::logger()->notice(strip_tags($task_message));
}
\Drush\Drush::config()->set('runtime.php.halt-on-error', $halt_on_error);

$finished = $batch_context['finished'];
if ($finished >= 1) {
// Make sure this step is not counted twice when computing $current.
$finished = 0;
Expand Down Expand Up @@ -371,8 +347,8 @@ function _drush_batch_finished() {
*/
function _drush_batch_shutdown() {
if ($batch = batch_get()) {
/** @var \Drupal\Core\Batch\BatchStorage $batch_storage */
$batch_storage = \Drupal::service('batch.storage');
$batch_storage->update($batch);
/** @var \Drupal\Core\Batch\BatchStorage $batch_storage */
$batch_storage = \Drupal::service('batch.storage');
$batch_storage->update($batch);
}
}
19 changes: 5 additions & 14 deletions src/Commands/core/UpdateDBCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Drush\Log\SuccessInterface;
use Drush\Drupal\DrupalUtil;
use DrushBatchContext;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Consolidation\OutputFormatters\StructuredData\UnstructuredListData;
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
Expand Down Expand Up @@ -151,10 +150,10 @@ public function process(string $batch_id, $options = ['format' => 'json']): Unst
* The update number to run.
* @param array $dependency_map
* The update dependency map.
* @param DrushBatchContext $context
* @param array $context
* The batch context object.
*/
public static function updateDoOne(string $module, int $number, array $dependency_map, DrushBatchContext $context): void
public static function updateDoOne(string $module, int $number, array $dependency_map, array $context): void
{
$function = $module . '_update_' . $number;

Expand Down Expand Up @@ -229,9 +228,7 @@ public static function updateDoOne(string $module, int $number, array $dependenc
if (!empty($ret['#abort'])) {
// Record this function in the list of updates that were aborted.
$context['results']['#abort'][] = $function;
// Setting this value will output an error message.
// @see \DrushBatchContext::offsetSet()
$context['error_message'] = "Update failed: $function";
Drush::logger()->error("Update failed: $function");
}

// Record the schema update if it was completed successfully.
Expand All @@ -242,8 +239,6 @@ public static function updateDoOne(string $module, int $number, array $dependenc
} else {
drupal_set_installed_schema_version($module, $number);
}
// Setting this value will output a success message.
// @see \DrushBatchContext::offsetSet()
$context['message'] = "Update completed: $function";
}
}
Expand All @@ -255,7 +250,7 @@ public static function updateDoOne(string $module, int $number, array $dependenc
* The post-update function to execute.
* The batch context object.
*/
public static function updateDoOnePostUpdate(string $function, DrushBatchContext $context): void
public static function updateDoOnePostUpdate(string $function, array $context): void
{
$ret = [];

Expand Down Expand Up @@ -331,12 +326,8 @@ public static function updateDoOnePostUpdate(string $function, DrushBatchContext
if (!empty($ret['#abort'])) {
// Record this function in the list of updates that were aborted.
$context['results']['#abort'][] = $function;
// Setting this value will output an error message.
// @see \DrushBatchContext::offsetSet()
$context['error_message'] = "Update failed: $function";
Drush::logger()->error("Update failed: $function");
} elseif ($context['finished'] == 1 && empty($ret['#abort'])) {
// Setting this value will output a success message.
// @see \DrushBatchContext::offsetSet()
$context['message'] = "Update completed: $function";
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/Drupal/Commands/core/DeployHookCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Drupal\Core\Utility\Error;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use DrushBatchContext;
use Drush\Exceptions\UserAbortException;
use Psr\Log\LogLevel;

Expand Down Expand Up @@ -161,7 +160,7 @@ public function process(string $batch_id, $options = ['format' => 'json']): Unst
* The deploy-hook function to execute.
* The batch context object.
*/
public static function updateDoOneDeployHook(string $function, DrushBatchContext $context): void
public static function updateDoOneDeployHook(string $function, array $context): void
{
$ret = [];

Expand Down Expand Up @@ -239,12 +238,8 @@ public static function updateDoOneDeployHook(string $function, DrushBatchContext
if (!empty($ret['#abort'])) {
// Record this function in the list of updates that were aborted.
$context['results']['#abort'][] = $function;
// Setting this value will output an error message.
// @see \DrushBatchContext::offsetSet()
$context['error_message'] = "Deploy hook failed: $function";
Drush::logger()->error("Deploy hook failed: $function");
} elseif ($context['finished'] == 1 && empty($ret['#abort'])) {
// Setting this value will output a success message.
// @see \DrushBatchContext::offsetSet()
$context['message'] = "Performed: $function";
}
}
Expand Down