如何使用 Magento 测试 cron?
首先我需要了解 Magento cron 是如何工作的。
我知道 cron 在 Linux 上如何工作,使用 crontab -e
。
我知道我需要设置 Magento 的 cron.php 定期运行
但是当我在 magento 配置文件中定义 cron 时,它们如何匹配它们应该运行?
因为如果将我的 cron.php 设置为每 15 分钟运行一次 (0,15,30,45 * * * *
) 并且我有一个 Magento cron
例如。
它将如何匹配/工作?
顺便问一下,我如何测试我的 cron 是否正常工作而无需等待很长时间?
First I need to understand how Magento cron works.
I know how cron works on linux, using crontab -e
.
I know I need to set up Magento's cron.php to run periodically
But when I define cron within magento config file, how does they match that they should be run?
Because if set my cron.php to run every 15 min (0,15,30,45 * * * *
) and I have a Magento cron <schedule><cron_expr>10 * * * *</cron_expr></schedule>
for example.
How will it match/work?
By the way, how could I test that my cron works without waiting a long time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Magento 的 cron 入口点是 Magento 根目录中的 cron.php 脚本。您需要设置一个操作系统 crontab 条目来命中此脚本,您似乎已经完成了。
cron 的工作原理
每次执行
cron.php
脚本时,都会发生三件事:config.xml...
文件,读取其jobs
条目的cron_expr
元素以了解有关其运行频率的详细信息。然后,Magento 用将来应该执行的作业以及应该运行它们的时间戳来填充 cron 调度表。 Magento 将来执行此操作的程度可以在管理员中进行配置。重复运行?
您提出了一个有趣的观点。如果您在 Magento 中安排了 10 分钟的 cron,但仅保证每 15 分钟执行一次 cron.php,会发生什么情况。在某些情况下,Magento 似乎会运行您的 Magento 作业两次:
我只读过代码,看起来就是这样。当然,找出答案的最佳方法是运行它并找出答案。如果您想避免此问题,在大多数情况下,请将 cron.php 执行频率增加到每 5 分钟一次,这就是我们所做的。
测试你的代码
如果你想测试你的 cron 是否真的在运行而无需等待一段时间,请将
cron_expr
设置为每分钟执行一次,或者清除你的 cron 计划表,然后点击cron.php
两次(一次生成计划,另一次运行作业)。如果您只想测试您的模型是否正常工作,您可以设置一个测试脚本(在 https://www.nicksays.co.uk/testing-magento-modules),然后运行它。
您的问题引发了一篇博客文章:https://www.nicksays.co .uk/dissecting-the-magento-cron-system :)
Magento's cron entry point is the
cron.php
script in your Magento root. You'll need to setup an OS crontab entry hit this script, which you appear to have done already.How the cron works
Every time the
cron.php
script is hit three things happen:config.xml
files for<config><crontab>...</crontab></config>
jobs
entries, reading theircron_expr
elements for detail on how often they should be run. Magento then populates the cron schedule table with jobs that should be executed in the future, along with timestamps for when they should be run. The extent into the future that Magento does this is configurable in the admin.Duplicate runs?
You raise an interesting point. What happens if you have a 10 minute cron scheduled in Magento, but
cron.php
is only guaranteed to be hit every 15 minutes. It appears that Magento would run your Magento jobs twice, in some cases:I've only read the code, and that's what appears to happen. The best way to find out would be, of course, to run it and find out. If you want to avoid the issue, in most cases, then increase the
cron.php
execution frequency to every 5 minutes, which is what we do.Testing your code
If you want to test if your cron is actually working without waiting an age, set the
cron_expr
to execute every minute, or clear out your cron schedule table then hitcron.php
twice (once to generate the schedule, and again to run the job).If you want to just test that your model is working, you could setup a test script (outlined in https://www.nicksays.co.uk/testing-magento-modules), and run that.
Your question prompted a blog post: https://www.nicksays.co.uk/dissecting-the-magento-cron-system :)
关于你问题的后半部分......如何判断它是否正在运行。
这是我所做的事情。我在 cron.php 文件末尾添加了以下代码。每次调用 cron.php 文件时,都会更新一个名为“cronlog.txt”的简单日志。
因此,如果我对 cron 运行有疑问,我只需浏览该文件即可查看 cron.php 文件运行的最后日期和时间。
On the second half of your question... how to tell if its running.
Here is something that I did. I added the following code at the end of the cron.php file. This updates a simple log called "cronlog.txt" everytime the cron.php file gets called.
So, if I have doubts about cron running I can just glance at that file and see the last date and time the cron.php file ran.
将这些添加到 cron.php 的底部将使其在执行时使用日期时间写出 cron.php.log
adding these to the bottom of cron.php will make it write out cron.php.log on execution with the datetime
如果您使用的是 GNU/Linux,则可以检查
/var/log/syslog
并使用grep
过滤掉 cronjobs。要查看已运行的所有 cronjobs 的列表,请输入grep 'cron' /var/log/syslog
。If you're on GNU/Linux you can check
/var/log/syslog
and filter out the cronjobs by usinggrep
. To see a list of all cronjobs that were run, type ingrep 'cron' /var/log/syslog
.要测试 cron 是否正常工作,您只需在 cron.php 文件末尾编写
Mage::log('cronworking', null, 'cron.log');
即可。如果 cron 正在工作,它将在您的基本目录中创建一个 cron.log 文件。
To test cron working or not you can simply write
Mage::log('cron working', null, 'cron.log');
at the end of the cron.php file.If the cron is working, it will create a cron.log file in your base dir.