Setting up CRON job in Magento

有些话、适合烂在心里 提交于 2019-12-06 13:32:14

问题


There are plenty of tutorials out there for setting up cron, and I think I've done it correctly, but for some reason it isn't working. I have also created a controller class to test the model and it's working correctly.

Here's my config.xml:

<config>
    <modules>
        <VPS_Export>
            <version>0.1.0</version>
        </VPS_Export>
    </modules>
    <global>
        <models>
            <vps_export>
                <class>VPS_Export_Model</class>
            </vps_export>
        </models>
        <helpers>
            <vps_export>
                <class>VPS_Export_Helper</class>
            </vps_export>
        </helpers>
    </global>
<frontend>
    <routers>
        <vps_export>
            <use>standard</use>
            <args>
                <module>VPS_Export</module>
                <frontName>vpsexport</frontName>
            </args>
        </vps_export>
    </routers>  
</frontend>
    <crontab>
        <jobs>
            <vps_export>
                <schedule>
                    <cron_expr>*/5 * * * *</cron_expr><!-- every 5 minutes -->
                </schedule>
                <run>
                    <model>vps_export/observer::exportProducts</model>
                </run>
            </vps_export>
        </jobs>
    </crontab>
</config>

My Observer.php file is:

<?php
class VPS_Export_Model_Observer
{
    public function exportProducts()
    {
        echo "VPS Export Products called!";
        Mage::Log("exportProducts called!");
    }
}
?>

And my test IndexController.php file is:

<?php
class VPS_Export_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo "index action called!";
        Mage::getModel('vps_export/observer')->exportProducts();
    }
}
?>

If I point my browser at http://my_server/vpsexport/index I see the proper output from the echo statements and the message gets printed to the log, so I know the model is properly configured. However, cron is not having the same results. If I run cron.php manually, I get no errors, but it still doesn't seem to do anything.

Any thoughts?


回答1:


As far as I understand magento's cron system, it works in 2 phase:

  1. it checks the modules config.xml to find out the methods which have to be run by cron and insert them into the database (cron_schedule)
  2. it get the info from this table and run the methods matching the scheduled_at column with actual time.

As you say there are entries in your cron_schedule table, the first phase is ok
So in order to check if your cron executes fine, you actually have to setup a cronjob in your server, which would be your computer if you're working locally.
It's actually very simple on ubuntu, I guess it will be more complicated on windows, but (guessing again) not imposible.
Or maybe refreshing your browser pointing to your cron.php file a few times would every 5 minutes (as you have it configured in your config.xml) would do the trick, but setting up a cronjob would be quite more eficient :)
Hope That Helps

edit: here is waht my cronjob looks like, if it helps you:

*/5 * * * * wget -q http://magento.local/cron.php



回答2:


There can be a fundamental difference to the performance and ramifications of how you run the cron script depending on whether it's called natively or through your web server.

One important defference is when you're using the APC opcode cacher for PHP ( which is a really good idea ). According to the documentation, accessing PHP from the command line will clear down the cache, which is probably not something that you really want to happen every 5 minutes!




回答3:


I know this is ages later, but I should expand a bit. I sort of misspoke, as running PHP from the command line with APC enabled uses it's own private cache. If you run the cron jobs through the web server, then it's existing cache is available, and added to.

This will store relevant stuff from run to run. Using APC with a command line job is potentially a hindrance, as all you're possibly doing is loading up the cache, then dropping it.



来源:https://stackoverflow.com/questions/5644037/setting-up-cron-job-in-magento

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!