Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

magento cron in backend configuration

Tags:

magento

So I know how to setup a cron using the config.xml crontab stuff:

<crontab>
    <jobs>
        <millena_export_send_all>
            <schedule><cron_expr>* * * * *</cron_expr></schedule>
            <run><model>millena_export/observer::exportOrderData</model></run>
        </millena_export_send_all>
    </jobs>
</crontab>

But what I am confused about is how to make that cron_expr a setting in the backend that can be changed (every 5 minutes, every 10 minutes, etc). I am thinking I can use a backend_model and then in an after_save method I can do setStoreConfig('path/to/schedule/cron_expr', '*/5 * * * *') or something to that sort and it will save in the cache. Is my thinking correct? Is there a better way to do this?

like image 650
Bryan Ruiz Avatar asked Jun 22 '11 21:06

Bryan Ruiz


People also ask

How can I tell if Magento 2 cron is working?

To check the configured cron jobs you can use the command crontab -l in your terminal and you will see the cron jobs configured and the time they will run. Based on the cron jobs configured, you can view the status of cron jobs(missed, pending or success) in the cron_schedule table.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .


3 Answers

A better solution that doesn't involve a custom backend model.

config.xml

<?xml version="1.0"?>   
<config>
    <modules>
        <Company_Export>
            <version>0.1.0</version>
        </Company_Export>
    </modules>
    <global>
        <models>
            <company_export>
                <class>Company_Export_Model</class>
            </company_export>
        </models>
    </global>
    <default>
        <export>                
            <order>
                <cron_settings>*/5 * * * *</cron_settings>
            </order>
        </export>
    </default>
    <crontab>
        <jobs>                
            <company_export_send_order>
                <schedule>
                    <config_path>export/order/cron_settings</config_path>
                </schedule>
                <run>
                    <model>company_export/observer::exportOrderData</model>
                </run>
            </company_export_send_order>
        </jobs>
    </crontab>
</config>

system.xml

<?xml version="1.0"?>
<config>
    <tabs>
        <feedsconfig translate="label" module="export">
            <label>Feeds Configuration</label>
            <sort_order>99999</sort_order>
        </feedsconfig>
    </tabs>
    <sections>
        <export translate="label" module="export">
            <label>Export</label>
            <tab>feedsconfig</tab>
            <frontend_type>text</frontend_type>
            <sort_order>10000</sort_order>
            <show_in_default>1</show_in_default>
            <groups>
                <order translate="label">
                    <label>Order</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>2</sort_order>
                    <show_in_default>1</show_in_default>
                    <fields>
                         <cron_settings>
                            <label>How often do you want the cron to run?</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>40</sort_order>
                            <comment>Use Crontab Format (Eg. "*/5 * * * *" for every 5 minutes)</comment>
                            <show_in_default>1</show_in_default>
                        </cron_settings>
                    </fields>
                </order>
            </groups>
        </export>
    </sections>
</config>
like image 95
Lee Saferite Avatar answered Oct 10 '22 19:10

Lee Saferite


If I were approaching this problem, I might run the cronjob at whatever the maximum interval useful to me was, and then use the cronjob execution itself to account for the system setting. I'm not sure if the solution you posted will work, but if it does come tell us, because it's another pretty clever approach :)

like image 32
Joe Mastey Avatar answered Oct 10 '22 20:10

Joe Mastey


A proof of concept. Modify to your needs:

<?php

/**
 * Model for Working with the backend cron configuration for export
 *
 * @author bryan
 */
class Company_Export_Model_Config_Cron extends Mage_Core_Model_Config_Data
{

    protected function _afterSave(){

        $groupId = $this->getGroupId();

        $cronStringPath = 'crontab/jobs/company_export_send_' . $groupId . '/schedule/cron_expr';
        $cronModelPath = 'crontab/jobs/company_export_send_' . $groupId . '/run/model';

        $value = $this->getData('groups/' . $groupId . '/fields/cron_setting/value');

        Mage::getModel('core/config_data')
        ->load($cronStringPath, 'path')
            ->setValue($value)
            ->setPath($cronStringPath)
            ->save();
        Mage::getModel('core/config_data')
            ->load($cronModelPath, 'path')
            ->setValue((string) Mage::getConfig()->getNode($cronModelPath))
            ->setPath($cronModelPath)
            ->save();           

    }
}

and the config.xml:

<?xml version="1.0"?>

<config>
    <modules>
        <Company_Export>
            <version>0.1.0</version>
        </Company_Export>
    </modules>
    <global>
        <models>
            <company_export>
                <class>Company_Export_Model</class>
            </company_export>
        </models>
        <helpers>
            <export>
                <class>Company_Export_Helper</class>
            </export>
        </helpers>
        <resources>
            <export_setup>
                <setup>
                    <module>Company_Export</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </export_setup>
            <export_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </export_write>
            <export_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </export_read>
        </resources>
    </global>
    <adminhtml> 
        <acl> 
            <resources> 
                <admin> 
                    <children> 
                        <system> 
                            <children> 
                                <config> 
                                    <children> 
                                        <export> 
                                            <title>Order Export Configuration</title> 
                                        </export> 
                                    </children> 
                                </config> 
                            </children> 
                        </system> 
                    </children> 
                </admin> 
            </resources> 
        </acl> 
    </adminhtml>
    <crontab>
        <jobs>                
            <company_export_send_order>
                <run><model>company_export/observer::exportOrderData</model></run>
            </company_export_send_order>
        </jobs>
    </crontab>
</config>

and system.xml:

<?xml version="1.0"?>

<config>
    <tabs>
        <feedsconfig translate="label" module="export">
            <label>Feeds Configuration</label>
            <sort_order>99999</sort_order>
        </feedsconfig>
    </tabs>
    <sections>
        <export translate="label" module="export">
            <label>Export</label>
            <tab>feedsconfig</tab>
            <frontend_type>text</frontend_type>
            <sort_order>10000</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>0</show_in_website>
            <show_in_store>0</show_in_store>
            <groups>
                <order translate="label">
                    <label>Order</label>
                    <frontend_type>text</frontend_type>
                    <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>
                    <fields>
                         <cron_setting>
                            <label>How often do you want the cron to run?</label>
                            <frontend_type>text</frontend_type>
                            <backend_model>company_export/config_cron</backend_model>
                            <sort_order>40</sort_order>
                            <comment>Use Crontab Format (Eg. "*/5 * * * *" for every 5 minutes)</comment>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                        </cron_setting>
                    </fields>
                </order>
            </groups>
        </export>
    </sections>
</config>
like image 20
Bryan Ruiz Avatar answered Oct 10 '22 19:10

Bryan Ruiz