From acafce9cd26e8e118f665cff8344c61c8132e141 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 27 Aug 2022 12:14:55 +1200 Subject: [PATCH] Fix import classes to call runAllInteractive --- CRM/Import/Form/Preview.php | 4 ++- CRM/Import/Parser.php | 10 +++++-- CRM/Queue/Queue.php | 2 +- CRM/Queue/Runner.php | 28 +++++++++++-------- CRM/Queue/Service.php | 8 +++--- .../Import/Parser/ContributionTest.php | 2 +- 6 files changed, 34 insertions(+), 20 deletions(-) diff --git a/CRM/Import/Form/Preview.php b/CRM/Import/Form/Preview.php index 693a612f46..2232994440 100644 --- a/CRM/Import/Form/Preview.php +++ b/CRM/Import/Form/Preview.php @@ -99,6 +99,8 @@ abstract class CRM_Import_Form_Preview extends CRM_Import_Forms { /** * Run the import. + * + * @throws \CRM_Core_Exception */ protected function runTheImport(): void { $parser = $this->getParser(); @@ -112,7 +114,7 @@ abstract class CRM_Import_Form_Preview extends CRM_Import_Forms { 'reset' => 1, ], FALSE, NULL, FALSE), ]); - $runner->runAllViaWeb(); + $runner->runAllInteractive(); } /** diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index 88d891f7ca..26b950f8ac 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -663,10 +663,16 @@ abstract class CRM_Import_Parser implements UserJobInterface { UserJob::update(FALSE)->addWhere('id', '=', $userJob['id'])->setValues(['metadata' => $this->userJob['metadata']])->execute(); } - public function queue() { + /** + * Queue the user job as one or more tasks. + * + * @throws \CRM_Core_Exception + */ + public function queue(): void { $dataSource = $this->getDataSourceObject(); $totalRowCount = $totalRows = $dataSource->getRowCount(['new']); - $queue = Civi::queue('user_job_' . $this->getUserJobID(), ['type' => 'Sql', 'error' => 'abort']); + $queue = Civi::queue('user_job_' . $this->getUserJobID(), ['type' => 'Sql', 'error' => 'abort', 'runner' => 'task', 'user_job_id' => $this->getUserJobID()]); + UserJob::update(FALSE)->setValues(['queue_id.name' => 'user_job_' . $this->getUserJobID()])->addWhere('id', '=', $this->getUserJobID())->execute(); $offset = 0; $batchSize = 5; while ($totalRows > 0) { diff --git a/CRM/Queue/Queue.php b/CRM/Queue/Queue.php index 83c27b0f49..3920becde2 100644 --- a/CRM/Queue/Queue.php +++ b/CRM/Queue/Queue.php @@ -101,7 +101,7 @@ abstract class CRM_Queue_Queue { } /** - * Perform any registation or resource-allocation for a new queue + * Perform any registration or resource-allocation for a new queue */ abstract public function createQueue(); diff --git a/CRM/Queue/Runner.php b/CRM/Queue/Runner.php index 80124b03ee..a70ebadfb2 100644 --- a/CRM/Queue/Runner.php +++ b/CRM/Queue/Runner.php @@ -9,6 +9,7 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\UserJob; use Civi\Core\Event\GenericHookEvent; /** @@ -147,10 +148,12 @@ class CRM_Queue_Runner { * environments which support multiprocessing (background queue-workers) can use those; * otherwise, they can use the traditional AJAX runner. * - * To ensure portability, requesters must satisfy the requirements of *both/all* - * execution mechanisms. + * To ensure portability, requesters must satisfy the requirements of + * *both/all* execution mechanisms. + * + * @throws \CRM_Core_Exception */ - public function runAllInteractive() { + public function runAllInteractive(): void { $this->assertRequirementsWeb(); $this->assertRequirementsBackground(); @@ -160,14 +163,13 @@ class CRM_Queue_Runner { 'onEndUrl' => $this->onEndUrl, // 'onEnd' ==> No, see comments in assertRequirementsBackground() ]; - \Civi\Api4\UserJob::save(FALSE)->setRecords([$userJob])->execute(); + UserJob::save(FALSE)->setRecords([$userJob])->execute(); if (Civi::settings()->get('enableBackgroundQueue')) { - return $this->runAllViaBackground(); - } - else { - return $this->runAllViaWeb(); + $this->runAllViaBackground(); + return; } + $this->runAllViaWeb(); } protected function runAllViaBackground() { @@ -441,11 +443,15 @@ class CRM_Queue_Runner { * * @return array|null * The record, per APIv4. - * This may return NULL. UserJobs are required for `runAllInteractively()` and - * `runAllViaBackground()`, but (for backward compatibility) they are not required for `runAllViaWeb()`. + * This may return NULL. UserJobs are required for `runAllInteractively()` + * and + * `runAllViaBackground()`, but (for backward compatibility) they are not + * required for `runAllViaWeb()`. + * + * @throws \CRM_Core_Exception */ protected function findUserJob(): ?array { - return \Civi\Api4\UserJob::get(FALSE) + return UserJob::get(FALSE) ->addWhere('queue_id.name', '=', $this->queue->getName()) ->execute() ->first(); diff --git a/CRM/Queue/Service.php b/CRM/Queue/Service.php index 338c46eac2..372a477cc4 100644 --- a/CRM/Queue/Service.php +++ b/CRM/Queue/Service.php @@ -54,11 +54,11 @@ class CRM_Queue_Service { * * @return \CRM_Queue_Service */ - public static function &singleton($forceNew = FALSE) { - if ($forceNew || !self::$_singleton) { - self::$_singleton = new CRM_Queue_Service(); + public static function &singleton(bool $forceNew = FALSE) { + if ($forceNew || !isset(\Civi::$statics[__CLASS__]['singleton'])) { + \Civi::$statics[__CLASS__]['singleton'] = new CRM_Queue_Service(); } - return self::$_singleton; + return \Civi::$statics[__CLASS__]['singleton']; } /** diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index 1a821648c7..7444fea20e 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -49,7 +49,7 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { * * @param string $thousandSeparator * - * @throws \Exception + * @throws \CRM_Core_Exception */ public function testImportParserWithSoftCreditsByExternalIdentifier(string $thousandSeparator): void { $this->setCurrencySeparators($thousandSeparator); -- 2.25.1