Skip to content

Commit

Permalink
Remove DrushBatchContext magic wrapper for batch . (#5354)
Browse files Browse the repository at this point in the history
Co-authored-by: Halison Fernandes <halison@imagexmedia.com>
Co-authored-by: Moshe Weitzman <weitzman@tejasa.com>
  • Loading branch information
3 people authored Mar 23, 2023
1 parent 884369d commit 481c931
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 57 deletions.
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 @@ -8,7 +8,6 @@
use Drush\Log\SuccessInterface;
use Drush\Drupal\DrupalUtil;
use Drush\Attributes as CLI;
use DrushBatchContext;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Consolidation\OutputFormatters\StructuredData\UnstructuredListData;
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
Expand Down Expand Up @@ -155,10 +154,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 @@ -233,9 +232,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 @@ -246,8 +243,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 @@ -259,7 +254,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 @@ -335,12 +330,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 @@ -15,7 +15,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 @@ -154,7 +153,7 @@ public function process(string $batch_id, $options = ['format' => 'json']): Unst
/**
* Batch command that executes a single deploy hook.
*/
public static function updateDoOneDeployHook(string $function, DrushBatchContext $context): void
public static function updateDoOneDeployHook(string $function, array $context): void
{
$ret = [];

Expand Down Expand Up @@ -232,12 +231,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

0 comments on commit 481c931

Please sign in to comment.