Skip to content

Commit

Permalink
Merge pull request #9089 from mabumusa1/pr_8617
Browse files Browse the repository at this point in the history
  • Loading branch information
npracht committed Dec 1, 2020
2 parents 71940e5 + ebb2415 commit 9713933
Showing 1 changed file with 22 additions and 12 deletions.
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

0 comments on commit 9713933

Please sign in to comment.