php 如何使用 Magento 测试 cron?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5290868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:53:10  来源:igfitidea点击:

How would test cron with Magento?

phptestingmagentocron

提问by éricP

First I need to understand how Magento cron works.

首先,我需要了解 Magento cron 的工作原理。

I know how cron works on linux, using crontab -e.
I know I need to set up Magento's cron.php to run periodically

我知道 cron 如何在 linux 上工作,使用crontab -e.
我知道我需要设置 Magento 的 cron.php 来定期运行

But when I define cron within magento config file, how does they match that they should be run?

但是当我在 magento 配置文件中定义 cron 时,它们如何匹配它们应该运行?

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.

因为如果将我的 cron.php 设置为每 15 分钟 ( 0,15,30,45 * * * *)运行一次<schedule><cron_expr>10 * * * *</cron_expr></schedule>,例如我有一个 Magento cron 。

How will it match/work?

它将如何匹配/工作?

By the way, how could I test that my cron works without waiting a long time?

顺便说一句,我怎样才能在不等待很长时间的情况下测试我的 cron 是否有效?

回答by Nick

Magento's cron entry point is the cron.phpscript in your Magento root. You'll need to setup an OS crontab entry hit this script, which you appear to have done already.

Magento 的 cron 入口点是cron.phpMagento 根目录中的脚本。您需要设置一个操作系统 crontab 条目,点击此脚本,您似乎已经完成了。

How the cron works

cron 的工作原理

Every time the cron.phpscript is hit three things happen:

每次cron.php脚本被点击时,都会发生三件事:

  • Schedule: Magento parses the merged config.xmlfiles for <config><crontab>...</crontab></config>jobsentries, reading their cron_exprelements 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).
  • Schedule:Magento 解析合并config.xml文件中的<config><crontab>...</crontab></config>jobs条目,读取它们的cron_expr元素以了解它们应该多久运行一次的详细信息。然后,Magento 使用应该在未来执行的作业以及它们应该何时运行的时间戳来填充 cron 调度表。Magento 执行此操作的未来范围可在管理员中配置。
  • 执行:Magento 读取 cron 调度表,以获取需要在这一秒执行的作业和应该已经执行的作业,即过去的时间戳,尚未过期。到期限制也是管理员中可配置的参数。
  • 清理:Magento 通过调度表删除已完成或错过的作业(由于截止日期)。

Duplicate runs?

重复运行?

You raise an interesting point. What happens if you have a 10 minute cron scheduled in Magento, but cron.phpis only guaranteed to be hit every 15 minutes. It appears that Magento would run your Magento jobs twice, in some cases:

你提出了一个有趣的观点。如果您在 Magento 中安排了 10 分钟的 cron,但cron.php只能保证每 15 分钟命中一次,会发生什么。在某些情况下,Magento 似乎会运行您的 Magento 作业两次:

  • HH:00-> HH:50 and HH:00
  • HH:15-> HH:10
  • HH:30-> HH:20 and HH:30
  • HH:45-> HH:40
  • HH:00-> HH:50 和 HH:00
  • 高:15-> 高:10
  • HH:30-> HH:20 和 HH:30
  • 高:45-> 高:40

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.phpexecution frequency to every 5 minutes, which is what we do.

我只阅读了代码,这就是发生的事情。当然,最好的方法是运行它并找出答案。如果你想避免这个问题,在大多数情况下,然后将cron.php执行频率提高到每 5 分钟,这就是我们所做的。

Testing your code

测试你的代码

If you want to test if your cron is actually working without waiting an age, set the cron_exprto execute every minute, or clear out your cron schedule table then hit cron.phptwice (once to generate the schedule, and again to run the job).

如果您想测试您的 cron 是否真的在工作而无需等待一段时间,请将 设置cron_expr为每分钟执行一次,或者清除您的 cron 计划表然后点击cron.php两次(一次生成计划,然后再次运行作业)。

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.

如果您只想测试您的模型是否正常工作,您可以设置一个测试脚本(在https://www.nicksays.co.uk/testing-magento-modules 中概述),然后运行它。

Your question prompted a blog post: https://www.nicksays.co.uk/dissecting-the-magento-cron-system:)

您的问题提示了一篇博文:https: //www.nicksays.co.uk/dissecting-the-magento-cron-system:)

回答by Max Hodges

adding these to the bottom of cron.php will make it write out cron.php.log on execution with the datetime

将这些添加到 cron.php 的底部将使其在执行时用日期时间写出 cron.php.log

try {
    Mage::getConfig()->init()->loadEventObservers('crontab');
    Mage::app()->addEventArea('crontab');
    Mage::dispatchEvent('default');

$log = fopen(__FILE__.'.log', 'a');
fwrite($log, date("Y-m-d H:i:s").PHP_EOL);
fclose($log);
} catch (Exception $e) {
    Mage::printException($e);
}

回答by RThomas

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.

这是我做的事情。我在 cron.php 文件的末尾添加了以下代码。每次调用 cron.php 文件时,都会更新一个名为“cronlog.txt”的简单日志。

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 运行有疑问,我可以只看一眼那个文件,看看 cron.php 文件运行的最后日期和时间。

try {
   $myFile = "cronlog.txt";
   $fh = fopen($myFile, 'w');
   $stringData = date('l jS \of F Y h:i:s A');
   fwrite($fh, $stringData);
   fclose($fh);
} catch (Exception $e) {
   Mage::printException($e);
}

回答by RThomas

If you're on GNU/Linux you can check /var/log/syslogand filter out the cronjobs by using grep. To see a list of all cronjobs that were run, type in grep 'cron' /var/log/syslog.

如果您使用的是 GNU/Linux,您可以/var/log/syslog使用grep. 要查看已运行的所有 cronjob 的列表,请输入grep 'cron' /var/log/syslog.

回答by rbncha

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.

要测试 cron 是否工作,您可以简单地Mage::log('cron working', null, 'cron.log');在 cron.php 文件的末尾编写。

If the cron is working, it will create a cron.log file in your base dir.

如果 cron 正在运行,它将在您的基本目录中创建一个 cron.log 文件。