Skip to content
Snippets Groups Projects
Commit 01c58426 authored by Steve Wirt's avatar Steve Wirt
Browse files

Issue #3471007 by swirt, cdesautels: Add method to run BatchOperation from custom code

parent 9c78f343
No related branches found
No related tags found
No related merge requests found
Pipeline #268793 passed with warnings
......@@ -74,7 +74,7 @@ https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension%21modul
* A description of what this update will do.
*/
function MY_MODULE_NAME_update_90??(&$sandbox) {
$script = \Drupal::classResolver('\Drupal\MY_MODULE_NAME\cbo_scripts\SCRIPT_NAME';
$script = \Drupal::classResolver('\Drupal\MY_MODULE_NAME\cbo_scripts\SCRIPT_NAME');
return $script->run($sandbox, 'hook_update');
}
```
......@@ -95,7 +95,7 @@ Post update runs after all the `hook_update_N` have run. Post update hooks live
* A description of what this post update will do.
*/
function MY_MODULE_NAME_post_update_SCRIPT_NAME(&$sandbox) {
$script = \Drupal::classResolver('\Drupal\MY_MODULE_NAME\cbo_scripts\SCRIPT_NAME';
$script = \Drupal::classResolver('\Drupal\MY_MODULE_NAME\cbo_scripts\SCRIPT_NAME');
return $script->run($sandbox, 'post_update');
}
```
......@@ -109,8 +109,8 @@ Drush deploy hooks run after all the `hook_update_N` and Post update hooks run.
* A description of what this post update will do.
*/
function MY_MODULE_NAME_deploy_SCRIPT_NAME(array &$sandbox) {
$script = \Drupal::classResolver('\Drupal\MY_MODULE_NAME\cbo_scripts\SCRIPT_NAME';
return $script->run($sandbox, 'post_update');
$script = \Drupal::classResolver('\Drupal\MY_MODULE_NAME\cbo_scripts\SCRIPT_NAME');
return $script->run($sandbox, 'deploy');
}
```
......@@ -130,6 +130,19 @@ When coding your batch operation, define a `public function getCronTiming()` and
- 'on the 4th of July'
- 'on the 4th of July after 14:00'
## Running Batch Operation from within custom code
It is possible to run a BatchOperation within custom code (event subscriber,
submit handler...) However, calling it from within custom code does not run it
as a true Drupal Batch, so it is at risk for php timeouts. However if it does
timeout, it will pick up where it left off the.
1. In whatever local/custom code you want to use, add the following
```php
$script = \Drupal::classResolver('\Drupal\MY_MODULE_NAME\cbo_scripts\SCRIPT_NAME');
$script->runByCustomCode('CUSTOM EXECUTOR IDENTIFIER', $allow_skip = TRUE);
```
## FAQs
- How do I run my script and have it keep going if there are errors?
......
......@@ -458,6 +458,29 @@ class BatchOperations implements ContainerInjectionInterface {
}
}
/**
* Method to run the whole batch operation from custom code.
*
* WARNING: This is not running the batch using the Batch API. It is at risk
* for php timeout so this may fail if it takes too long. However, if it does
* fail, it will pick up where it left off the next time it is called. This
* is not a preferred method of running a BatchOperation.
*
* @param string $executor
* The thing that is executing the scripts (Name of your function).
* @param bool $allow_skip
* TRUE allows processing all items, FALSE will stop on the first error.
*/
public function runByCustomCode(string $executor, bool $allow_skip = TRUE): void {
// Establish a sandbox to keep state across multiple runs.
$sandbox = [];
// Initiate the Finished state as not even started.
$sandbox['#finished'] = 0;
do {
$this->run($sandbox, $executor, $allow_skip);
} while ($sandbox['#finished'] < 1);
}
/**
* The callback that gets called when the batch completes.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment