简体   繁体   English

Magento Cron工作表达动态

[英]Magento cron job expression dynamically

i am trying to set up a cron job dynamically in my custom module. 我试图在我的自定义模块中动态设置cron作业。 but this do not save the job_code to cron_schedule table. 但这不会将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>

And the cron_expr is commented, because i am generating it from configurations. 并且对cron_expr进行了注释,因为我是从配置生成它的。 and this saves expression and model in config_data table like, 这会将表达式和模型保存在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

this class executes and saves into config_data 此类执行并保存到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. 谁能帮我解决我的代码问题,因为每当我通过浏览器运行cron.php时,cron_scedule表就会更新,但不要保存此作业中的job_code。

  1. you don't need to save the run_model ( you already have declared it the xml ) 您不需要保存run_model(您已经声明了xml)
  2. try this: path: crontab/jobs/bookingreservation_summary/schedule/cron_expr instead of: path: crontab/jobs/process_bookingreservation_summary/schedule/cron_expr 试试这个: path: crontab/jobs/bookingreservation_summary/schedule/cron_expr代替: 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 ) 如果您不解决,请查看站点地图模块的cron设置如何工作(它可以实现您想要的效果)

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. 我认为这可能有效。...让我知道是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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