Skip to main content
Exact tag has been added for getting better response from the developers
Link
Source Link

Check when custom cron task last ran

I have created a new module that has a custom cron task to export orders. What i'm trying to do though is to only export orders that have been placed since the cron task last completed successfully.

Here's my etc/config.xml

<crontab>
    <jobs>
        <orders_exportsales>
            <schedule><cron_expr>*/5 * * * *</cron_expr></schedule>
            <run><model>Orders/export::ExportSales</model></run>
        </orders_exportsales>
    </jobs>
</crontab>

My controller

public function exportSalesAction()
{
    Mage::getModel('Garraways_OrderSync/export')->OrderExport();
}

In my model, I am then grabbing the orders to export, but I would like to use the 'start from' as the time that the cron task last finished so that I can get new orders. I assume that i will need to check for the relevant job_code, status and finished_at but i'm not sure how to do that from within the model so that it just gets the orders since the cron task last ran?

public function OrderExport()
{

    //Check here for job_code, status and finished_at?
    $lastRun = ;
    $current = now();

    //A collection of orders
    $orderCollection = Mage::getModel('sales/order')->getCollection();

    $orderCollection
        ->addAttributeToFilter('status', array('in' => array('pending', 'complete')))
        ->addFieldToFilter('start_date', array('gteq' => $lastRun))
        ->addFieldToFilter('end_date', array("lteq" => $current));

}