Skip to content

Commit 0f45d36

Browse files
committed
ProcessQueueCommand
1 parent 405011a commit 0f45d36

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

src/Domain/Analytics/Service/LinkTrackService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function extractAndSaveLinks(Message $message, int $userId): array
7272
$linkTrack->setUserId($userId);
7373
$linkTrack->setUrl($url);
7474

75-
$this->linkTrackRepository->save($linkTrack);
75+
$this->linkTrackRepository->persist($linkTrack);
7676
$savedLinks[] = $linkTrack;
7777
}
7878

src/Domain/Messaging/Command/ProcessQueueCommand.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpList\Core\Domain\Messaging\Command;
66

77
use DateTimeImmutable;
8+
use Doctrine\ORM\EntityManagerInterface;
89
use PhpList\Core\Domain\Configuration\Model\ConfigOption;
910
use PhpList\Core\Domain\Configuration\Service\Provider\ConfigProvider;
1011
use PhpList\Core\Domain\Messaging\Model\Message\MessageStatus;
@@ -31,14 +32,16 @@ class ProcessQueueCommand extends Command
3132
private CampaignProcessor $campaignProcessor;
3233
private ConfigProvider $configProvider;
3334
private TranslatorInterface $translator;
35+
private EntityManagerInterface $entityManager;
3436

3537
public function __construct(
3638
MessageRepository $messageRepository,
3739
LockFactory $lockFactory,
3840
MessageProcessingPreparator $messagePreparator,
3941
CampaignProcessor $campaignProcessor,
4042
ConfigProvider $configProvider,
41-
TranslatorInterface $translator
43+
TranslatorInterface $translator,
44+
EntityManagerInterface $entityManager,
4245
) {
4346
parent::__construct();
4447
$this->messageRepository = $messageRepository;
@@ -47,6 +50,7 @@ public function __construct(
4750
$this->campaignProcessor = $campaignProcessor;
4851
$this->configProvider = $configProvider;
4952
$this->translator = $translator;
53+
$this->entityManager = $entityManager;
5054
}
5155

5256
/**
@@ -73,11 +77,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7377
$this->messagePreparator->ensureSubscribersHaveUuid($output);
7478
$this->messagePreparator->ensureCampaignsHaveUuid($output);
7579

76-
$campaigns = $this->messageRepository->getByStatusAndEmbargo(
77-
status: MessageStatus::Submitted,
78-
embargo: new DateTimeImmutable()
79-
);
80+
$this->entityManager->flush();
81+
} catch (Throwable $throwable) {
82+
$output->writeln($throwable->getMessage());
83+
84+
return Command::FAILURE;
85+
} finally {
86+
$lock->release();
87+
}
8088

89+
$campaigns = $this->messageRepository->getByStatusAndEmbargo(
90+
status: MessageStatus::Submitted,
91+
embargo: new DateTimeImmutable()
92+
);
93+
94+
try {
8195
foreach ($campaigns as $campaign) {
8296
$this->campaignProcessor->process($campaign, $output);
8397
}

src/Domain/Messaging/Service/Handler/RequeueHandler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public function handle(Message $campaign, ?OutputInterface $output = null): bool
4646
$schedule->setEmbargo($next);
4747
$campaign->setSchedule($schedule);
4848
$campaign->getMetadata()->setStatus(MessageStatus::Submitted);
49-
$this->entityManager->flush();
5049

5150
$output?->writeln($this->translator->trans(
5251
'Requeued campaign; next embargo at %time%',

src/Domain/Messaging/Service/MessageProcessingPreparator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function ensureSubscribersHaveUuid(OutputInterface $output): void
4949
foreach ($subscribersWithoutUuid as $subscriber) {
5050
$subscriber->setUniqueId(bin2hex(random_bytes(16)));
5151
}
52-
$this->entityManager->flush();
5352
}
5453
}
5554

@@ -65,7 +64,6 @@ public function ensureCampaignsHaveUuid(OutputInterface $output): void
6564
foreach ($campaignsWithoutUuid as $campaign) {
6665
$campaign->setUuid(bin2hex(random_bytes(18)));
6766
}
68-
$this->entityManager->flush();
6967
}
7068
}
7169

src/Domain/Messaging/Service/Processor/CampaignProcessor.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public function process(Message $campaign, ?OutputInterface $output = null): voi
103103
['%message_id%' => $campaign->getId()]
104104
)
105105
);
106+
$this->entityManager->flush();
106107
continue;
107108
}
108109

@@ -125,6 +126,7 @@ public function process(Message $campaign, ?OutputInterface $output = null): voi
125126
}
126127

127128
if ($stoppedEarly && $this->requeueHandler->handle($campaign, $output)) {
129+
$this->entityManager->flush();
128130
return;
129131
}
130132

0 commit comments

Comments
 (0)