2012-11-07 94 views
1

我想在我的自定義模塊中動態設置一個cron作業。但是這不會將job_code保存到cron_schedule表中。Magento cron作業表達式動態

<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這個工作。

+0

你有沒有解決過這個問題,如果是的話,你需要做些什麼才能工作? –

+1

找到這種優秀和整潔的方式來做到這一點,沒有額外的代碼(對於未來的googlers): http://stackoverflow.com/a/11109761/653721 –

回答

0
  1. 你並不需要保存run_model(你已經宣佈它的XML)
  2. 試試這個: path: crontab/jobs/bookingreservation_summary/schedule/cron_expr 代替: path: crontab/jobs/process_bookingreservation_summary/schedule/cron_expr

,如果你不解決看看它是如何工作的網站地圖模塊的cron設置(它的功能與你想要實現的一樣)

0

取消註釋代碼中的以下行, KS。

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

我認爲這可能工作....讓我知道如果它。