From 6a8a701cd48c30286ffe0d2d6ad9c76bda904943 Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 7 Nov 2024 09:47:48 +0200 Subject: [PATCH 1/3] UHF-9452: Queue entities without checking revisions --- helfi_api_base.module | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/helfi_api_base.module b/helfi_api_base.module index 7774a17..21c3d8a 100644 --- a/helfi_api_base.module +++ b/helfi_api_base.module @@ -80,16 +80,11 @@ function helfi_api_base_entity_update(EntityInterface $entity) : void { if (!$revisionManager->entityTypeIsSupported($entity->getEntityTypeId())) { return; } - $revisions = $revisionManager->getRevisions($entity->getEntityTypeId(), $entity->id()); - - // Queue entity revisions for deletion. - if ($revisions) { - $queue = \Drupal::queue('helfi_api_base_revision'); - $queue->createItem([ - 'entity_id' => $entity->id(), - 'entity_type' => $entity->getEntityTypeId(), - ]); - } + $queue = \Drupal::queue('helfi_api_base_revision'); + $queue->createItem([ + 'entity_id' => $entity->id(), + 'entity_type' => $entity->getEntityTypeId(), + ]); } /** From ab2c683c6ceab2f531193d820e1595972a9cdfb8 Mon Sep 17 00:00:00 2001 From: tuutti Date: Mon, 11 Nov 2024 11:34:59 +0200 Subject: [PATCH 2/3] UHF-9452: Fixed tests --- tests/src/Kernel/Entity/Revision/RevisionManagerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/Kernel/Entity/Revision/RevisionManagerTest.php b/tests/src/Kernel/Entity/Revision/RevisionManagerTest.php index a6029eb..ee14a75 100644 --- a/tests/src/Kernel/Entity/Revision/RevisionManagerTest.php +++ b/tests/src/Kernel/Entity/Revision/RevisionManagerTest.php @@ -144,7 +144,7 @@ public function testRevision() : void { ->save(); $this->assertCount(0, $this->getSut()->getRevisions('remote_entity_test', $entity->id())); - $this->assertQueueItems(0); + $this->assertQueueItems(1); for ($i = 0; $i < 10; $i++) { $rmt = $storage->load($entity->id()); @@ -163,7 +163,7 @@ public function testRevision() : void { $storage->createRevision($rmt)->save(); } // Make sure items are queued on entity update. - $this->assertQueueItems(5); + $this->assertQueueItems(11); $revisions = $this->getSut()->getRevisionsPerLanguage('remote_entity_test', $entity->id()); From 2ffc309283a4a43f3df951c7902908c4fbd41928 Mon Sep 17 00:00:00 2001 From: tuutti Date: Mon, 11 Nov 2024 13:43:48 +0200 Subject: [PATCH 3/3] UHF-9452: Only queue once per request --- helfi_api_base.module | 18 +++++++++++++----- .../Entity/Revision/RevisionManagerTest.php | 4 ++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/helfi_api_base.module b/helfi_api_base.module index 21c3d8a..6b69667 100644 --- a/helfi_api_base.module +++ b/helfi_api_base.module @@ -80,11 +80,19 @@ function helfi_api_base_entity_update(EntityInterface $entity) : void { if (!$revisionManager->entityTypeIsSupported($entity->getEntityTypeId())) { return; } - $queue = \Drupal::queue('helfi_api_base_revision'); - $queue->createItem([ - 'entity_id' => $entity->id(), - 'entity_type' => $entity->getEntityTypeId(), - ]); + static $cache = []; + + $key = sprintf('%s-%s', $entity->getEntityTypeId(), $entity->id()); + + // Only queue entity once per request. + if (!isset($cache[$key])) { + $queue = \Drupal::queue('helfi_api_base_revision'); + $queue->createItem([ + 'entity_id' => $entity->id(), + 'entity_type' => $entity->getEntityTypeId(), + ]); + $cache[$key] = TRUE; + } } /** diff --git a/tests/src/Kernel/Entity/Revision/RevisionManagerTest.php b/tests/src/Kernel/Entity/Revision/RevisionManagerTest.php index ee14a75..c11d758 100644 --- a/tests/src/Kernel/Entity/Revision/RevisionManagerTest.php +++ b/tests/src/Kernel/Entity/Revision/RevisionManagerTest.php @@ -162,8 +162,8 @@ public function testRevision() : void { $storage->createRevision($rmt)->save(); } - // Make sure items are queued on entity update. - $this->assertQueueItems(11); + // Make sure items are only queued once per request. + $this->assertQueueItems(1); $revisions = $this->getSut()->getRevisionsPerLanguage('remote_entity_test', $entity->id());