简体   繁体   中英

Magento cron job expression dynamically

i am trying to set up a cron job dynamically in my custom module. but this do not save the job_code to cron_schedule table.

  <config>
    <crontab>
    <jobs>
    <bookingreservation_summary>    
         <!--<schedule><cron_expr>*/5 * * * *</cron_expr></schedule>-->     
        <run><model>bookingreservation/observer::sendBookingSummaryEmail</model></run>
    </bookingreservation_summary>
    </jobs>
   </crontab>
 </config>

And the cron_expr is commented, because i am generating it from configurations. and this saves expression and model in config_data table like,

 path: crontab/jobs/process_bookingreservation_summary/schedule/cron_expr  
 value: 0 17 * * *

 path : crontab/jobs/process_bookingreservation_summary/run/model
 value : bookingreservation/observer::sendBookingSummaryEmail

this class executes and saves into config_data

 class ABC_Bookingreservation_Model_Config_Backend_Cron extends Mage_Core_Model_Config_Data
    {
const CRON_STRING_PATH  = 'crontab/jobs/process_bookingreservation_summary/schedule/cron_expr';
const CRON_MODEL_PATH   = 'crontab/jobs/process_bookingreservation_summary/run/model';

/**
 * Cron settings after save
 *
 * @return Mage_Adminhtml_Model_System_Config_Backend_Log_Cron
 */
protected function _afterSave()
{
    $enabled    = $this->getData('groups/booking_summary/fields/enabled/value');
    $time       = $this->getData('groups/booking_summary/fields/time/value');
    $frequncy   = $this->getData('groups/booking_summary/fields/frequency/value');

    $frequencyDaily     = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_DAILY;
    $frequencyWeekly    = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_WEEKLY;
    $frequencyMonthly   = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_MONTHLY;

    if ($enabled)
{
        $cronDayOfWeek = date('N');
        $cronExprArray = array(
            intval($time[1]),                                   # Minute
            intval($time[0]),                                   # Hour
            ($frequncy == $frequencyMonthly) ? '1' : '*',       # Day of the Month
            '*',                                                # Month of the Year
            ($frequncy == $frequencyWeekly) ? '1' : '*',        # Day of the Week
        );
        $cronExprString = join(' ', $cronExprArray);
    }
    else{

        $cronExprString = '';
    }

    try
    {
        Mage::getModel('core/config_data')
            ->load(self::CRON_STRING_PATH, 'path')
            ->setValue($cronExprString)
            ->setPath(self::CRON_STRING_PATH)
            ->save();

        Mage::getModel('core/config_data')
            ->load(self::CRON_MODEL_PATH, 'path')
            ->setValue((string) Mage::getConfig()->getNode(self::CRON_MODEL_PATH))
            ->setPath(self::CRON_MODEL_PATH)
            ->save();
    }
    catch (Exception $e)
    {
        Mage::throwException(Mage::helper('adminhtml')->__('Unable to save the cron expression.'));
    }
}

}

can any one help where is issue with my code, because whenever i run cron.php through browser cron_scedule table updates, but do not save job_code from this job.

  1. you don't need to save the run_model ( you already have declared it the xml )
  2. try this: path: crontab/jobs/bookingreservation_summary/schedule/cron_expr instead of: path: crontab/jobs/process_bookingreservation_summary/schedule/cron_expr

if you don't solve look at how it works the cron settings for sitemap module ( it does the same you want to achieve )

Uncomment the following line in your code and see whether it works.

 <!--<schedule><cron_expr>*/5 * * * *</cron_expr></schedule>-->

I think this may work....Let me know if it does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM