|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\batch_addition_example\Batch; |
| 4 | + |
| 5 | +use Drupal\Core\Batch\BatchBuilder; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class to show how to set up a batch within a batch process. |
| 9 | + */ |
| 10 | +class BatchClass { |
| 11 | + |
| 12 | + /** |
| 13 | + * Process a batch operation. |
| 14 | + * |
| 15 | + * @param int $batchId |
| 16 | + * The batch ID. |
| 17 | + * @param array $chunk |
| 18 | + * The chunk to process. |
| 19 | + * @param bool $addBatch |
| 20 | + * Trigger to create an additional batch within this process. |
| 21 | + * @param array $context |
| 22 | + * Batch context. |
| 23 | + */ |
| 24 | + public static function batchProcess(int $batchId, array $chunk, bool $addBatch, array &$context): void { |
| 25 | + if (!isset($context['sandbox']['progress'])) { |
| 26 | + $context['sandbox']['progress'] = 0; |
| 27 | + $context['sandbox']['max'] = 1000; |
| 28 | + } |
| 29 | + if (!isset($context['results']['updated'])) { |
| 30 | + $context['results']['updated'] = 0; |
| 31 | + $context['results']['skipped'] = 0; |
| 32 | + $context['results']['failed'] = 0; |
| 33 | + $context['results']['progress'] = 0; |
| 34 | + } |
| 35 | + |
| 36 | + if ($addBatch === TRUE) { |
| 37 | + // For every "original" batch run we add another chunk of numbers to be |
| 38 | + // processed. This simulates generating an additional bach run inside an |
| 39 | + // existing batch. |
| 40 | + $batch = new BatchBuilder(); |
| 41 | + $batch->setTitle('Running batch process.') |
| 42 | + ->setFinishCallback([BatchClass::class, 'batchFinished']) |
| 43 | + ->setInitMessage('Commencing') |
| 44 | + ->setProgressMessage('Processing...') |
| 45 | + ->setErrorMessage('An error occurred during processing.'); |
| 46 | + |
| 47 | + // Add a new chunk to process. |
| 48 | + $args = [ |
| 49 | + $batchId + 1000, |
| 50 | + range(1, 100), |
| 51 | + FALSE, |
| 52 | + ]; |
| 53 | + $batch->addOperation([BatchClass::class, 'batchProcess'], $args); |
| 54 | + |
| 55 | + batch_set($batch->toArray()); |
| 56 | + } |
| 57 | + |
| 58 | + // Keep track of progress. |
| 59 | + $context['results']['progress'] += count($chunk); |
| 60 | + $context['results']['process'] = 'Addition batch completed'; |
| 61 | + |
| 62 | + // Message above progress bar. |
| 63 | + $context['message'] = t('Processing batch #@batch_id batch size @batch_size for total @count items.', [ |
| 64 | + '@batch_id' => number_format($batchId), |
| 65 | + '@batch_size' => number_format(count($chunk)), |
| 66 | + '@count' => number_format($context['sandbox']['max']), |
| 67 | + ]); |
| 68 | + |
| 69 | + foreach ($chunk as $number) { |
| 70 | + // Sleep for a bit to simulate work being done. |
| 71 | + usleep(4000 + $number); |
| 72 | + // Decide on the result of the batch. |
| 73 | + $result = rand(1, 4); |
| 74 | + switch ($result) { |
| 75 | + case '1': |
| 76 | + case '2': |
| 77 | + $context['results']['updated']++; |
| 78 | + break; |
| 79 | + |
| 80 | + case '3': |
| 81 | + $context['results']['skipped']++; |
| 82 | + break; |
| 83 | + |
| 84 | + case '4': |
| 85 | + $context['results']['failed']++; |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Handle batch completion. |
| 93 | + * |
| 94 | + * @param bool $success |
| 95 | + * TRUE if all batch API tasks were completed successfully. |
| 96 | + * @param array $results |
| 97 | + * An array of processed node IDs. |
| 98 | + * @param array $operations |
| 99 | + * A list of the operations that had not been completed. |
| 100 | + * @param string $elapsed |
| 101 | + * Batch.inc kindly provides the elapsed processing time in seconds. |
| 102 | + */ |
| 103 | + public static function batchFinished(bool $success, array $results, array $operations, string $elapsed): void { |
| 104 | + $messenger = \Drupal::messenger(); |
| 105 | + if ($success) { |
| 106 | + // Show success message to the user. |
| 107 | + $messenger->addMessage(t('@process processed @count, skipped @skipped, updated @updated, failed @failed in @elapsed.', [ |
| 108 | + '@process' => $results['process'], |
| 109 | + '@count' => $results['progress'], |
| 110 | + '@skipped' => $results['skipped'], |
| 111 | + '@updated' => $results['updated'], |
| 112 | + '@failed' => $results['failed'], |
| 113 | + '@elapsed' => $elapsed, |
| 114 | + ])); |
| 115 | + // Log the batch success. |
| 116 | + \Drupal::logger('delete_orphan')->info( |
| 117 | + '@process processed @count, skipped @skipped, updated @updated, failed @failed in @elapsed.', [ |
| 118 | + '@process' => $results['process'], |
| 119 | + '@count' => $results['progress'], |
| 120 | + '@skipped' => $results['skipped'], |
| 121 | + '@updated' => $results['updated'], |
| 122 | + '@failed' => $results['failed'], |
| 123 | + '@elapsed' => $elapsed, |
| 124 | + ]); |
| 125 | + } |
| 126 | + else { |
| 127 | + // An error occurred. $operations contains the operations that remained |
| 128 | + // unprocessed. Pick the last operation and report on what happened. |
| 129 | + $error_operation = reset($operations); |
| 130 | + if ($error_operation) { |
| 131 | + $message = t('An error occurred while processing %error_operation with arguments: @arguments', [ |
| 132 | + '%error_operation' => print_r($error_operation[0]), |
| 133 | + '@arguments' => print_r($error_operation[1], TRUE), |
| 134 | + ]); |
| 135 | + $messenger->addError($message); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments