eltrino / diamantedesk-application

DiamanteDesk
http://diamantedesk.com/
Other
118 stars 42 forks source link

Fix : Imap SSL read issue with parellel CronJob execution #56

Open sreekuttan-m-achari opened 7 years ago

sreekuttan-m-achari commented 7 years ago

Hi , We were having the following issue while running email processing cronjob

`

[Diamante\EmailProcessingBundle\Model\MessageProcessingException]
cannot read - connection closed?

Exception trace: () at /var/www/html/desk-application/vendor/diamante/email-processing-bundle/Diamante/EmailProcessingBundle/Infrastructure/Message/Zend/ImapMessageProvider.php:85 Diamante\EmailProcessingBundle\Infrastructure\Message\Zend\ImapMessageProvider->fetchMessagesToProcess() at /var/www/html/desk-application/vendor/diamante/email-processing-bundle/Diamante/EmailProcessingBundle/Model/Service/MessageProcessingManager.php:56 Diamante\EmailProcessingBundle\Model\Service\MessageProcessingManager->handle() at /var/www/html/desk-application/vendor/diamante/email-processing-bundle/Diamante/EmailProcessingBundle/Api/Impl/EmailProcessingServiceImpl.php:67 Diamante\EmailProcessingBundle\Api\Impl\EmailProcessingServiceImpl->process() at /var/www/html/desk-application/vendor/diamante/email-processing-bundle/Diamante/EmailProcessingBundle/Command/GeneralEmailProcessingCommand.php:32 Diamante\EmailProcessingBundle\Command\GeneralEmailProcessingCommand->execute() at /var/www/html/desk-application/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:256 Symfony\Component\Console\Command\Command->run() at /var/www/html/desk-application/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:846 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/desk-application/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:189 Symfony\Component\Console\Application->doRun() at /var/www/html/desk-application/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:96 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/html/desk-application/vendor/jms/job-queue-bundle/JMS/JobQueueBundle/Console/Application.php:45 JMS\JobQueueBundle\Console\Application->doRun() at /var/www/html/desk-application/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:120 Symfony\Component\Console\Application->run() at /var/www/html/desk-application/app/console:23

`

The cron was getting initiated every 5 minutes while the existing email processing was on going. So we created a file based locking mechanism to prevent this parallel execution.

File location :: "/vendor/diamante/email-processing-bundle/Diamante/EmailProcessingBundle/Command/GeneralEmailProcessingCommand.php "

Modification :

` protected function execute(InputInterface $input, OutputInterface $output) {

    //$flagFile = 'cronFlg.flg';

    $flagFile = dirname(__FILE__).'/cronFlg.flg';

    $output->writeln("Flag-File: ".$flagFile." :  ".date("Y-m-d h:i:sa"));

    if (!file_exists($flagFile)) {
        if (file_put_contents($flagFile, "COMPLETED")) {
            $output->writeln("Flag-File Created : ".date("Y-m-d h:i:sa"));
        } else {
            $output->writeln("Error! Unable to create Flag-File Please check folder/file permissions: ".date("Y-m-d h:i:sa"));
        }
    } else {

        $flg = file_get_contents($flagFile);

        if ($flg == 'COMPLETED') {
            file_put_contents($flagFile, "START");

            $output->writeln("Start General Email Processing : ".date("Y-m-d h:i:sa"));
            $this->getContainer()->get('diamante.email_processing.service')->process();
            $output->writeln("General Email Processing Done : ".date("Y-m-d h:i:sa"));

            file_put_contents($flagFile, "COMPLETED");
        } else {
            $output->writeln("Processing Emails... : ".date("Y-m-d h:i:sa"));
        }
    }

} `