3

I have created a custom module which has cronjob to run after certain interval:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default">
        <!--cron job to import customer,customer assignment,product data,pricing data and listing master-->
        <job name="import_customer_product" instance="Singh\Import\Cron\CustomerProduct" method="execute">
            <schedule>* * * * *</schedule>
        </job>
        <!--cron job to import order,invoice and shipment-->
        <job name="import_order_edi" instance="Singh\Import\Cron\OrderImport" method="execute">
            <schedule>* * * * *</schedule>
        </job>
    </group>
</config>

There are 5 processes running for 1st cron and 3 for 2nd cron. If any exception or error comes in process 1, the cron stops and its status change to error, it bypass other 4 processes and execute the next cron.

Content of CustomerProduct.php:

public function execute() {
    $cron = $this->_cronfactory->create();
    $this->_logger->info("customer and product import cron started");
    $this->_customer->execute();
    $this->_logger->info("customer imported");
    $this->_customerAssignment->execute();
    $this->_logger->info("customer ass imported");
    $this->_product->execute();
    $this->_logger->info("product imported");
    $this->_listingMaster->execute();
    $this->_logger->info("listing imported");
    $this->_productPricing->execute();
    $this->_logger->info("pricing imported");
    $this->_logger->info("customer and product import cron finished");
}

What I want is: the cron should run the next process if it found any exception or error in 1st process, it should not bypass other process.

2
  • 1
    I think you should handle exception in your CRON process
    – Keyur Shah
    Commented Aug 9, 2016 at 12:20
  • I tried by applying exception handling, but nothing happening Commented Aug 9, 2016 at 12:38

1 Answer 1

0

Cron jobs should not throw exceptions and break script execution.

Look for the exception type thrown and catch and log it. In the worst case catch \Exception and any exception will not stop script execution.

Reminder to add logger to your dependency injection.

Example using your code:

public function execute() {
    try {
        $cron = $this->_cronfactory->create();
        $this->_logger->info("customer and product import cron started");
        $this->_customer->execute();
        $this->_logger->info("customer imported");
        $this->_customerAssignment->execute();
        $this->_logger->info("customer ass imported");
        $this->_product->execute();
        $this->_logger->info("product imported");
        $this->_listingMaster->execute();
        $this->_logger->info("listing imported");
        $this->_productPricing->execute();
        $this->_logger->info("pricing imported");
        $this->_logger->info("customer and product import cron finished");
    } catch (\Exception $e) {
       $this->logger->error($e->getMessage());
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.