PHP- 需要一个 cron 用于用户注册的后台处理...(或 fork 进程) [英] PHP- Need a cron for back site processing on user signup... (or fork process)

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

问题描述

每当有新用户在我的网站上注册时,我都想进行一些预处理以缩短他们将来的搜索时间.这涉及 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?

事件/分叉路线 - 首选如果我可以在不中断用户体验的情况下向我的服务器抛出一些事件或从用户注册(而不是 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.

相反,您最好使用一个长时间运行的进程(守护进程),它从 消息队列.如果这是您的首选方法,则消息队列本身可以脱离数据库.

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 用于用户注册的后台处理...(或 fork 进程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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