首页 文章

简单的magento cronjob扩展

提问于
浏览
1

我正在尝试编写一个简单的扩展,为magento注册一个cron作业,似乎有一些我在这里错过的东西 . cron作业从未被安排在cron_schedule表中没有它的条目 . 我甚至尝试在那里输入一个,但工作从未执行过 . 默认的magento cron作业运行正常 . 通过查看/ var / log / syslog,我可以看到正在执行的cron.sh . 扩展非常简单 . 只是一个应该设置cron作业的模型(Observer.php)和config.xml . 我觉得自己像个傻瓜 . 从我在网上看到的,这应该是相当简单的设置,但我无处可去 .

这是config.xml的内容

<?xml version="1.0"?>
<config>
    <modules>
        <Twobuy_Import>
            <version>1.0</version>
        </Twobuy_Import>
    </modules>
    <global>
        <models>
            <twobuy_import>
                <class>Twobuy_Import_Model</class>
            </twobuy_import>
        </models>
    </global>
    <crontab>
        <jobs>
            <twobuy_import>
                <schedule>
                    <cron_expr>50 13 * * *</cron_expr>  <!-- I set this value to say 10-20-30 minutes in the future, and wait, nothing ever happens -->
                </schedule>
                <run>
                    <model>twobuy_import/observer::parse</model>
                </run>
            </twobuy_import>
        </jobs>
    </crontab>
</config>

这是app / etc / modules中的Twobuy_Import.xml(扩展名显示在system-> configuration-> advanced下)

<?xml version="1.0"?>
<config>
    <modules>
        <Twobuy_Import>
            <active>true</active>
            <codePool>local</codePool>
        </Twobuy_Import>
    </modules>
</config>

和Observer.php

class Twobuy_Import_Model_Observer
{
    public function parse()
    {
        $row = 1;
        if (($handle = fopen(Mage::getBaseDir('var')."/orders/Stock/webstock.csv", "r")) !== FALSE)
        {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
            {
                if($row++ > 1)
                {
                    if($product = Mage::getModel('catalog/product')->loadByAttribute('sku',$data[1]))
                    {
                        $product = Mage::getModel('catalog/product')->load($product->getEntityId());
                        $product->setPrice($data['3']);
                        $product->setAnalysiscode($data['4']);
                        if($data[5] == 'Y')
                            $stockData['is_in_stock'] = 0;
                        else
                            $stockData['is_in_stock'] = 1;
                        $product->setStockData($stockData);
                        try
                        {
                            $product->save();
                        }
                        catch (Exception $e)
                        {
                           Mage::log('Saving error with the following message: '.$e->getMessage());
                           Mage::log('Saving product:');
                           Mage::log($product->getData);
                           Mage::log('CSV line:');
                           Mage::log($data);
                        }
                    }
                    else
                    {
                        Mage::log('Import error, sku not found: '.$data[1]);
                        Mage::log('CSV line:');
                        Mage::log($data);
                    }
                }
            }
            fclose($handle);
        }
        else
        {
            Mage::log('Import, no csv file found!')
        }
    }
}

这里是我的系统 - >配置 - >系统的cron作业配置:

Generate Schedules Every 1
Schedule Ahead for 5
Missed if Not Run Within 20
History Cleanup Every 30
Success History Lifetime 60
Failure History Lifetime 600

1 回答

  • 2

    像这样运行cron,所以你得到一个错误日志来读取:

    * * * * * /usr/bin/php /home/magento/public_html/cron.php >> /home/magento/public_html/var/log/cron.log 2>&1
    

    同时弹出此扩展程序:http://connect20.magentocommerce.com/community/Aoe_Scheduler它将允许您随时运行cron作业,清除计划并重建它 .

    不,我不能告诉你为什么你的分机无法正常工作,但是,通过记录和控制您的分机你应该在简单的街道上 .

    你可能也想要这个:

    config.xml文件:

    <events>
      <mymodule_update_stuff>
        <observers>
          <mymodule_update_stuff_handler>
            <type>model</type>
            <class>mymodule/observer</class>
            <method>updateStuff</method>
            <args/>
          </mymodule_update_stuff_handler>
        </observers>
      </mymodule_update_stuff>
    </events>
    ....
    <crontab>
    <jobs>
      <mymodule_updatestuff>
        <schedule>
          <!--<cron_expr>*/15 * * * *</cron_expr> -->
          <config_path>mymodule/misc/frequency</config_path>
        </schedule>
        <run>
          <model>mymodule/observer::updateStuff</model>
        </run>
      </mymodule_updatestuff>
    </jobs>
    </crontab>
    

    现在在system.xml中:

    <config>
    <sections>
    <mymodule translate="label" module="mymodule">
      <label>Whatever</label>
      <tab>catalog</tab>
      <frontend_type>text</frontend_type>
      <sort_order>200</sort_order>
      <show_in_default>1</show_in_default>
      <show_in_website>1</show_in_website>
      <show_in_store>0</show_in_store>
      <groups>
        <misc translate="label">
          <label>Import Options</label>
          <frontend_type>text</frontend_type>
          <sort_order>0</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>0</show_in_store>
          <fields>
            <frequency translate="label">
              <label>Update Frequency</label>
              <frontend_type>text</frontend_type>
              <sort_order>100</sort_order>
              <show_in_default>1</show_in_default>
              <show_in_website>0</show_in_website>
              <show_in_store>0</show_in_store>
            </frequency>
    

    您现在可以将cron计划放在模块的admin配置中,而不是将其硬连接到config.xml中 . 这样可以更容易地开发cron样式的模块 .

相关问题