Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
codit_batch_operations
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
codit_batch_operations
Commits
01c58426
Commit
01c58426
authored
5 months ago
by
Steve Wirt
Browse files
Options
Downloads
Patches
Plain Diff
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
5 months ago
Stage: build
Stage: validate
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
README.md
+17
-4
17 additions, 4 deletions
README.md
src/BatchOperations.php
+23
-0
23 additions, 0 deletions
src/BatchOperations.php
with
40 additions
and
4 deletions
README.md
+
17
−
4
View file @
01c58426
...
...
@@ -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?
...
...
This diff is collapsed.
Click to expand it.
src/BatchOperations.php
+
23
−
0
View file @
01c58426
...
...
@@ -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.
*
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment