Skip to content

Instantly share code, notes, and snippets.

@maverick193
Created January 22, 2014 10:12
Show Gist options
  • Save maverick193/8556341 to your computer and use it in GitHub Desktop.
Save maverick193/8556341 to your computer and use it in GitHub Desktop.
Dynamique Cron Expression System Configuration
+ Dans config.xml :
<config>
<crontab>
<jobs>
<cron_task_identifier>
<run>
<model>mon_module/cron::run</model>
</run>
</cron_task_identifier>
</jobs>
</crontab>
</config>
+ Dans system.xml :
<sections>
<my_section translate="label" module="mon_module">
<label>Ma Section</label>
<tab>mon_tab</tab>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<groups>
<cron translate="label" module="mon_module">
<label>Cron Configuration</label>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<fields>
<enabled translate="label">
<label>Enable Cron</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>2</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</enabled>
<time translate="label">
<label>Start Time</label>
<frontend_type>time</frontend_type>
<sort_order>3</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</time>
<frequency translate="label">
<label>Frequency</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_cron_frequency</source_model>
<backend_model>mon_module/system_config_backend_cron</backend_model>
<sort_order>4</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</frequency>
</fields>
</cron>
</groups>
</my_section>
</sections>
+ Dans le Backend Model : Namespace_Monmodule_Model_System_Config_Backend_Cron
class Namespace_Monmodule_Model_System_Config_Backend_Cron extends Mage_Core_Model_Config_Data
{
const CRON_STRING_PATH = 'crontab/jobs/cron_task_identifier/schedule/cron_expr';
const CRON_MODEL_PATH = 'crontab/jobs/cron_task_identifier/run/model';
/**
* Cron settings after save
*
* @return Mage_Adminhtml_Model_System_Config_Backend_Log_Cron
*/
protected function _afterSave()
{
$enabled = $this->getData('groups/cron/fields/enabled/value');
$time = $this->getData('groups/cron/fields/time/value');
$frequncy = $this->getData('groups/cron/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();
if (!empty($cronExprString)) {
Mage::getSingleton('adminhtml/session')->addNotice(
Mage::helper('mon_module')->__('Cron Expression Set To [%s]', $cronExprString)
);
}
}
catch (Exception $e) {
Mage::throwException(
Mage::helper('mon_module')->__('Unable to save the cron expression.')
);
}
}
}
+ Dans le Model Cron :
class Namespace_Monmodule_Model_Cron
{
const XML_PATH_CRON_ENABLED = 'my_section/cron/enabled';
public function run()
{
if (!Mage::getStoreConfigFlag(self::XML_PATH_CRON_ENABLED)) {
return $this;
}
.......
$this->DoSomeStuff();
.......
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment