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