-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessCategoryImportQueueCommand.php
More file actions
211 lines (174 loc) · 7.62 KB
/
ProcessCategoryImportQueueCommand.php
File metadata and controls
211 lines (174 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
declare(strict_types=1);
namespace App\Command;
use App\Entity\CategoryImportQueue;
use App\Enum\ArticleImportQueueStatus;
use App\Repository\CategoryImportQueueRepository;
use App\Service\CategoryImportProcessor;
use App\Service\UserNotificationService;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:category-import:process-queue',
description: 'Imports queued category export files and creates or updates categories by slug.'
)]
class ProcessCategoryImportQueueCommand extends Command
{
private const STORAGE_TIMEZONE = 'UTC';
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly ManagerRegistry $managerRegistry,
private readonly CategoryImportQueueRepository $categoryImportQueueRepository,
private readonly CategoryImportProcessor $categoryImportProcessor,
private readonly UserNotificationService $userNotificationService,
private readonly LoggerInterface $logger,
) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$entityManager = $this->entityManager;
$queueRepository = $this->categoryImportQueueRepository;
$queueItem = $queueRepository->claimNextPending();
if (null === $queueItem) {
$io->success('No queued category imports to process.');
return Command::SUCCESS;
}
$processedCount = 0;
$failedCount = 0;
while (null !== $queueItem) {
$connection = $entityManager->getConnection();
try {
$connection->beginTransaction();
$importedItems = $this->categoryImportProcessor->process($queueItem);
$queueItem
->setStatus(ArticleImportQueueStatus::COMPLETED)
->setProcessedAt($this->utcNow())
->setErrorMessage(null);
$entityManager->flush();
$connection->commit();
$this->notifyImportCompletion($queueItem->getRequestedBy()?->getId(), true, $queueItem);
++$processedCount;
$io->success(sprintf(
'Imported %d categor%s from queue item %d.',
$importedItems,
1 === $importedItems ? 'y' : 'ies',
$queueItem->getId() ?? 0,
));
} catch (\Throwable $exception) {
$this->logger->error('Category import failed while processing queue item.', [
'queue_item_id' => $queueItem->getId(),
'file_path' => $queueItem->getFilePath(),
'requested_by_user_id' => $queueItem->getRequestedBy()?->getId(),
'exception' => $exception,
]);
$this->rollbackFailedImportTransaction($entityManager, $connection);
[$entityManager, $queueRepository] = $this->markQueueItemAsFailed(
$queueItem,
$exception->getMessage(),
$entityManager,
$queueRepository,
);
$this->notifyImportCompletion($queueItem->getRequestedBy()?->getId(), false, $queueItem);
++$failedCount;
$io->error(sprintf(
'Category import failed for queue item %d: %s',
$queueItem->getId() ?? 0,
$exception->getMessage()
));
}
$queueItem = $queueRepository->claimNextPending();
}
if (0 === $failedCount) {
$io->success(sprintf('Imported %d queued category file(s).', $processedCount));
return Command::SUCCESS;
}
$io->warning(sprintf(
'Processed %d queued category file(s), but %d import(s) failed.',
$processedCount,
$failedCount
));
return Command::FAILURE;
}
private function rollbackFailedImportTransaction(EntityManagerInterface $entityManager, Connection $connection): void
{
if ($connection->isTransactionActive()) {
$connection->rollBack();
}
if ($entityManager->isOpen()) {
$entityManager->clear();
}
}
private function utcNow(): \DateTimeImmutable
{
return new \DateTimeImmutable('now', new \DateTimeZone(self::STORAGE_TIMEZONE));
}
private function markQueueItemAsFailed(
CategoryImportQueue $queueItem,
string $errorMessage,
EntityManagerInterface $entityManager,
CategoryImportQueueRepository $queueRepository,
): array {
if ($entityManager->isOpen()) {
$managedQueueItem = $entityManager->find(CategoryImportQueue::class, $queueItem->getId());
if (!$managedQueueItem instanceof CategoryImportQueue) {
throw new \RuntimeException(sprintf(
'Unable to reload category import queue item %d after import failure.',
$queueItem->getId() ?? 0,
));
}
$managedQueueItem
->setStatus(ArticleImportQueueStatus::FAILED)
->setErrorMessage($errorMessage);
$entityManager->flush();
return [$entityManager, $queueRepository];
}
$this->managerRegistry->resetManager();
$entityManager = $this->managerRegistry->getManagerForClass(CategoryImportQueue::class);
if (!$entityManager instanceof EntityManagerInterface) {
throw new \RuntimeException('Entity manager for category import queue is not available.');
}
$managedQueueItem = $entityManager->find(CategoryImportQueue::class, $queueItem->getId());
if (!$managedQueueItem instanceof CategoryImportQueue) {
throw new \RuntimeException(sprintf(
'Unable to reload category import queue item %d after import failure.',
$queueItem->getId() ?? 0,
));
}
$managedQueueItem
->setStatus(ArticleImportQueueStatus::FAILED)
->setErrorMessage($errorMessage);
$entityManager->flush();
return [$entityManager, $this->refreshQueueRepository()];
}
private function refreshQueueRepository(): CategoryImportQueueRepository
{
$repository = $this->managerRegistry->getRepository(CategoryImportQueue::class);
if (!$repository instanceof CategoryImportQueueRepository) {
throw new \RuntimeException('Category import queue repository is not available.');
}
return $repository;
}
private function notifyImportCompletion(?int $userId, bool $success, CategoryImportQueue $queueItem): void
{
try {
$this->userNotificationService->notifyCategoryImportCompleted($userId, $success);
} catch (\Throwable $exception) {
$this->logger->warning('Failed to create category import completion notification.', [
'queue_item_id' => $queueItem->getId(),
'file_path' => $queueItem->getFilePath(),
'requested_by_user_id' => $userId,
'success' => $success,
'exception' => $exception,
]);
}
}
}