PHP的需要用户注册...(或叉处理)回到现场处理一个cron [英] PHP- Need a cron for back site processing on user signup... (or fork process)

查看:172
本文介绍了PHP的需要用户注册...(或叉处理)回到现场处理一个cron的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当一个新用户注册在我的网站,我想做一些pre-处理以缩短他们对未来的搜索。这涉及从30到2分钟的处理时间的任何地方。很显然,我不能做到这一点,当他们点击注册提交按钮......或者他们访问的任何PHP页面上。不过,我想5分钟其中内完成这件事注册(或更少)。

Whenever a new user signs up on my site, I want to do some pre-processing to shorten their searches in the future. This involves anywhere from 30 to 2 minutes processing time. Obviously I cannot do this when they click the submit button on signup... or on any PHP page they visit. However, I would like this done within 5 minutes of them signing up (or less).

的Cron路线
我想这需要在cron作业,如果是这样,我应该如何设置cron作业?如果是这样,我应该我的cron线看喜欢每2分钟运行,我怎么能保证我没有同样的cron作业重叠下?

Cron Route I THINK this needs to be in a cron job, and if so, how should I setup the cron job? If so, what should my cron line look like to run every 2 minutes, and how can I insure that I don't have the same cron job overlapping the next?

事件/叉干线 - preferred
如果我能有可能抛出一些事件到我的服务器,而不会中断我的用户体验,或创建一个进程关闭用户注册(而不是cron作业)的我怎么能做到这一点?

Event/Fork Route - Preferred If I can possibly throw some event to my server without disrupting my users experience or fork a process off of the users signup (instead of a cron job) how could I do this?

推荐答案

我会建议没有解决方案。

I would recommend neither solution.

相反,你最好了与一个长期运行的进程(守护进程),采用的消息队列。消息队列本身可以关闭一个数据库,如果这是你的preferred方法。

Instead, you would be best off with a long running process (daemon) that gets its jobs from a message queue. The message queue itself could be off a database if that is your preferred method.

您将发布作业标识符到您的数据库,然后长时间运行的过程中会通过他们曾经在一段时间迭代,对他们采取行动。

You will post an identifier for the job to your database, and then a long running process will iterate through them once in a while and act upon them.

这是简单:

<?php
while(true) {
   jobs = getListOfJobsFromDatabase();  // get the jobs from the databbase
   foreach (jobs as job) {
      processAJob(job); // do whatever needs to be done for the job
      deleteJobFromDatabase(job); //remember to delete the job once its done!
   }
   sleep(60); // sleep for a while so it doesnt thrash your database when theres nothing to do
}
?>

和刚刚在命令行中运行该脚本。

And just run that script from the command line.

这在cron作业的好处是,你不会得到一个竞争条件。

The benefits of this over a cron job are that you wont get a race condition.

您可能还需要关闭叉作业的实际处理这么多可以并行进行,而不是按顺序处理。

You may also want to fork off the actually processing of the jobs so many can be done in parallel, rather than processing sequentially.

这篇关于PHP的需要用户注册...(或叉处理)回到现场处理一个cron的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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