如何使用cron工作在zendframework 2? [英] How to work with cron job in zendframework 2?

查看:133
本文介绍了如何使用cron工作在zendframework 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在zend框架2中有两个函数,它们从另一个网站获取公寓的详细信息,并将它们保存在我的数据库中。 (但是,当我运行功能时,它们只是这样)。

I have two functions in zend framework 2, which fetch me the details of apartments from another site and save them in my data base. (BUT THEY DO SO ONLY WHEN I RUN THE FUNCTIONS).

说控制器名称是'FetchFromSourceController',函数是'index'和'ical'。这些是我使用的名称。

say the controller name is 'FetchFromSourceController' and the functions are 'index' and 'ical'. those are the names I used.

但是我的要求是这两个功能应该在一天开始时自动一个接一个地运行,间隙为10分钟。即索引操作应在12:00 AM运行,并且ical Action应每天在12:10 AM运行。

But my requirement is those two functions should run automatically one after the other when the day begins, with a gap of 10 minutes. i.e. index action should run at 12:00 AM and ical Action should run at 12:10 AM everyday.

我听说我可以使用'cron job'来达到这个目的,但从来没有使用过。请告诉我在ZF2中编写cron工作代码在哪些特定时间运行这些函数。

I heard that I could use 'cron job' for that purpose, but never used that before. Please tell me where to write the cron job code in ZF2 to run those functions in those particular times.

或至少给我一些在zendframework2中使用cron工作的教程。我搜索it.but找不到任何。提前感谢。

or atleast give me some tutorial for using cron job in zendframework2. I searched for it.but not find any. Thanks in advance.

推荐答案

ZF2没有cron job服务,

Cron作业是在特定时间执行的任务,通常在操作系统级别设置。在unix中,它是通过crontab完成的,在windows中可以使用任务调度程序。 ZF2中没有什么可以在12:00和12:10运行你的脚本。

A Cron Job is a task performed at a certain time and is typically setup at the operating system level. In unix, it's accomplished via the crontab, in windows you can use the task scheduler. There is nothing in ZF2 which will run your scripts at exactly 12:00 and 12:10.

选项#1:Cron Job通过管理面板

对于大多数托管环境,您可以通过网站托管管理面板(如Plesk或cPanel)设置cron作业。在那里,您可以通过输入时间和要运行的命令来计划作业。当您以这种方式创建cron作业时,请记住,您将输入要通过命令行运行的命令。通常这是直接调用php来执行一个特定的脚本,但你也可以使用 wget 来获取任何来自互联网的URL。如果你直接调用php,你需要根据Zdenek的回答设置控制台路由。

For most hosting environments, you can setup a "cron job" via your website hosting admin panel (such as Plesk or cPanel). There you'll be able to schedule a job by entering in the time and the command to be run. When you create a cron job this way, keep in mind that you'll be entering a command to be run via the command line. Usually this is directly calling php to execute a specific script, but you could also use wget to fetch any URL from the internet. If you directly call php, you'll need to setup a console route as per Zdenek's answer.

/usr/bin/php /path/to/zf2/public/index.php myIndexConsoleRoute

wget http://yoursite.com/fetch-from-source/index

选项#2:通过命令行设置Crontab

访问您的服务器和正确的用户权限,您可以根据Zdenek的答案通过命令行设置一个crontab。还有相关信息此处。这通常是首选方法,但在我看来,它有点更多的参与,可能是一个挑战设置和调试,特别是如果你没有体验过Unix。

Alternatively, if you have command line access to your server and the proper user permissions, you can setup a crontab via the command line per Zdenek's answer. There is also relevant info here. This is typically the preferred method, but in my opinion it's a bit more involved and can be a challenge to setup and debug, especially if you're not experienced with Unix.

选项#3:Cron作业服务
如果您正在寻找一个更简单的解决方案,您可以考虑其中一个cron服务,将在给定时间加载特定的URL。

Option #3: Cron Job service If you're looking for a simpler solution, you might consider one of the cron services out there which will load a specific URL at a given time.

例如,我以前使用过此服务:
https://www.setcronjob.com

For example, I've used this service in the past: https://www.setcronjob.com

创建帐户后,只需输入网址并选择您想要的时间要调用的URL。通过设置两个作业,一个在12:00运行,另一个在12:10运行,您可以轻松地完成您的要求在12:00调用indexAction和在12:10调用icalAction。 SetCronjob有一个免费的计划,可能符合您的需要,但请注意,他们要求您输入一个验证码每30天更新您的免费帐户(这是好的,但变得恼人一段时间后)。

After you create your account, you simply enter a URL and select the time you'd like the URL to be called. You could easily accomplish your requirements to call the indexAction at 12:00 and icalAction at 12:10 by setting up two jobs, one to run at 12:00 and the other to run at 12:10. SetCronjob has a free plan which might fit your needs, but be aware that they require you to enter in a captcha every 30 days to renew your free account (which is fine, but becomes annoying after awhile).

网址对命令行

在选项#1中使用wget,在选项#3中使用Cron服务,您需要通过http使您的indexAction和icalAction可用。这可能是一个重要的考虑。即使你添加了一些验证(通过检查通过查询字符串或post发送的代码),仍然可能的是,如果他们知道正确的秘密URL,互联网上的任何人都可以触发这些功能。

With "wget" in Option #1 and a Cron Service in Option #3, you'll need to make your indexAction and icalAction available via http. This could be a significant consideration. Even if you add some sort of validation (by checking for a code you send via query string or post), it's still possible that anyone on the internet could trigger these functions if they know the correct 'secret' URL. Having your ical and index methods accessed via http has the additional benefit of you being able to easily call them if you want to manually run these scripts to force an update.

有了通过http访问的方法和索引方法,您可以轻松地调用它们。 Zdenek提到的CLI方法,您在ZF2中创建控制台路由,并且通过命令行触发该路由的唯一方式。这通常是首选,因为您的索引和ical方法不会暴露给http,但它并不总是一个选项,这取决于您的托管设置,它可能不是一个重大问题,这些功能对您的数据和公共网站。

With the CLI approach mentioned by Zdenek, you create a console route in ZF2 and the only way to trigger that route is via the command line. This is typically preferred because your index and ical methods won't be exposed to http, but it's not always an option depending on your hosting setup, and it might not be a significant concern depending on what these functions do to your data and public website.

这篇关于如何使用cron工作在zendframework 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆