php 使用 Zend Framework 创建 cronjob
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/143320/
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
Create cronjob with Zend Framework
提问by Thomaschaaf
I am trying to write a cronjob controller, so I can call one website and have all modules cronjob.php executed. Now my problem is how do I do that?
我正在尝试编写一个 cronjob 控制器,因此我可以调用一个网站并执行所有模块 cronjob.php。现在我的问题是我该怎么做?
Would curl be an option, so I also can count the errors and successes?
curl 是一种选择,所以我也可以计算错误和成功吗?
[Update]
[更新]
I guess I have not explained it enough.
我想我解释得还不够多。
What I want to do is have one file which I can call like from http://server/cronjoband then make it execute every /application/modules/*/controller/CronjobController.php or have another way of doing it so all the cronjobs aren't at one place but at the same place the module is located. This would offer me the advantage, that if a module does not exist it does not try to run its cronjob.
我想要做的是有一个我可以从http://server/cronjob调用的文件,然后让它执行每个 /application/modules/*/controller/CronjobController.php 或者有另一种方式来做所有的cronjobs 不在一个地方,而是在模块所在的同一个地方。这将为我提供优势,即如果模块不存在,它不会尝试运行其 cronjob。
Now my question is how would you execute all the modules CronjobController or would you do it a completly different way so it still stays modular?
现在我的问题是你将如何执行所有模块 CronjobController 或者你是否会以完全不同的方式执行它以便它仍然保持模块化?
And I want to be able to giveout how many cronjobs ran successfully and how many didn't
我希望能够给出有多少 cronjobs 成功运行,有多少没有成功
回答by ispytodd
After some research and a lot procrastination I came to the simple conclusion that a ZF-ized cron script should contain all the functionality of you zend framework app - without all the view stuff. I accomplished this by creating a new cronjobfoo.php file in my application directory. Then I took the bare minimum from: -my front controller (index.php) -my bootstrap.php
经过一些研究和大量的拖延,我得出了一个简单的结论,即 ZF 化的 cron 脚本应该包含 zend 框架应用程序的所有功能 - 没有所有的视图内容。我通过在我的应用程序目录中创建一个新的 cronjobfoo.php 文件来实现这一点。然后我取了最低限度: -my front controller (index.php) -my bootstrap.php
I took out all the view stuff and focused on keeping the environment setup, db setup, autoloader, & registry setup. I had to take a little time to correct the document root variable and remove some of the OO functionality copied from my bootstrap.
我拿出了所有的视图内容,专注于保持环境设置、数据库设置、自动加载器和注册表设置。我不得不花一点时间来更正文档根变量并删除一些从引导程序复制的 OO 功能。
After that I just coded away.. in my case it was compiling and emailing out nightly reports. It was great to use Zend_Mail. When I was confident that my script was working the way I wanted, I just added it my crontab.
在那之后,我只是编码了……在我的情况下,它正在编译和通过电子邮件发送夜间报告。使用 Zend_Mail 很棒。当我确信我的脚本按照我想要的方式工作时,我只是将它添加到我的 crontab 中。
good luck!
祝你好运!
回答by gregor
For Zend Framework I am currently using the code outlined bellow. The script only includes the portal file index.php, where all the paths, environment and other Zendy code is bootstrapped. By defining a constant in the cron script we cancel the final step , where the application is run.
对于 Zend 框架,我目前正在使用下面概述的代码。该脚本仅包含门户文件 index.php,其中所有路径、环境和其他 Zendy 代码都被引导。通过在 cron 脚本中定义一个常量,我们取消了运行应用程序的最后一步。
This means the application is only setup, not even bootstrapped. At this point we start bootstraping the resources we need and that is that
这意味着应用程序只是设置,甚至没有引导。在这一点上,我们开始引导我们需要的资源,那就是
//public/index.php
if(!defined('DONT_RUN_APP') || DONT_RUN_APP == false) {
$application->bootstrap()->run();
}
// application/../cron/cronjob.php
define("DONT_RUN_APP",true);
require(realpath('/srv/www/project/public/index.php'));
$application->bootstrap('config');
$application->bootstrap('db');
//cron code follows
回答by dragonmantank
I would caution putting your cronjobs accessible to the public because they could be triggered outside their normal times and, depending on what they do, cause problems (I know that is not what you intend, but by putting them into an actual controller it becomes reachable from the browser). For example, I have one cron that sends e-mails. I would be spammed constantly if someone found the cron URL and just began hitting it.
我会谨慎地将您的 cronjobs 向公众开放,因为它们可能会在正常时间之外触发,并且根据它们的行为,会导致问题(我知道这不是您想要的,但是通过将它们放入实际控制器中,它变得可以访问从浏览器)。例如,我有一个发送电子邮件的 cron。如果有人找到 cron URL 并开始点击它,我会不断收到垃圾邮件。
What I did was make a cron folder and in there created a heartbeat.php which bootstraps Zend Framework (minus MVC) for me. It checks a database which has a list of all the installed cron jobs and, if it is time for them to run, generates an instances of the cron job's class and runs it.
我所做的是创建一个 cron 文件夹,并在其中创建了一个 heartbeat.php,它为我引导 Zend 框架(减去 MVC)。它检查一个数据库,该数据库包含所有已安装的 cron 作业的列表,如果需要运行它们,则生成 cron 作业类的实例并运行它。
The cron jobs are just child classes from an abstract cron class that has methods like install(), run(), deactivate(), etc.
cron 作业只是抽象 cron 类的子类,该类具有 install()、run()、deactivate() 等方法。
To fire off my job I just have a simple crontab entry that runs every 5 minutes that hits heartbeat.php. So far it's worked wonderful on two different sites.
为了解雇我的工作,我只有一个简单的 crontab 条目,它每 5 分钟运行一次,点击 heartbeat.php。到目前为止,它在两个不同的站点上运行良好。
回答by Till
Someone mentioned this blog entrya couple days ago on fw-general (a mailinglistwhich I recommend reading when you use the Zend Framework).
几天前有人在 fw-general(一个邮件列表,我建议您在使用 Zend 框架时阅读它)上提到了这个博客条目。
There is also a proposal for Zend_Controller_Request_Cli, which should address this sooner or later.
还有一个关于Zend_Controller_Request_Cli的提案,它迟早会解决这个问题。
回答by Gerald Ekosso
I have access to a dedicated server and I initially had a different bootstrap for the cron jobs. I eventually hated the idea, just wishing I could do this within the existing MVC setup and not have to bother about moving things around.
我可以访问专用服务器,并且我最初为 cron 作业使用了不同的引导程序。我最终讨厌这个想法,只是希望我可以在现有的 MVC 设置中做到这一点,而不必费心四处移动。
I created a file cron.sh, saved is within my site root (not public) and in it I put a series of commands I would like to run. As I wanted to run many commands at once I wrote the PHP within my controllers as usual and added curl calls to those urls within cron.sh. for example curl http://www.mysite.com/cron_controller/actionThen on the cron interface I ran bash /path/to/cron.sh.
我创建了一个文件 cron.sh,保存在我的站点根目录(非公开)中,并在其中放置了一系列我想要运行的命令。因为我想一次运行许多命令,所以我像往常一样在控制器中编写了 PHP,并向 cron.sh 中的这些 url 添加了 curl 调用。例如curl http://www.mysite.com/cron_controller/action然后在我运行的 cron 界面上bash /path/to/cron.sh。
As pointed out by others your crons can be fired by anyone who guesses the url so there's always that caveat. You can find a solution to that in many different ways.
正如其他人所指出的那样,任何猜到 url 的人都可以触发您的 cron,因此总是有警告。您可以通过许多不同的方式找到解决方案。
回答by Gerald Ekosso
Why not just create a crontab.php, including, or requiring the index.php bootstrap file?
为什么不创建一个 crontab.php,包括或需要 index.php 引导文件?
Considering that the bootstrap is executing Zend_Loader::registerAutoload(), you can start working directly with the modules, for instance, myModules_MyClass::doSomething();
考虑到引导程序正在执行Zend_Loader::registerAutoload(),您可以直接开始使用模块,例如,myModules_MyClass::doSomething();
That way you are skipping the controllers. The Controller job is to control the access via http. In this case, you don't need the controller approach because you are accessing locally.
这样你就跳过了控制器。Controller 的工作是通过 http 控制访问。在这种情况下,您不需要控制器方法,因为您正在本地访问。
回答by takeshin
Take a look at zf-cli:
看看zf-cli:
This handles well all cron jobs.
这可以很好地处理所有 cron 作业。
回答by lony
回答by A.P.
My solution:
我的解决方案:
- curl /cron
- Global cron method will include_once all controllers
- Check whether each of the controllors has ->cron method
- If they have, run those.
- 卷曲/cron
- 全局 cron 方法将包含_once 所有控制器
- 检查每个控制器是否有 ->cron 方法
- 如果有,运行它们。
Public cron url (for curl) is not a problem, there are many ways to avoid abuse. As said, checking remote IP is the easiest.
public cron url (for curl) 不是问题,有很多方法可以避免滥用。如前所述,检查远程 IP 是最简单的。
回答by Stefan Gehrig
Do you have filesystem access to the modules' directories? You could iterate over the directories and determine where a CronjobController.php is available. Then you could either use Zend_Http_Clientto access the controller via HTTP or use an approach like Zend_Test_PHPUnit: simulate the actual dispatch process locally.
您是否具有对模块目录的文件系统访问权限?您可以遍历目录并确定 CronjobController.php 可用的位置。然后您可以使用Zend_Http_Client通过 HTTP 访问控制器或使用类似的方法Zend_Test_PHPUnit:在本地模拟实际的调度过程。

