-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathProcessQueueCommand.php
More file actions
105 lines (89 loc) · 3.64 KB
/
ProcessQueueCommand.php
File metadata and controls
105 lines (89 loc) · 3.64 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
<?php
declare(strict_types=1);
namespace PhpList\Core\Domain\Messaging\Command;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use PhpList\Core\Domain\Configuration\Model\ConfigOption;
use PhpList\Core\Domain\Configuration\Service\Provider\ConfigProvider;
use PhpList\Core\Domain\Messaging\Message\CampaignProcessor\CampaignProcessorMessage;
use PhpList\Core\Domain\Messaging\Model\Message\MessageStatus;
use PhpList\Core\Domain\Messaging\Repository\MessageRepository;
use PhpList\Core\Domain\Messaging\Service\MessageProcessingPreparator;
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\Lock\LockFactory;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Throwable;
#[AsCommand(
name: 'phplist:process-queue',
description: 'Processes the email campaign queue.'
)]
class ProcessQueueCommand extends Command
{
private MessageRepository $messageRepository;
private LockFactory $lockFactory;
private MessageProcessingPreparator $messagePreparator;
private MessageBusInterface $messageBus;
private ConfigProvider $configProvider;
private TranslatorInterface $translator;
private EntityManagerInterface $entityManager;
public function __construct(
MessageRepository $messageRepository,
LockFactory $lockFactory,
MessageProcessingPreparator $messagePreparator,
MessageBusInterface $messageBus,
ConfigProvider $configProvider,
TranslatorInterface $translator,
EntityManagerInterface $entityManager,
) {
parent::__construct();
$this->messageRepository = $messageRepository;
$this->lockFactory = $lockFactory;
$this->messagePreparator = $messagePreparator;
$this->messageBus = $messageBus;
$this->configProvider = $configProvider;
$this->translator = $translator;
$this->entityManager = $entityManager;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$lock = $this->lockFactory->createLock('queue_processor');
if (!$lock->acquire()) {
$output->writeln($this->translator->trans('Queue is already being processed by another instance.'));
return Command::FAILURE;
}
if ($this->configProvider->isEnabled(ConfigOption::MaintenanceMode)) {
$output->writeln(
$this->translator->trans('The system is in maintenance mode, stopping. Try again later.')
);
return Command::FAILURE;
}
try {
$this->messagePreparator->ensureSubscribersHaveUuid($output);
$this->messagePreparator->ensureCampaignsHaveUuid($output);
$this->entityManager->flush();
} catch (Throwable $throwable) {
$output->writeln($throwable->getMessage());
$lock->release();
return Command::FAILURE;
}
$campaigns = $this->messageRepository->getByStatusAndEmbargo(
status: MessageStatus::Submitted,
embargo: new DateTimeImmutable()
);
try {
foreach ($campaigns as $campaign) {
$this->messageBus->dispatch(new CampaignProcessorMessage(messageId: $campaign->getId()));
}
} catch (Throwable $throwable) {
$output->writeln($throwable->getMessage());
return Command::FAILURE;
} finally {
$lock->release();
}
return Command::SUCCESS;
}
}