Chinaunix首页 | 论坛 | 博客
  • 博客访问: 39035
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-08 16:34
文章分类

全部博文(31)

文章存档

2015年(31)

我的朋友
最近访客

分类: 系统运维

2015-06-08 18:08:14

在上面几篇文章中,我们主要讨论了在Magento中怎样使用计划任务(Cron Job),在这些例子中,我们是直接把时间周期设置在config.xml文件中,我们可以根据自己的需要设置每个计划任务的执行周期,这对熟悉Magento的程序员来说很方便,但对于那些不熟悉程序的用户,他们更希望能在Magento的后台中根据需要决定是否开启这个计划任务以及设置计划任务的执行周期,一个典型的例子是我们可以在后台设置货币汇率更新的频率以及清除数据库中Log的频率,下面我们将添加一个计划任务,计划任务的内容和上篇文章相似,在system.log文件中添加一行文字,计划任务的周期在后台配置。

在/app/code/local/MagentoBoy/MyCron/etc/config.xml文件中添加:
  1. <config>
  2.     <crontab>
  3.         <jobs>
  4.             <log_hellomagento>
  5.                 <run>
  6.                     <model>mycron/observer::helloMagento</model>
  7.                 </run>
  8.             </log_hellomagento>
  9.         </jobs>
  10.     </crontab>
  11. </config>
和上个例子不同,在配置文件中我们只需要指定计划任务log_hellomagento的执行的代码,而不需要指定时间周期,同样,我们在/app/code/local/MagentoBoy/MyCron/Model/Observer.php中添加helloMagento()函数:
  1. <?php

  2. class MagentoBoy_Mycron_Model_Observer
  3. {
  4.     //...

  5.     public function helloMagento()
  6.     {
  7.         Mage::log('Hello, Magento!');
  8.         return $this;
  9.     }
  10. }
执行的代码很简单,在/var/log/system.log文件中添加一行'Hello, Magento!',以上的代码和上个例子没有什么差别,下面我们要为这个计划任务建立后台配置表单,首先创建/app/code/local/MagentoBoy/MyCron/etc/system.xml文件:
  1. <?xml version="1.0"?>
  2. <config>
  3.     <sections>
  4.         <system>
  5.             <groups>
  6.                 <log_hellomagento translate="label" module="mycron">
  7.                     <label>Log HelloMagento</label>
  8.                     <frontend_type>text</frontend_type>
  9.                     <sort_order>250</sort_order>
  10.                     <show_in_default>1</show_in_default>
  11.                     <show_in_website>0</show_in_website>
  12.                     <show_in_store>0</show_in_store>
  13.                     <fields>
  14.                         <enabled translate="label">
  15.                             <label>Enable Log HelloMagento</label>
  16.                             <frontend_type>select</frontend_type>
  17.                             <source_model>adminhtml/system_config_source_yesno</source_model>
  18.                             <sort_order>1</sort_order>
  19.                             <show_in_default>1</show_in_default>
  20.                             <show_in_website>0</show_in_website>
  21.                             <show_in_store>0</show_in_store>
  22.                         </enabled>
  23.                         <time translate="label">
  24.                             <label>Start Time</label>
  25.                             <frontend_type>time</frontend_type>
  26.                             <sort_order>2</sort_order>
  27.                             <show_in_default>1</show_in_default>
  28.                             <show_in_website>0</show_in_website>
  29.                             <show_in_store>0</show_in_store>
  30.                         </time>
  31.                         <frequency translate="label">
  32.                             <label>Frequency</label>
  33.                             <frontend_type>select</frontend_type>
  34.                             <source_model>adminhtml/system_config_source_cron_frequency</source_model>
  35.                             <backend_model>mycron/system_config_backend_loghellomagento_cron</backend_model>
  36.                             <sort_order>3</sort_order>
  37.                             <show_in_default>1</show_in_default>
  38.                             <show_in_website>0</show_in_website>
  39.                             <show_in_store>0</show_in_store>
  40.                         </frequency>
  41.                     </fields>
  42.                 </log_hellomagento>
  43.             </groups>
  44.         </system>
  45.     </sections>
  46. </config>
通过上面的代码,我们将在后台System>ADVANCED>System页面的Log Cleaning表单下面添加一个表单Log HelloMagento,表单中包括三个元素:是否开启Log HelloMagento计划任务、计划任务执行的时间以及计划任务执行的频率,其中频率选择框的元素包括Daily/Weekly/Monthly,分别代表每天/每周/每月,这三个表单元素的值将保存在core_config_data表中,path路径分别为system/log_hellomagento/enabled, system/log_hellomagento/time以及system/log_hellomagento/frequency,我们可以为这三个元素设置初始值,在/app/code/local/MagentoBoy/MyCron/etc/config.xml中添加:
  1. <config>
  2.     <default>
  3.         <system>
  4.             <log_hellomagento>
  5.                 <enabled>0</enabled>
  6.                 <time></time>
  7.                 <frequency>D</frequency>
  8.             </log_hellomagento>
  9.         </system>
  10.     </default>
  11. </config>
enabled的值为0说明该计划任务默认不执行,frequency的值为D说明计划任务在开启的情况下默认的执行周期为每天一次,我们可以根据需要设置相应的默认值。

在system.xml中我们指定frequency的backend_model为mycron/system_config_backend_loghellomagento_cron,需要添加/app/code/local/MagentoBoy/MyCron/Model/System/Config/Backend/Loghellomagento/Cron.php文件:
  1. <?php

  2. class MagentoBoy_MyCron_Model_System_Config_Backend_Loghellomagento_Cron extends Mage_Core_Model_Config_Data
  3. {
  4.     const CRON_STRING_PATH = 'crontab/jobs/log_hellomagento/schedule/cron_expr';

  5.     protected function _afterSave()
  6.     {
  7.         $enabled = $this->getData('groups/log_hellomagento/fields/enabled/value');
  8.         $time = $this->getData('groups/log_hellomagento/fields/time/value');
  9.         $frequency = $this->getData('groups/log_hellomagento/fields/frequency/value');

  10.         $frequencyDaily = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_DAILY;
  11.         $frequencyWeekly = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_WEEKLY;
  12.         $frequencyMonthly = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_MONTHLY;

  13.         $cronDayOfWeek = date('N');

  14.         $cronExprArray = array(
  15.             intval($time[1]), # Minute
  16.             intval($time[0]), # Hour
  17.             ($frequency == $frequencyMonthly) ? '1' : '*', # Day of the Month
  18.             '*', # Month of the Year
  19.             ($frequency == $frequencyWeekly) ? '1' : '*', # Day of the Week
  20.         );

  21.         $cronExprString = join(' ', $cronExprArray);

  22.         try {
  23.             Mage::getModel('core/config_data')
  24.                 ->load(self::CRON_STRING_PATH, 'path')
  25.                 ->setValue($cronExprString)
  26.                 ->setPath(self::CRON_STRING_PATH)
  27.                 ->save();
  28.         } catch (Exception $e) {
  29.             throw new Exception(Mage::helper('cron')->__('Unable to save the cron expression.'));
  30.         }
  31.     }
  32. }
以上代码的功能是在保存Log HelloMagento表单的时候,根据所设置的时间和周期为该计划任务生成周期表达式并保存在core_config_data表的'crontab/jobs/log_hellomagento/schedule/cron_expr'元素里,这样Magento就能根据这个表达式为计划任务设定执行周期了。

阅读(661) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~