cron 作业或 PHP 调度程序
我使用 MYSQL 作为我的数据库,使用 PHP 作为我的编程语言.我想运行一个 cron 作业,该作业将一直运行到当前系统日期与我的名为PROJECT"的数据库表中的截止日期(日期)"列匹配为止.一旦日期相同,就必须运行更新查询,这会将状态(项目表的字段)从打开"更改为关闭".
I am using MYSQL as my database and PHP as my programming language.I wanted to run a cron job which would run until the current system date matches the "deadline(date)" column in my database table called "PROJECT".Once the dates are same an update query has to run which would change the status(field of project table) from "open" to "close".
我不确定 cron 作业是否是最好的方法,或者我可以使用触发器或其他方法.此外,我使用 Apache 作为我的网络服务器,我的操作系统是 windows vista.
I am not really sure if cron jobs are the best way or I could use triggers or may be something else.Also I am using Apache as my web server and my OS is windows vista.
还有最好的方法是什么?PHP 调度程序或 cron 作业或任何其他方法?有没有人能指教一下?
Also which is the best way to do it? PHP scheduler or cron jobs or any other method? can anybody enlighten me?
推荐答案
我认为您的概念需要改变.
I think your concept needs to change.
PHP 不能调度作业,MySQL 也不能.MySQL 中的触发器在 mysql 查询发生时执行,而不是在特定时间执行.都没有
PHP cannot schedule a job, neither can MySQL. Triggers in MySQL execute when a mysql query occurs, not at a specific time. Neither
这个限制在 Web 开发中通常不是问题.原因是因为您的 PHP 应用程序应该控制所有进出的数据.通常,这仅意味着向用户或其他程序显示该数据的 HTML 或其他格式.
This limitation usually isn't a problem in web development. The reason is because your PHP application should control all data going in and out. Usually, this means just the HTML that displays that data, or other formats to users, or other programs.
在你的情况下,你可以这样考虑.截止日期是一个固定的日期.您可以将其视为数据,并将其保存到您的数据库中.截止日期何时发生并不重要,重要的是您在数据库中发送的数据是否被正确查看.
In your case you can think about it this way. The deadline is a set date. You can treat it as data, and save it to your database. When the deadline occurs is not important, it is that the data you have sent in your database is viewed correctly.
当对您的应用程序提出请求时,检查截止日期是否已过去,如果是,则显示该项目已关闭 - 或在显示之前更新该项目已关闭.
When a request is made to your application, check if the date of the deadline is in the past, if it is, then display that the project is closed - or update that the project is closed, just before display.
确实没有理由独立于您的 PHP 应用程序更新数据.
There really is no reason to update data independantly of your PHP application.
通常,您想要安排的唯一事情是会在负载方面影响您的应用程序的作业,或者只需要完成一次的作业,或者并发性或时间有问题的作业.
Usually, the only things you want to schedule are jobs that would affect your application in terms of load, or that need to be done only once, or where concurrency or time is an issue.
就您而言,这些都不适用.
In your case none of those apply.
PS:我还没有尝试过 PHPscheduler,但我猜它不是一个真正的调度器.Cron 是一个守护进程,它会休眠直到给定任务在其队列中到期,然后执行该任务,然后休眠直到下一个到期(至少在当前算法中它是这样做的).PHP 不能在没有套接字和 fork 扩展的情况下做到这一点,作为特殊设置.因此,PHPscheduler 很可能只是在每次加载网页时(每当 PHP 执行页面时)检查任务的日期是否已过期.这与您只是检查项目日期是否已过期没有什么不同,没有 PHPScheduler 的开销.
PS: I haven't tried PHPscheduler but I can guess it isn't a true scheduler. Cron is a deamon that sleeps until a given task is due in its queue, executes the task, then sleeps till the next one is due (at least thats what it does in the current algorithm). PHP cannot do that without the sockets and fork extensions, as special setup. So PHPscheduler is most likely just checking if a date for a task has expired, on each load of a webpage (whenever PHP executes a page). This is no different then you just checking if the date on the project has expired, without the overhead of PHPScheduler.
相关文章