Add runAs to import job
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 24 Oct 2022 22:13:00 +0000 (11:13 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 25 Oct 2022 02:12:30 +0000 (15:12 +1300)
CRM/Import/Parser.php
tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php
tests/phpunit/CRMTraits/Import/ParserTrait.php

index dd5d6cf65659b35f8b8bfabe19ad4bd6ae308aef..0ccb550ac7e4c612d4c8ba0865b9e8413e1605e6 100644 (file)
@@ -667,7 +667,9 @@ abstract class CRM_Import_Parser implements UserJobInterface {
   public function queue(): void {
     $dataSource = $this->getDataSourceObject();
     $totalRowCount = $totalRows = $dataSource->getRowCount(['new']);
-    $queue = Civi::queue('user_job_' . $this->getUserJobID(), ['type' => 'Sql', 'error' => 'abort', 'runner' => 'task', 'user_job_id' => $this->getUserJobID()]);
+    // The retry limit for the queue is set to 5 - allowing for a few deadlocks but we might consider
+    // making this configurable at some point.
+    $queue = Civi::queue('user_job_' . $this->getUserJobID(), ['type' => 'Sql', 'error' => 'abort', 'runner' => 'task', 'user_job_id' => $this->getUserJobID(), 'retry_limit' => 5]);
     UserJob::update(FALSE)->setValues(['queue_id.name' => 'user_job_' . $this->getUserJobID()])->addWhere('id', '=', $this->getUserJobID())->execute();
     $offset = 0;
     $batchSize = 50;
@@ -681,6 +683,7 @@ abstract class CRM_Import_Parser implements UserJobInterface {
         ['userJobID' => $this->getUserJobID(), 'limit' => $batchSize, 'offset' => 0],
         ts('Processed %1 rows out of %2', [1 => $offset + $batchSize, 2 => $totalRowCount])
       );
+      $task->runAs = ['contactId' => CRM_Core_Session::getLoggedInContactID(), 'domainId' => CRM_Core_Config::domainID()];
       $queue->createItem($task);
       $totalRows -= $batchSize;
       $offset += $batchSize;
index 032aeb0cea46c198823abae3c5f919f75f74946e..146ecb778421809789a37c2d10f2ae7f47c968ce 100644 (file)
@@ -32,6 +32,13 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
    */
   protected $entity = 'Contribution';
 
+  /**
+   * Original value for background processing.
+   *
+   * @var bool
+   */
+  protected $enableBackgroundQueueOriginalValue;
+
   /**
    * These extensions are inactive at the start. They may be activated during the test. They should be deactivated at the end.
    *
@@ -48,8 +55,9 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
 
   protected function setUp(): void {
     parent::setUp();
-    $origExtensions = array_column(CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'fullName');
-    $this->assertEquals([], array_intersect($origExtensions, $this->toggleExts), 'These extensions may be enabled and disabled during the test. The start-state and end-state should be the same. It appears that we have an unexpected start-state. Perhaps another test left us with a weird start-state?');
+    $originalExtensions = array_column(CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'fullName');
+    $this->assertEquals([], array_intersect($originalExtensions, $this->toggleExts), 'These extensions may be enabled and disabled during the test. The start-state and end-state should be the same. It appears that we have an unexpected start-state. Perhaps another test left us with a weird start-state?');
+    $this->enableBackgroundQueueOriginalValue = Civi::settings()->get('enableBackgroundQueue');
   }
 
   /**
@@ -68,6 +76,7 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
       CRM_Extension_System::singleton()->getManager()->disable([$ext]);
       CRM_Extension_System::singleton()->getManager()->uninstall([$ext]);
     }
+    Civi::settings()->set('enableBackgroundQueue', $this->enableBackgroundQueueOriginalValue);
     parent::tearDown();
   }
 
@@ -223,9 +232,15 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
    * These features are
    *  - default_value for each field.
    *
+   * @dataProvider getBooleanDataProvider
+   *
+   * @param bool $isBackGroundProcessing
+   *
    * @throws \CRM_Core_Exception
    */
-  public function testImportFromUserJobConfiguration(): void {
+  public function testImportFromUserJobConfiguration(bool $isBackGroundProcessing): void {
+    Civi::settings()->set('enableBackgroundQueue', $isBackGroundProcessing);
+    $this->createLoggedInUser();
     $importMappings = [
       ['name' => 'organization_name'],
       ['name' => 'legal_name'],
index 338acd76b3a247469f9429ff6a16f04fd073c416..c28d26b49fc6c97477e44ba26b6461b242784968 100644 (file)
@@ -70,6 +70,9 @@ trait CRMTraits_Import_ParserTrait {
     catch (CRM_Core_Exception_PrematureExitException $e) {
       $queue = Civi::queue('user_job_' . $this->userJobID);
       $this->assertEquals(1, CRM_Core_DAO::singleValueQuery('SELECT COUNT(*) FROM civicrm_queue_item'));
+      $item = $queue->claimItem(0);
+      $this->assertEquals(['contactId' => CRM_Core_Session::getLoggedInContactID(), 'domainId' => CRM_Core_Config::domainID()], $item->data->runAs);
+      $queue->releaseItem($item);
       $runner = new CRM_Queue_Runner([
         'queue' => $queue,
         'errorMode' => CRM_Queue_Runner::ERROR_ABORT,