From fc8f09b3d9c1a75122ed60dc70bda02c2089e85f Mon Sep 17 00:00:00 2001
From: Steve Wirt <swirtMiles@138230.no-reply.drupal.org>
Date: Thu, 21 Mar 2024 22:44:22 -0400
Subject: [PATCH] Issue #3431994 by swirt: Document how to run from a
 hook_post_update

---
 README.md | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 9354f7f..fd187a5 100644
--- a/README.md
+++ b/README.md
@@ -54,8 +54,7 @@ None
   https://git.drupalcode.org/project/codit_batch_operations/-/blob/1.0.x/src/cbo_scripts/TestDo10Things.php
 
 ## How to run Batch Operation from hook_update_n()?
-hook_update_n() runs before config import and before deploy (when using drush
-deploy) Full documentation about hook_update_n()
+hook_update_n() runs before config import before hook_post_update() and before deploy (when using drush deploy) They live in any module's install file. (MY_MODULE_NAME.install.php) Full documentation about hook_update_n()
 https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension%21module.api.php/function/hook_update_N/
 1.  In whatever local/custom module you want to run this Batch Operation, add
     or use the existing MY_MODULE_NAME.install add the following
@@ -77,6 +76,21 @@ https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension%21modul
     - drush deploy
     - visit /update.php and run updates in the UI.
 
+## How to run Batch Operation from hook_post_update()?
+Post update runs after all the hook_update_N have run.  Post update hooks live in their own file (MY_MODULE_NAME.post_update.php).  Hook_post_updates are not sequentially numbered, though they could be.  They are suffixed with a name. I suggest using the same name as your BatchOp script or anything else that is meaningful.
+1.  In whatever local/custom module you want to run this Batch Operation, add
+    or use the existing MY_MODULE_NAME.post_update.php add the following
+    ```php
+      /**
+       * A description of what this post update will do.
+       */
+      function MY_MODULE_NAME_post_update_SCRIPTNAME(&$sandbox) {
+        $script = \Drupal::classResolver('\Drupal\MY_MODULE_NAME\cbo_scripts\SCRIPTNAME';
+        return $script->run($sandbox, 'post_update');
+      }
+    ```
+    hook_post_update functions are only ever run once.  The function name is tracked, but is not sequential.  Drupal is more stable and fully operationnal than it is during hook_update_N().
+
 ## FAQs
 
 - How do I run my script and have it keep going if there are errors?
-- 
GitLab