CRM_Queue_Runner - Fix upgrade warnings. Extract disableBackgroundExecution().
authorTim Otten <totten@civicrm.org>
Mon, 13 Jun 2022 19:55:51 +0000 (12:55 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 13 Jun 2022 20:06:45 +0000 (13:06 -0700)
This is a revision to #23775.  As before, it prevents a hard error when
upgrading to 5.51.  However, the try-catch produces confusing error messages
in the `CiviCRM.log.*` (ie they indicate a SQL error, but the situation is
actually a normal/expected during upgrade).  This avoids the error messages.

Tested (`r-run`) with `civicrm/upgrade`, `drush civicrm-upgrade-db`, and `cv
upgrade:db` -- for both 5.45=>5.51 and 5.45=>5.49=>5.51.

CRM/Queue/Runner.php

index 1e9d1b2d7ce1a7fb5da806723f3a7dc49c87ae36..80124b03ee2dd83c438a0911a380f98db81aef47 100644 (file)
@@ -184,16 +184,7 @@ class CRM_Queue_Runner {
   public function runAllViaWeb() {
     $_SESSION['queueRunners'][$this->qrid] = serialize($this);
     $url = CRM_Utils_System::url($this->pathPrefix . '/runner', 'reset=1&qrid=' . urlencode($this->qrid));
-    try {
-      // If this was persistent/registered queue, ensure that no one else tries to execute it.
-      CRM_Core_DAO::executeQuery('UPDATE civicrm_queue SET status = NULL WHERE name = %1', [
-        1 => [$this->queue->getName(), 'String'],
-      ]);
-    }
-    catch (PEAR_Exception $e) {
-      // For sites being upgraded the field may not exist as yet.
-      // https://lab.civicrm.org/dev/core/-/issues/3653
-    }
+    $this->disableBackgroundExecution();
     CRM_Utils_System::redirect($url);
   }
 
@@ -208,6 +199,7 @@ class CRM_Queue_Runner {
    *   failed task
    */
   public function runAll() {
+    $this->disableBackgroundExecution();
     $taskResult = $this->formatTaskResult(TRUE);
     while ($taskResult['is_continue']) {
       // setRaiseException should't be necessary here, but there's a bug
@@ -507,4 +499,24 @@ class CRM_Queue_Runner {
     }
   }
 
+  /**
+   * Ensure that background workers will not try to run this queue.
+   */
+  protected function disableBackgroundExecution(): 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;
+      }
+    }
+
+    // We don't actually know if the queue was registered persistently.
+    // But if it was, then it should be disabled.
+    CRM_Core_DAO::executeQuery('UPDATE civicrm_queue SET status = NULL WHERE name = %1', [
+      1 => [$this->queue->getName(), 'String'],
+    ]);
+  }
+
 }