Magento – Strange issue with Cron not working

cronlog

I am having a very unusual issue at the moment with a cron task not working. In the table cron_schedule, it marks the executed_at time but never a finished_at time.

What makes this unusual is that I can get this to work fine, by placing a Mage::log() in the code.

DOESN'T WORK

function exportNewOrders()
{
    if( ! Mage::helper('warehouse/sales')->isExportEnabled() || ! Mage::helper('warehouse')->shouldRunCronJob('orders_export')) return;

    $_collection = $this->getOrdersCreatedSinceLastExport();

    // Don't generate xml if no new orders found
    if( ! count($_collection)) return;

    $_csv = Mage::getModel('warehouse/csv_order_export');

    foreach( $_collection as $_order )
    {
        $_csv->addOrder($_order);
    }
    $filename = Mage::helper('warehouse/file')->getFilename('orderexport');

    $result = $_csv->saveCsv($filename);

    if(!$result)
    {
        $this->getDebugHelper()->log( $this->getHelper()->__( 'Unable to write orders export to %s - writing error', $filename ));
    }
    else
    {
        $this->getDebugHelper()->log( $this->getHelper()->__( 'Wrote orders export to %s', $filename ));
        $this->setLastDate('sales_last_export');
        Mage::helper('warehouse/file')->setLastFileId(Mage::helper('warehouse/file')->getLastFileId() + 1);
    }

    return $this;
}

DOES WORK

function exportNewOrders()
{
    if( ! Mage::helper('warehouse/sales')->isExportEnabled() || ! Mage::helper('warehouse')->shouldRunCronJob('orders_export')) return;

    $_collection = $this->getOrdersCreatedSinceLastExport();
    Mage::log('hello', null, 'lu.log', true); //SEE THIS LINE

    // Don't generate xml if no new orders found
    if( ! count($_collection)) return;

    $_csv = Mage::getModel('warehouse/csv_order_export');

    foreach( $_collection as $_order )
    {
        $_csv->addOrder($_order);
    }
    $filename = Mage::helper('warehouse/file')->getFilename('orderexport');

    $result = $_csv->saveCsv($filename);

    if(!$result)
    {
        $this->getDebugHelper()->log( $this->getHelper()->__( 'Unable to write orders export to %s - writing error', $filename ));
    }
    else
    {
        $this->getDebugHelper()->log( $this->getHelper()->__( 'Wrote orders export to %s', $filename ));
        $this->setLastDate('sales_last_export');
        Mage::helper('warehouse/file')->setLastFileId(Mage::helper('warehouse/file')->getLastFileId() + 1);
    }

    return $this;
}

As you can see I am doing no extra logic, except for this log, however this now allows the cron task to complete and gives a finished_at time. I have replicated this 100% with several tests.

Anyone come across something similar? Any idea why this is happening?

Best Answer

A Cron Job with no finished_at time, it probably never finished.

You should find something in your error logs.

Also it helps to use the module http://www.magentocommerce.com/magento-connect/aoe-scheduler.html to debug, as it allows to call your cron directly from cli via:
php shell/scheduler.php -action runNow -code productimporter_import_xml

Related Topic