Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Magento cron job task programmatically

Tags:

cron

magento

I want to create cron job task programmatically without using config.xml file. Is it possible?

like image 514
Milos Avatar asked Aug 07 '14 13:08

Milos


2 Answers

I found the solution at: http://www.ayasoftware.com/how-create-cron-jobs-dynamically-magento

$timecreated   = strftime("%Y-%m-%d %H:%M:%S",  mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y")));
$timescheduled = strftime("%Y-%m-%d %H:%M:%S", mktime(date("H"), date("i")+ 5, date("s"), date("m"), date("d"), date("Y")));
$jobCode = 'job_id';

try {
    $schedule = Mage::getModel('cron/schedule');
     $schedule->setJobCode($jobCode)
        ->setCreatedAt($timecreated)
        ->setScheduledAt($timescheduled)
        ->setStatus(Mage_Cron_Model_Schedule::STATUS_PENDING)
        ->save();
   } catch (Exception $e) {
     throw new Exception(Mage::helper('cron')->__('Unable to save Cron expression'));
   }
like image 166
Milos Avatar answered Sep 20 '22 11:09

Milos


I do not see what is the purpose of that and there is probably a better way to do but I think it should be done like that

I never had this case but you probably can use the class Mage_Cron_Model_Schedule Mage::getModel('cron/schedule') and set data accordingly, then save. You need to define what is the cron task anyway in a config.xml for magento to be able to associate.

It should populate the table cron_schedule that it is checked for the cron tasks to be ran.

like image 27
Christophe Ferreboeuf Avatar answered Sep 21 '22 11:09

Christophe Ferreboeuf