Magento Config.xml – Using Dynamic Info

configurationmagento-1.7magento-enterprisexml

How i can set up cron time on config.xml on my module to magento??

Something like::

 <crontab>
        <jobs>
            <captcha_delete_old_attempts>
                <schedule>
                    <cron_expr><!--dynamic info here--></cron_expr>
                </schedule>
                <run>
                    <model>captcha/observer::deleteOldAttempts</model>
                </run>
            </captcha_delete_old_attempts>
            <captcha_delete_expired_images>
                <schedule>
                    <cron_expr><!--dynamic info here--></cron_expr>
                </schedule>
                <run>
                    <model>captcha/observer::deleteExpiredImages</model>
                </run>
            </captcha_delete_expired_images>

        </jobs>
    </crontab>

Best Answer

See my example:
1.Your cron configuration in config.xml:

 <crontab>
        <jobs>
            <uzkart_fetch_payment>
                <run>
                    <model>uzkart/observer::uzkart_fetch_payment</model>
                </run>
            </uzkart_fetch_payment>
        </jobs>
  </crontab>

2.Create cron time configuration in your system.xml:

<config>
    <sections>
        <payment>
            <groups>
                <uzkart_standard translate="label" module="uzkart">
                    <label>Uzkart Standard Payment</label>
                    <sort_order>1</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <!--maybe another fields here-->
                        <frequency translate="label">
                            <label>Frequency</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_cron_frequency</source_model>
                            <!-- Custom backend model which saves cron configuration -->
                            <backend_model>uzkart/adminhtml_system_config_backend_payment_cron</backend_model>
                            <sort_order>200</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>Payment Fetch Frequency</comment>
                        </frequency>
                        <!-- Cron job time -->
                        <time translate="label">
                            <label>Start Time</label>
                            <frontend_type>time</frontend_type>
                            <sort_order>201</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </time>
                    </fields>
                </uzkart_standard>
            </groups>
        </payment>
    </sections>
</config>


It shows following configuration in admin: enter image description here

3.Backend model
<backend_model>uzkart/adminhtml_system_config_backend_payment_cron</backend_model>
refers to this model:

class SSD_Uzkart_Model_Adminhtml_System_Config_Backend_Payment_Cron extends Mage_Core_Model_Config_Data
{
    const CRON_STRING_PATH = 'crontab/jobs/uzkart_fetch_payment/schedule/cron_expr';
    const CRON_MODEL_PATH = 'crontab/jobs/uzkart_fetch_payment/run/model';

    protected function _afterSave()
    {
        $time = $this->getData('groups/uzkart_standard/fields/time/value');
        $frequncy = $this->getData('groups/uzkart_standard/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;

        $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);

        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) {
            throw new Exception(Mage::helper('cron')->__('Unable to save the cron expression.'));
        }
    }
}

4.When you save your configuration in admin, it will generate following lines in core_config_data table of magento and magento cron scheduler will use this config:

enter image description here