1

我正在尝试在我的自定义模块中动态设置 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。

4

2 回答 2

0
  1. 您不需要保存 run_model (您已经将其声明为 xml )
  2. 试试这个: path: crontab/jobs/bookingreservation_summary/schedule/cron_expr 而不是: path: crontab/jobs/process_bookingreservation_summary/schedule/cron_expr

如果你不解决看看它是如何工作的站点地图模块的 cron 设置(它和你想要实现的一样)

于 2012-11-07T11:16:47.540 回答
0

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

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

我认为这可能有效....如果有效,请告诉我。

于 2013-07-23T11:41:02.477 回答