【问题标题】:magento cron in backend configuration后端配置中的magento cron
【发布时间】:2015-12-31 15:52:55
【问题描述】:

所以我知道如何使用 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 * * * *') 或类似的东西,它将保存在缓存。我的想法正确吗?有没有更好的方法来做到这一点?

【问题讨论】:

    标签: magento


    【解决方案1】:

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

    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.1(大约 2009 年)开始就是这样
    • 为什么这实际上没有任何官方记录!?
    • 确实很棒,有时 Magento 会用这样的简洁解决方案让您大吃一惊 - 但要找到它的文档并不容易
    【解决方案2】:

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

    【讨论】:

    • 按预期工作。我花了一点时间来弄清楚如何加载和保存值,但很好去
    【解决方案3】:

    概念证明。根据您的需要进行修改:

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

    【讨论】:

    • 这行得通吗?我不会想到可以通过数据库表设置任意 XML 值,core_config_data
    • 虽然这行得通(从外观上看)我已经发布了一个更好的解决方案,自 1.1.1 起就在核心代码中得到支持
    猜你喜欢
    • 2011-07-02
    • 2017-11-03
    • 2017-04-23
    • 1970-01-01
    • 2015-06-07
    • 2014-01-14
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多