Skip to content

Commit

Permalink
Protect FetchMailboxesHandler with a lock
Browse files Browse the repository at this point in the history
  • Loading branch information
marien-probesys committed Nov 19, 2024
1 parent 3f04bd7 commit dce0053
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions src/MessageHandler/FetchMailboxesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Repository\MailboxEmailRepository;
use App\Security\Encryptor;
use Psr\Log\LoggerInterface;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Webklex\PHPIMAP;

Expand All @@ -23,6 +24,7 @@ public function __construct(
private MailboxRepository $mailboxRepository,
private MailboxEmailRepository $mailboxEmailRepository,
private Encryptor $encryptor,
private LockFactory $lockFactory,
private LoggerInterface $logger,
) {
}
Expand All @@ -31,7 +33,24 @@ public function __invoke(FetchMailboxes $message): void
{
$mailboxes = $this->mailboxRepository->findAll();
foreach ($mailboxes as $mailbox) {
$this->fetchMailbox($mailbox);
$lock = $this->lockFactory->createLock("fetch-mailbox.{$mailbox->getId()}", ttl: 10 * 60);

if (!$lock->acquire()) {
continue;
}

try {
$this->fetchMailbox($mailbox);
} catch (\Exception $e) {
$error = $e->getMessage();

$mailbox->setLastError($error);
$this->mailboxRepository->save($mailbox, true);

$this->logger->error("Mailbox #{$mailbox->getId()} error: {$error}");
} finally {
$lock->release();
}
}
}

Expand All @@ -48,46 +67,37 @@ protected function fetchMailbox(Mailbox $mailbox): void
'password' => $this->encryptor->decrypt($mailbox->getPassword()),
]);

try {
$client->connect();
$client->connect();

$postAction = $mailbox->getPostAction();
$postAction = $mailbox->getPostAction();

$folder = $client->getFolderByPath($mailbox->getFolder());
$messages = $folder->messages()->unseen()->get();
$error = '';
$folder = $client->getFolderByPath($mailbox->getFolder());
$messages = $folder->messages()->unseen()->get();
$error = '';

foreach ($messages as $email) {
$mailboxEmail = new MailboxEmail($mailbox, $email);
$this->mailboxEmailRepository->save($mailboxEmail, true);
foreach ($messages as $email) {
$mailboxEmail = new MailboxEmail($mailbox, $email);
$this->mailboxEmailRepository->save($mailboxEmail, true);

if ($postAction === 'delete') {
try {
$email->delete();
} catch (\Exception $e) {
$error = $e->getMessage();
if ($postAction === 'delete') {
try {
$email->delete();
} catch (\Exception $e) {
$error = $e->getMessage();

$this->logger->warning(
"Mailbox #{$mailbox->getId()} error (will try to mark as seen): {$error}"
);
$email->setFlag('Seen');
}
} elseif ($postAction === 'mark as read') {
$this->logger->warning(
"Mailbox #{$mailbox->getId()} error (will try to mark as seen): {$error}"
);
$email->setFlag('Seen');
}
} elseif ($postAction === 'mark as read') {
$email->setFlag('Seen');
}
}

$client->disconnect();

$mailbox->setLastError($error);
$this->mailboxRepository->save($mailbox, true);
} catch (\Exception $e) {
$error = $e->getMessage();

$mailbox->setLastError($error);
$this->mailboxRepository->save($mailbox, true);
$client->disconnect();

$this->logger->error("Mailbox #{$mailbox->getId()} error: {$error}");
}
$mailbox->setLastError($error);
$this->mailboxRepository->save($mailbox, true);
}
}

0 comments on commit dce0053

Please sign in to comment.