Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message Queue Changes Tracking in setup:db:status Command #39698

Open
wants to merge 3 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions lib/internal/Magento/Framework/Setup/Queue/UpToDateQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Framework\Setup\Queue;

use Magento\Framework\Setup\UpToDateValidatorInterface;
use Magento\Framework\Module\ModuleList;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Xml\Parser;
use Magento\Framework\Component\ComponentRegistrar;

class UpToDateQueue implements UpToDateValidatorInterface
{
/**
* @param ModuleList $moduleList
* @param LoggerInterface $logger
* @param ResourceConnection $resourceConnection
* @param File $fileDriver
* @param Parser $xmlParser
* @param ComponentRegistrar $componentRegistrar
*/
public function __construct(
private readonly ModuleList $moduleList,
private readonly LoggerInterface $logger,
private readonly ResourceConnection $resourceConnection,
private readonly File $fileDriver,
private readonly Parser $xmlParser,
private readonly ComponentRegistrar $componentRegistrar
) {}

/**
* @return string
*/
public function getNotUpToDateMessage(): string
{
return 'Queue is not up to date';
}

/**
* @return bool
*/
public function isUpToDate(): bool
{
$existingQueues = $this->getQueueFromDatabase();
$queueFromXml = $this->getQueueFromXml();

return $this->queuesMatch($existingQueues, $queueFromXml);
}

private function queuesMatch(array $existingQueues, array $queueFromXml): bool
{
if (count($existingQueues) !== count($queueFromXml)) {
return false;
}

sort($existingQueues);
sort($queueFromXml);

return $existingQueues === $queueFromXml;
}

private function getQueueFromDatabase(): array
{
$connection = $this->resourceConnection->getConnection();
$tableName = $connection->getTableName('queue');
$select = $connection->select()->distinct()->from($tableName, ['name']);
return $connection->fetchCol($select);
}

private function getQueueFromXml(): array
{
$queues = [];

foreach ($this->moduleList->getAll() as $moduleName) {
$modulePath = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName['name']);

if (!$modulePath) {
continue;
}

$queueFile = $modulePath . '/etc/queue_consumer.xml';

if (!$this->fileDriver->isExists($queueFile)) {
continue;
}

try {
$xmlContent = $this->fileDriver->fileGetContents($queueFile);
$parsedXml = $this->xmlParser->loadXML($xmlContent)->xmlToArray();

if (isset($parsedXml['config']['_value']['consumer'])) {
$consumers = $parsedXml['config']['_value']['consumer'];

foreach ($consumers as $item) {
if (isset($item['_attribute']['queue'])) {
$queues[] = $item['_attribute']['queue'];
} else {
if (isset($item['queue'])) {
$queues[] = $item['queue'];
}
}
}
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
continue;
}
}
return array_unique($queues);
}
}
2 changes: 2 additions & 0 deletions setup/src/Magento/Setup/Console/Command/DbStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\Setup\OldDbValidator;
use Magento\Framework\Setup\Patch\UpToDateData;
use Magento\Framework\Setup\Patch\UpToDateSchema;
use Magento\Framework\Setup\Queue\UpToDateQueue;
use Magento\Framework\Setup\UpToDateValidatorInterface;
use Magento\Setup\Model\ObjectManagerProvider;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function __construct(ObjectManagerProvider $objectManagerProvider, Deploy
$this->objectManagerProvider->get()->get(UpToDateSchema::class),
$this->objectManagerProvider->get()->get(UpToDateData::class),
$this->objectManagerProvider->get()->get(OldDbValidator::class),
$this->objectManagerProvider->get()->get(UpToDateQueue::class),
];
parent::__construct();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,21 @@ protected function setUp(): void
->getMock(),
'old_validator' => $this->getMockBuilder(UpToDateValidatorInterface::class)
->getMock(),
'up_to_date_queue' => $this->getMockBuilder(UpToDateValidatorInterface::class)
->getMock(),
];

$objectManagerProvider->expects($this->any())
->method('get')
->willReturn($objectManager);
$objectManager->expects(self::exactly(4))
$objectManager->expects(self::exactly(5))
->method('get')
->willReturnOnConsecutiveCalls(
$this->validators['declarative_schema'],
$this->validators['up_to_date_schema'],
$this->validators['up_to_date_data'],
$this->validators['old_validator']
$this->validators['old_validator'],
$this->validators['up_to_date_queue']
);
$this->command = new DbStatusCommand($objectManagerProvider, $this->deploymentConfig);
}
Expand All @@ -99,6 +102,9 @@ public function testExecute()
$this->validators['declarative_schema']->expects(self::once())
->method('isUpToDate')
->willReturn(true);
$this->validators['up_to_date_queue']->expects(self::once())
->method('isUpToDate')
->willReturn(true);
$this->deploymentConfig->expects($this->once())
->method('isAvailable')
->willReturn(true);
Expand Down