简体   繁体   English

限制使用PHP进行cron作业

[英]Limit to cron job with PHP

I have a Cron Job with PHP which I want to set up on my webhost, but at the moment the script takes about 20 seconds to run with only 3 users data being refreshed. 我有一个使用PHP的Cron Job,我想在我的网络主机上进行设置,但是此脚本大约需要20秒才能运行,只有3个用户的数据被刷新。 If I get a 1000 users - gonna take ages. 如果我有1000个用户-将会花费很多时间。 Is there an alternative to Cron Job? 有Cron Job的替代品吗? Will my web host let me run a cron job which takes, for example, 10 minutes to run? 我的网络托管服务商是否可以让我运行一项cron作业,例如,该作业需要10分钟才能运行?

A cron job shouldn't have any bearing on how long it's 'job' takes to complete. Cron作业对完成“作业”需要多长时间没有任何影响。 If you're jobs are taking 20 seconds to complete, it's PHP's fault, not cronjob. 如果您的工作需要20秒才能完成,那是PHP的错,而不是cronjob。

Will my web host let me run a cron job which takes, for example, 10 minutes to run? 我的网络托管服务商是否可以让我运行一项cron作业,例如,该作业需要10分钟才能运行?

Ask your webhost. 询问您的虚拟主机。

If you want to learn about optimizing php scripts, take a look at Profiling PHP Code . 如果您想了解有关优化php脚本的信息,请查看Profiling PHP Code

Your cron job can be as long as you want. 您的Cron工作可以随心所欲。

The main problem for you is that you must ensure the next cron job execution is not occuring while the first one is still running . 您的主要问题是,必须确保在第一个cron作业仍在运行时不执行下一个cron作业 You have a lot of solutions to avoid it, basically use a semaphore. 您有很多避免它的解决方案,基本上使用信号量。

It can be a lock file, a record in database. 它可以是锁定文件,也可以是数据库中的记录。 Your cron job should check if the previous one is finished or not. 您的Cron作业应检查上一个作业是否完成。 A good thing is maybe sending you an email if he cannot run because of a long previous job (this way you'll have some notice alerting you that something is maybe getting wrong) By default cron jobs with bad error dstatus on exit are outputing all the standard output to the email of the account running the job, depending on how is configured the platform you could use this behavior or build an smtp connexion on the job (or store the alert in a database table). 一件好事是,如果他由于长期从事的工作而无法运行,可能会向您发送电子邮件(这样,您将收到一些通知,警告您可能出了一些问题)默认情况下,退出时出现错误dstatus的cron作业会输出所有内容运行作业帐户的电子邮件的标准输出,具体取决于如何配置平台才能使用此行为或在作业上构建smtp连接(或将警报存储在数据库表中)。

If you want some alternatives to cron jobs you should have a look at work queues . 如果您想要替代cron作业的替代方法,则应查看工作队列 You can mix work queues with a cron job, or use work queue in apache-php envirronment, lot of solutions, but the main idea is to make on single queue of things that should be done, and execute them one after the other (but be careful, if you handle theses tasks very slowly you'll get a big fat waiting queue). 您可以将工作队列与cron作业混合使用,也可以在apache-php环境中使用工作队列(有很多解决方案),但主要思想是将应完成的工作放在一个队列中,然后一个接一个地执行(但是请注意,如果您处理这些任务的速度很慢,则会排队等候大量胖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM