Skip to content

Commit d4406f5

Browse files
committed
Merge branch 'ACP2E-3387' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-11-14-2024
2 parents 651928c + 405df06 commit d4406f5

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

app/code/Magento/MysqlMq/Model/ResourceModel/Queue.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\MysqlMq\Model\ResourceModel;
77

88
use Magento\Framework\DB\Select;
99
use Magento\Framework\DB\Sql\Expression;
10+
use Magento\Framework\Model\ResourceModel\Db\Context;
1011
use Magento\MysqlMq\Model\QueueManagement;
1112

1213
/**
1314
* Resource model for queue.
1415
*/
1516
class Queue extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
1617
{
18+
/**
19+
*
20+
*/
21+
private const CHUNK_SIZE = 10000;
22+
23+
/**
24+
* @var int|mixed
25+
*/
26+
private int $chunkSize;
27+
28+
/**
29+
* @param Context $context
30+
* @param string|null $connectionName
31+
* @param int|null $chunkSize
32+
*/
33+
public function __construct(Context $context, $connectionName = null, int $chunkSize = null)
34+
{
35+
parent::__construct($context, $connectionName);
36+
if ($chunkSize) {
37+
$this->chunkSize = $chunkSize;
38+
} else {
39+
$this->chunkSize = self::CHUNK_SIZE;
40+
}
41+
}
42+
1743
/**
1844
* Model initialization
1945
*
@@ -163,22 +189,29 @@ public function getMessages($queueName, $limit = null)
163189
}
164190

165191
/**
166-
* Delete messages if there is no queue whrere the message is not in status TO BE DELETED
192+
* Delete messages if there is no queue where the message is not in status TO BE DELETED
167193
*
168194
* @return void
169195
*/
170-
public function deleteMarkedMessages()
196+
public function deleteMarkedMessages(): void
171197
{
172198
$connection = $this->getConnection();
173-
174199
$select = $connection->select()
175200
->from(['queue_message_status' => $this->getMessageStatusTable()], ['message_id'])
176-
->where('status <> ?', QueueManagement::MESSAGE_STATUS_TO_BE_DELETED)
201+
->joinLeft(
202+
['message_status2' => $this->getMessageStatusTable()],
203+
'queue_message_status.message_id = message_status2.message_id AND message_status2.status <> ' .
204+
QueueManagement::MESSAGE_STATUS_TO_BE_DELETED,
205+
[]
206+
)
207+
->where('queue_message_status.status = ?', QueueManagement::MESSAGE_STATUS_TO_BE_DELETED)
208+
->where('message_status2.message_id IS NULL')
177209
->distinct();
178-
$messageIds = $connection->fetchCol($select);
179210

180-
$condition = count($messageIds) > 0 ? ['id NOT IN (?)' => $messageIds] : null;
181-
$connection->delete($this->getMessageTable(), $condition);
211+
$messageIds = $connection->fetchCol($select);
212+
foreach (array_chunk($messageIds, $this->chunkSize) as $messageIdsChunk) {
213+
$connection->delete($this->getMessageTable(), ['id IN (?)' => $messageIdsChunk]);
214+
}
182215
}
183216

184217
/**

app/code/Magento/MysqlMq/Test/Unit/Model/ResourceModel/QueueTest.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2017 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -171,10 +171,12 @@ function ($arg1, $arg2) use ($tableNames) {
171171

172172
/**
173173
* Test for getMessages method.
174+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
175+
* @SuppressWarnings(PHPMD.NPathComplexity)
174176
*
175177
* @return void
176178
*/
177-
public function testGetMessages()
179+
public function testGetMessages(): void
178180
{
179181
$limit = 100;
180182
$queueName = 'queueName0';
@@ -261,7 +263,6 @@ function ($arg1, $arg2 = null) use ($queueName, $select) {
261263
*/
262264
public function testDeleteMarkedMessages()
263265
{
264-
$messageIds = [1, 2];
265266
$tableNames = ['queue_message_status', 'queue_message'];
266267
$connection = $this->getMockBuilder(AdapterInterface::class)
267268
->disableOriginalConstructor()
@@ -284,13 +285,24 @@ function ($arg1, $arg2) use ($tableNames) {
284285
$connection->expects($this->once())->method('select')->willReturn($select);
285286
$select->expects($this->once())
286287
->method('from')->with(['queue_message_status' => $tableNames[0]], ['message_id'])->willReturnSelf();
287-
$select->expects($this->once())->method('where')
288-
->with('status <> ?', QueueManagement::MESSAGE_STATUS_TO_BE_DELETED)
288+
$select->expects($this->once())->method('joinLeft')
289+
->with(
290+
['message_status2' => 'queue_message_status'],
291+
'queue_message_status.message_id = message_status2.message_id AND message_status2.status <> ' .
292+
QueueManagement::MESSAGE_STATUS_TO_BE_DELETED,
293+
[]
294+
)
295+
->willReturnSelf();
296+
$select->expects($this->exactly(2))->method('where')
289297
->willReturnSelf();
290298
$select->expects($this->once())->method('distinct')->willReturnSelf();
291-
$connection->expects($this->once())->method('fetchCol')->with($select)->willReturn($messageIds);
299+
$connection->expects($this->once())->method('fetchCol')->with($select)->willReturn([1, 2]);
300+
292301
$connection->expects($this->once())->method('delete')
293-
->with($tableNames[1], ['id NOT IN (?)' => $messageIds])->willReturn(2);
302+
->with(
303+
$tableNames[1],
304+
['id IN (?)' => [1,2]]
305+
)->willReturn(2);
294306
$this->queue->deleteMarkedMessages();
295307
}
296308

0 commit comments

Comments
 (0)