Re-enable any queues that were disabled for background processing, on end
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 9 Aug 2023 04:30:42 +0000 (16:30 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 9 Aug 2023 04:44:24 +0000 (16:44 +1200)
This addresses what is likely an artificial scenario but

essentially if these steps are followed twice within the same function
- create a queue
- run the queue via runAll

The second time will fail because the queue was disabled & no re-enabled, despite
being perisistent

CRM/Queue/Runner.php

index dfbb37328ac3c4e97b2496492360f59984ff4d9a..33359009b4c709e3da564f67d4f79580f3374803 100644 (file)
@@ -214,6 +214,7 @@ class CRM_Queue_Runner {
 
     if ($taskResult['numberOfItems'] === 0) {
       $result = $this->handleEnd();
+      $this->enableBackgroundExecution();
       if (!empty($result['redirect_url'])) {
         CRM_Utils_System::redirect($result['redirect_url']);
       }
@@ -521,4 +522,24 @@ class CRM_Queue_Runner {
     ]);
   }
 
+  /**
+   * Ensure that background workers will not try to run this queue.
+   */
+  protected function enableBackgroundExecution(): void {
+    if (CRM_Core_Config::isUpgradeMode()) {
+      // Versions <=5.50 do not have `status` column.
+      if (!CRM_Core_DAO::checkTableExists('civicrm_queue') || !CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_queue', 'status')) {
+        // The system doesn't have automatic background workers yet. Neither necessary nor possible to toggle `status`.
+        // See also: https://lab.civicrm.org/dev/core/-/issues/3653
+        return;
+      }
+    }
+
+    // If it was disabled for background processing & has not been otherwise altered then
+    // re-enable it as it might be a persistent queue.
+    CRM_Core_DAO::executeQuery('UPDATE civicrm_queue SET status = "active" WHERE name = %1 AND status IS NULL', [
+      1 => [$this->queue->getName(), 'String'],
+    ]);
+  }
+
 }