在 Magento 中设置 CRON 作业

发布于 2024-10-31 18:47:39 字数 2217 浏览 0 评论 0原文

有很多关于设置 cron 的教程,我认为我已经正确完成了,但由于某种原因它不起作用。我还创建了一个控制器类来测试模型并且它工作正常。

这是我的 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>

我的 Observer.php 文件是:

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

我的测试 IndexController.php 文件是:

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

如果我将浏览器指向 http://my_server/vpsexport/index 我会看到正确的输出echo 语句和消息被打印到日志中,所以我知道模型已正确配置。然而,cron 并没有得到相同的结果。如果我手动运行 cron.php,我不会收到任何错误,但它似乎仍然没有执行任何操作。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱的十字路口 2024-11-07 18:47:39

据我了解magento的cron系统,它分两个阶段工作:

  1. 它检查模块config.xml以找出cron必须运行的方法并将它们插入数据库(cron_schedule
  2. 它从此表中获取信息并运行与实际时间匹配的 scheduled_at 列的方法。

正如您所说,cron_schedule 表中有条目,第一阶段没问题
因此,为了检查您的 cron 是否正常执行,您实际上必须在服务器中设置一个 cronjob,如果您在本地工作,则该服务器将是您的计算机。
实际上在ubuntu上非常简单,我猜在windows上会更复杂,但是(再次猜测)并非不可能。
或者也许每 5 分钟刷新一次指向 cron.php 文件的浏览器(因为您在 config.xml 中配置了它)就可以解决问题,但设置 cronjob 会更有效:)
希望有帮助

编辑: 这是我的 cronjob 的样子,如果它对您有帮助的话:

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

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
淡淡離愁欲言轉身 2024-11-07 18:47:39

运行 cron 脚本的性能和影响可能存在根本差异,具体取决于它是本机调用还是通过 Web 服务器调用。

一个重要的区别是当您使用 PHP 的 APC 操作码缓存器时(这是一个非常好的主意)。根据文档,从命令行访问 PHP 将清除缓存,这可能不是您真正希望每 5 分钟发生一次的事情!

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!

A君 2024-11-07 18:47:39

我知道这已经是很久以后的事了,但我应该扩展一下。我有点说错了,因为从启用 APC 的命令行运行 PHP 使用它自己的私有缓存。如果您通过 Web 服务器运行 cron 作业,则现有缓存可用并添加到其中。

这将存储每次运行的相关内容。在命令行作业中使用 APC 可能会成为一个障碍,因为您可能要做的就是加载缓存,然后删除它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文