如何测试cron与Magento? [英] How would test cron with Magento?
问题描述
首先,我需要了解Magento cron如何工作。
我知道cron如何在linux上运行,使用 crontab -e
。
我知道我需要设置Magento的cron.php来定期运行
但是当我在magento配置文件中定义cron时,
因为如果设置我的cron.php每15分钟运行一次( 0,15,30, 45 * * * *
)和我有一个Magento cron < schedule>< cron_expr> 10 * * * *< / cron_expr>< / schedule> code>
顺便说一下,如何匹配/工作?
Magento的cron入口点是 cron.php
脚本在您的Magento根。
cron的工作原理
p>每当 cron.php
脚本命中时,会发生以下三种情况:
- 计划:Magento解析合并的
config.xml
文件config>< crontab> ...< / crontab>< / config>
jobs
cron_expr 元素,以详细说明应该运行它们的频率。 Magento然后用应该在将来执行的作业填充cron计划表,以及它们应该何时运行的时间戳。 - 执行:Magento读取cron schedule表,以查找需要执行的作业第二和应该已经执行的作业,即具有过去的时间戳,尚未过期。过期限制也是在admin中可配置的参数。
- 清理:Magento通过计划表删除已完成或错过的作业
- HH:00 - > HH :50和HH:00
- HH:15 - > HH:10
- / em> - > HH:20 and HH:30
- HH:45 - > HH:40
- Schedule: Magento parses the merged
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. - Execute: Magento reads the cron schedule table for jobs that need to be executed this very second and jobs that should have already been executed, i.e. with timestamps in the past, that haven't expired. The expiry limit is also a parameter configurable in the admin.
- Cleanup: Magento goes through the schedule table deleting jobs that have been completed, or missed (due to the deadline).
- HH:00 -> HH:50 and HH:00
- HH:15 -> HH:10
- HH:30 -> HH:20 and HH:30
- HH:45 -> HH:40
p>你提出一个有趣的点。如果你有一个10分钟cron计划在Magento会发生什么,但
cron.php
只保证每15分钟打一次。在某些情况下,Magento似乎会执行两次Magento工作: 我只读了代码,这就是发生的事情。找到的最好的方法是,当然,运行它,找出来。如果你想避免这个问题,在大多数情况下,然后增加 cron.php
执行频率每5分钟,这是我们做的。
测试您的代码
如果您想测试cron是否在等待年龄,设置 cron_expr
每分钟执行一次,或者清除cron调度表,然后按 cron.php
两次生成日程表,然后再次运行作业)。
如果只想测试模型是否正常工作,可以设置一个测试脚本href =https://www.nicksays.co.uk/testing-magento-modules =nofollow> https://www.nicksays.co.uk/testing-magento-modules ),并运行它。
您的问题提示了一个博文: https://www.nicksays.co.uk/dissecting-the-magento-cron-system :)
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?
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:
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 hit cron.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与Magento?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!