Magento cron作业动态表达

时间:2012-11-07 09:26:12

标签: php magento cron

我正在尝试在自定义模块中动态设置cron作业。但是这不会将job_code保存到cron_schedule表。

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

cron_expr被评论,因为我是从配置中生成的。这样可以在config_data表中保存表达式和模型,如

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

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

此类执行并保存到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.'));
    }
}

}

任何人都可以帮助解决我的代码问题,因为无论何时我通过浏览器cron_scedule表更新运行cron.php,但不保存此作业的job_code。

2 个答案:

答案 0 :(得分:0)

  1. 您不需要保存run_model(您已经将其声明为xml)
  2. 试试这个: path: crontab/jobs/bookingreservation_summary/schedule/cron_expr 代替: path: crontab/jobs/process_bookingreservation_summary/schedule/cron_expr
  3. 如果你没有解决它看起来如何工作的站点地图模块的cron设置(它做你想要实现的相同)

答案 1 :(得分:0)

取消注释代码中的以下行,看看它是否有效。

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

我认为这可能有用......如果确实如此,请告诉我。