diff --git a/core/includes/form.inc b/core/includes/form.inc index 67088c853f..8514462f71 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -755,58 +755,68 @@ function batch_set($batch_definition) { $batch['sets'][] = $batch_set; } else { - // The set is being added while the batch is running. Insert the new set - // right after the current one to ensure execution order, and store its - // operations in a queue. If multiple sets are being added while the batch - // is running ensure that each new one will be appended instead of - // prepended. - $append_after_index = $batch['current_set']; - $reached_current_set = FALSE; - foreach ($batch['sets'] as $index => $set) { - // As the indexes are not ordered numerically we need to first reach - // the index of the current set and then search for the proper place to - // append the new batch set. - if (!$reached_current_set) { - if ($index == $batch['current_set']) { - $reached_current_set = TRUE; - } - continue; - } - if ($index > $append_after_index) { - if (isset($set['appended_after_index'])) { - $append_after_index = $index; - } - else { - break; - } - } + // The set is being added while the batch is running. + _batch_append_set($batch, $batch_set); + } + } +} + +/** + * Appends a batch set to a running batch. + * + * Inserts the new set right after the current one to ensure execution order, + * and store its operations in a queue. If multiple sets are being added while + * the batch is running ensure that each new one will be appended instead of + * prepended. + * + * @param &$batch + * The batch array. + * @param $batch_set + * The batch set. + */ +function _batch_append_set(&$batch, $batch_set) { + $append_after_index = $batch['current_set']; + $reached_current_set = FALSE; + foreach ($batch['sets'] as $index => $set) { + // As the indexes are not ordered numerically we need to first reach the + // index of the current set and then search for the proper place to append + // the new batch set. + if (!$reached_current_set) { + if ($index == $batch['current_set']) { + $reached_current_set = TRUE; } - $batch_set['appended_after_index'] = $append_after_index; - - // Iterate by reference over the existing batch sets and assign them by - // reference in the new batch sets array in order not to break a retrieved - // reference to the current set. Among other places a reference to the - // current set is being retrieved in _batch_process(). We have to preserve - // the original indexes as they are used to generate the queue name of - // each batch set, as if we do not preserve the indexes of the existing - // batch sets the operations of the new batch will be queued in the queue - // of a previous batch set, because without index preservation we will be - // using not a new, but an existing and already used index for the new - // batch set. - // @see _batch_populate_queue(). - $new_sets = []; - foreach ($batch['sets'] as $index => &$set) { - $new_sets[$index] = &$set; - if ($index == $append_after_index) { - $new_set_index = count($batch['sets']); - $new_sets[$new_set_index] = $batch_set; - } + continue; + } + if ($index > $append_after_index) { + if (isset($set['appended_after_index'])) { + $append_after_index = $index; } - - $batch['sets'] = $new_sets; - _batch_populate_queue($batch, $new_set_index); + else { + break; + } + } + } + $batch_set['appended_after_index'] = $append_after_index; + + // Iterate by reference over the existing batch sets and assign them by + // reference in the new batch sets array in order not to break a retrieved + // reference to the current set. Among other places a reference to the current + // set is being retrieved in _batch_process(). Additionally, we have to + // preserve the original indexes, as they are used to generate the queue name + // of each batch set, otherwise the operations of the new batch set will be + // queued in the queue of a previous batch set. + // @see _batch_populate_queue(). + $new_sets = []; + foreach ($batch['sets'] as $index => &$set) { + $new_sets[$index] = &$set; + if ($index == $append_after_index) { + $new_set_index = count($batch['sets']); + $new_sets[$new_set_index] = $batch_set; } } + + $batch['sets'] = $new_sets; + _batch_populate_queue($batch, $new_set_index); } /** diff --git a/core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc b/core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc index fea4ff6d5e..35c18fec75 100644 --- a/core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc +++ b/core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc @@ -15,7 +15,7 @@ * Performs a simple batch operation. */ function _batch_test_callback_1($id, $sleep, &$context) { - // No-op, but ensure the batch take a couple iterations. + // No-op, but ensure the batch takes a couple iterations. // Batch needs time to run for the test, so sleep a bit. usleep($sleep); // Track execution, and store some result for post-processing in the @@ -39,7 +39,7 @@ function _batch_test_callback_2($start, $total, $sleep, &$context) { // Process by groups of 5 (arbitrary value). $limit = 5; for ($i = 0; $i < $limit && $context['sandbox']['count'] < $total; $i++) { - // No-op, but ensure the batch take a couple iterations. + // No-op, but ensure the batch takes a couple iterations. // Batch needs time to run for the test, so sleep a bit. usleep($sleep); // Track execution, and store some result for post-processing in the @@ -63,7 +63,7 @@ function _batch_test_callback_2($start, $total, $sleep, &$context) { * Implements callback_batch_operation(). */ function _batch_test_callback_5($id, $sleep, &$context) { - // No-op, but ensure the batch take a couple iterations. + // No-op, but ensure the batch takes a couple iterations. // Batch needs time to run for the test, so sleep a bit. usleep($sleep); // Track execution, and store some result for post-processing in the @@ -80,7 +80,7 @@ function _batch_test_callback_5($id, $sleep, &$context) { * Performs a simple batch operation. */ function _batch_test_callback_6($id, $sleep, &$context) { - // No-op, but ensure the batch take a couple iterations. + // No-op, but ensure the batch takes a couple iterations. // Batch needs time to run for the test, so sleep a bit. usleep($sleep); // Track execution, and store some result for post-processing in the @@ -95,7 +95,7 @@ function _batch_test_callback_6($id, $sleep, &$context) { * Performs a simple batch operation. */ function _batch_test_callback_7($id, $sleep, &$context) { - // No-op, but ensure the batch take a couple iterations. + // No-op, but ensure the batch takes a couple iterations. // Batch needs time to run for the test, so sleep a bit. usleep($sleep); // Track execution, and store some result for post-processing in the diff --git a/core/modules/system/tests/modules/batch_test/batch_test.module b/core/modules/system/tests/modules/batch_test/batch_test.module index e169f61af9..76ae17b1da 100644 --- a/core/modules/system/tests/modules/batch_test/batch_test.module +++ b/core/modules/system/tests/modules/batch_test/batch_test.module @@ -183,7 +183,7 @@ function _batch_test_batch_6() { } /** - * Batch 6: Performs two batches within a batch. + * Batch 7: Performs two batches within a batch. * * Operations: * - op 7 from 1 to 5, @@ -197,11 +197,11 @@ function _batch_test_batch_7() { $sleep = (1000000 / $total) * 2; $operations = []; - for ($i = 1; $i <= round($total / 2); $i++) { + for ($i = 1; $i <= $total / 2; $i++) { $operations[] = ['_batch_test_callback_7', [$i, $sleep]]; } $operations[] = ['_batch_test_nested_batch_callback', [[5, 6]]]; - for ($i = round($total / 2) + 1; $i <= $total; $i++) { + for ($i = ($total / 2) + 1; $i <= $total; $i++) { $operations[] = ['_batch_test_callback_7', [$i, $sleep]]; } $batch = [ @@ -254,7 +254,6 @@ function batch_test_stack($data = NULL, $reset = FALSE) { if (!isset($data)) { return $state->get('batch_test.stack'); } - $state->resetCache(); $stack = $state->get('batch_test.stack'); $stack[] = $data; $state->set('batch_test.stack', $stack);