1

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>
5

2 Answers 2

3

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

1
  • Excellent answer. It's kinda hard to find some good stuff about that. You should write a tutorial about this =] Congrats
    – Guerra
    Feb 6, 2014 at 13:59
2

From http://www.adminschoice.com/crontab-quick-reference/

*     *     *   *    *
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

For example,

<!-- Run at 02:01 every Friday -->
<cron_expr>1 2 * * 5</cron_expr>

Some things to keep in mind:

  1. The schedule depends highly on how frequently you are running cron.sh/cron.php on your system. If it is only run every 15 mins then you will never see something run more frequently than that.
  2. Pay attention to the timezone
  3. There are additional cron settings in System > Config > Advanced > System which control scheduling of cron tasks.
2
  • Thanks for your time and answer, but i need to let user configure how the cron will run. It's possible?
    – Guerra
    Feb 5, 2014 at 21:28
  • @Guerra If you want it to be configured by the user then follow the example of Mage_Sitemap. There is no <cron_expr> in etc/config.xml, but rather there is a configuration option in etc/system.xml which sets the crontab/jobs/sitemap_generate/schedule/cron_expr record in the core_config_data database table.
    – nachito
    Feb 5, 2014 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.