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

Add lock name on email send command #9089

Merged
merged 3 commits into from Dec 1, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 22 additions & 12 deletions app/bundles/EmailBundle/Command/ProcessEmailQueueCommand.php
Expand Up @@ -38,6 +38,7 @@ protected function configure()
->addOption('--do-not-clear', null, InputOption::VALUE_NONE, 'By default, failed messages older than the --recover-timeout setting will be attempted one more time then deleted if it fails again. If this is set, sending of failed messages will continue to be attempted.')
->addOption('--recover-timeout', null, InputOption::VALUE_OPTIONAL, 'Sets the amount of time in seconds before attempting to resend failed messages. Defaults to value set in config.')
->addOption('--clear-timeout', null, InputOption::VALUE_OPTIONAL, 'Sets the amount of time in seconds before deleting failed messages. Defaults to value set in config.')
->addOption('--lock-name', null, InputOption::VALUE_OPTIONAL, 'Set name of lock to run multiple mautic:emails:send command at time')
->setHelp(<<<'EOT'
The <info>%command.name%</info> command is used to process the application's e-mail queue
Expand All @@ -53,22 +54,23 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$options = $input->getOptions();
$env = (!empty($options['env'])) ? $options['env'] : 'dev';
$container = $this->getContainer();
$dispatcher = $container->get('event_dispatcher');
$skipClear = $input->getOption('do-not-clear');
$quiet = $input->hasOption('quiet') ? $input->getOption('quiet') : false;
$timeout = $input->getOption('clear-timeout');
$queueMode = $container->get('mautic.helper.core_parameters')->get('mailer_spool_type');
$options = $input->getOptions();
$env = (!empty($options['env'])) ? $options['env'] : 'dev';
$container = $this->getContainer();
$dispatcher = $container->get('event_dispatcher');
$skipClear = $input->getOption('do-not-clear');
$quiet = $input->hasOption('quiet') ? $input->getOption('quiet') : false;
$timeout = $input->getOption('clear-timeout');
$queueMode = $container->get('mautic.helper.core_parameters')->get('mailer_spool_type');
$lockName = $input->getOption('lock-name') ?? '';

if ('file' != $queueMode) {
$output->writeln('Mautic is not set to queue email.');

return 0;
}

if (!$this->checkRunStatus($input, $output)) {
if (!$this->checkRunStatus($input, $output, $lockName)) {
return 0;
}

Expand Down Expand Up @@ -96,11 +98,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
//the file is not old enough to be resent yet
continue;
}
if (!$handle = @fopen($file, 'r+')) {
continue;
}
if (!flock($handle, LOCK_EX | LOCK_NB)) {
/* This message has just been catched by another process */
continue;
}

//rename the file so no other process tries to find it
$tmpFilename = str_replace(['.finalretry', '.sending', '.tryagain'], '', $failedFile);
$tmpFilename = str_replace(['.finalretry', '.sending', '.tryagain'], '', $file);
$tmpFilename .= '.finalretry';
rename($failedFile, $tmpFilename);
rename($file, $tmpFilename);

$message = unserialize(file_get_contents($tmpFilename));
if (false !== $message && is_object($message) && 'Swift_Message' === get_class($message)) {
Expand All @@ -125,11 +134,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
if ($tryAgain) {
$retryFilename = str_replace('.finalretry', '.tryagain', $tmpFilename);
rename($tmpFilename, $retryFilename);
rename($tmpFilename, $retryFilename, $handle);
} else {
//delete the file, either because it sent or because it failed
unlink($tmpFilename);
}
fclose($handle);
}
}
}
Expand Down