后端配置中的magento cron

时间:2011-06-22 21:46:15

标签: magento

所以我知道如何使用config.xml crontab设置一个cron:

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

但令我困惑的是如何让cron_expr成为后端中可以更改的设置(每5分钟,每10分钟等)。我想我可以使用backend_model然后在after_save方法中我可以做setStoreConfig('path / to / schedule / cron_expr','* / 5 * * * *')或类似的东西,它将保存在缓存。我的想法是否正确?有更好的方法吗?

3 个答案:

答案 0 :(得分:24)

更好的解决方案,不涉及自定义后端模型。

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>

答案 1 :(得分:1)

如果我正在接近这个问题,我可以在任何对我有用的最大间隔运行cronjob,然后使用cronjob执行本身来考虑系统设置。我不确定你发布的解决方案是否有效,但如果它确实来了,请告诉我们,因为这是另一种非常聪明的方法:)

答案 2 :(得分:1)

概念证明。修改您的需求:

<?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();           

    }
}

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

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