0

I have created a custom module and a custom cron job. Here is my code:

<crontab>
    <jobs>
        <customer_cron_job>
            <schedule>
                <cron_expr>*/1 * * * *</cron_expr><!-- will be configurable from custom admin frontend-->
            </schedule>
            <run>
                <model>globalconnector/observer::cronUpdates</model>
            </run>
        </customer_cron_job>
    </jobs>
</crontab>

Now this module has a custom admin frontend. Here I want to provide the functionality to the user to specify the time (cron_expr) for the cron job to run. i.e. the 'cron_expr' will be dynamically set. Any help is greatly appreciated.

Krishna
  • 5,194
  • 2
  • 28
  • 33

2 Answers2

0
<crontab>
        <jobs>                
            <company_export_send_order>
                <schedule>
                    <!-- Use the config path in your system.xml here -->
                    <config_path>globalconnector/general/cron_settings</config_path>
                </schedule>
                <run>
                    <model>globalconnector/observer::cronUpdates</model>
                </run>
            </company_export_send_order>
        </jobs>
    </crontab>

In your module's system.xml, simply define a text field in which to enter the cron expression.

More detail here: magento cron in backend configuration

Community
  • 1
  • 1
rbaker86
  • 1,832
  • 15
  • 22
  • But isn't what you said will add a text field to the existing default system configuration page?? I have a custom module.phtml file which contains a form. In the form i have a text field which will take the value of cron_expr from user and then i want this cron_expr to be set in my config.xml. Is there any way to do this? – Krishna Jul 23 '13 at 13:29
0

I got it....Here is what I did.

  1. Commented the following from the config.xml

         <schedule>
            <cron_expr>*/1 * * * *</cron_expr><!-- will be configurable from custom admin frontend-->
        </schedule>
    
  2. I created a block file for my custom adminmodule.phtml. This file contains a form where the user provides the various parameters and then submits the form on the same page.

  3. I created a function named setCron($cronData). This function is called from the phtml:

    $this->setCron($_POST)
    
  4. The function is as follows

    function setCron($cronData){
        $gcSwitch = new Mage_Core_Model_Config();
        $cronExprString = '';
        foreach ($cronData as $key => $value) {
            $cronExprString = $cronExprString.' '.$value;
        }
        $cronExprString = ltrim($cronExprString);
    
        $gcSwitch ->saveConfig('crontab/jobs/customer_cron_job/schedule/cron_expr', $cronExprString, 'default', 0);
    }
    

    What this does is it updates the "core_config_data" table in the database. For more evidence one can take look at this table. This table has the following columns:

    config_id(PK),scope,scope_id,path,value
    

I think now one can understand the above setConfig() methods operation. One more thing the table already contains an entry for "crontab/jobs/customer_cron_job/schedule/cron_expr". If it does not then there is some problem in the config.xml.

Krishna
  • 5,194
  • 2
  • 28
  • 33