2424 * implementations.
2525 */
2626
27+ use Drush \Drush ;
2728use Drush \Log \LogLevel ;
2829
2930/**
@@ -89,6 +90,9 @@ function drush_backend_batch_process($command = 'batch-process', $args = [], $op
8990 *
9091 * @param int $id
9192 * The batch ID of the batch being processed.
93+ *
94+ * @return bool|array
95+ * A results array.
9296 */
9397function drush_batch_command ($ id ) {
9498 include_once (DRUSH_DRUPAL_CORE . '/includes/batch.inc ' );
@@ -104,6 +108,9 @@ function drush_batch_command($id) {
104108 * @param command
105109 * The command to call to process the batch.
106110 *
111+ * @return array
112+ * A return array. The callers only care about the finished marker and an #abort on an operation.
113+ *
107114 */
108115function _drush_backend_batch_process ($ command = 'batch-process ' , $ args , $ options ) {
109116 $ result = NULL ;
@@ -141,8 +148,11 @@ function _drush_backend_batch_process($command = 'batch-process', $args, $option
141148 $ finished = FALSE ;
142149
143150 while (!$ finished ) {
144- $ result = drush_invoke_process ('@self ' , $ command , $ args );
145- $ finished = drush_get_error () || !$ result || (isset ($ result ['context ' ]['drush_batch_process_finished ' ]) && $ result ['context ' ]['drush_batch_process_finished ' ] == TRUE );
151+ $ process = Drush::drush (Drush::aliasManager ()->getSelf (), $ command , $ args );
152+ // Suppress printing stdout since we return a JSON array to the caller there.
153+ $ process ->run ($ process ->showRealtime ()->hideStdout ());
154+ $ result = $ process ->getOutputAsJson ();
155+ $ finished = drush_get_error () || !$ process ->isSuccessful () || (isset ($ result ['drush_batch_process_finished ' ]) && $ result ['drush_batch_process_finished ' ] === TRUE );
146156 }
147157 }
148158
@@ -158,6 +168,9 @@ function _drush_backend_batch_process($command = 'batch-process', $args, $option
158168 *
159169 * @param id
160170 * The batch id of the batch being processed.
171+ *
172+ * @return bool|array
173+ * A results array.
161174 */
162175function _drush_batch_command ($ id ) {
163176 $ batch =& batch_get ();
@@ -234,7 +247,7 @@ function _drush_batch_worker() {
234247 \Drush \Drush::config ()->set ('runtime.php.halt-on-error ' , FALSE );
235248 $ message = call_user_func_array ($ function , array_merge ($ args , [&$ batch_context ]));
236249 if (!empty ($ message )) {
237- drush_print ( strip_tags ($ message), 2 );
250+ Drush:: logger ()-> notice ($ message );
238251 }
239252 \Drush \Drush::config ()->set ('runtime.php.halt-on-error ' , $ halt_on_error );
240253
@@ -302,6 +315,8 @@ function _drush_batch_worker() {
302315 * End the batch processing:
303316 * Call the 'finished' callbacks to allow custom handling of results,
304317 * and resolve page redirection.
318+ *
319+ * @return array
305320 */
306321function _drush_batch_finished () {
307322 $ results = [];
@@ -319,7 +334,7 @@ function _drush_batch_finished() {
319334 $ queue = _batch_queue ($ batch_set );
320335 $ operations = $ queue ->getAllItems ();
321336 $ elapsed = $ batch_set ['elapsed ' ] / 1000 ;
322- $ elapsed = drush_drupal_major_version () >= 8 ? \Drupal::service ('date.formatter ' )->formatInterval ( $ elapsed ) : format_interval ($ elapsed );
337+ $ elapsed = \Drupal::service ('date.formatter ' )->formatInterval ($ elapsed );
323338 call_user_func_array ($ batch_set ['finished ' ], [$ batch_set ['success ' ], $ batch_set ['results ' ], $ operations , $ elapsed ]);
324339 $ results [$ id ] = $ batch_set ['results ' ];
325340 }
@@ -346,6 +361,7 @@ function _drush_batch_finished() {
346361 $ _batch = $ batch ;
347362 $ batch = NULL ;
348363 drush_set_option ('drush_batch_process_finished ' , TRUE );
364+ $ results ['drush_batch_process_finished ' ] = TRUE ;
349365
350366 return $ results ;
351367}
0 commit comments