我是个新手。
我正在用Codeigniter写一个社交应用。一旦用户登录,许多事情要做,准备用户网络,发送电子邮件,创建一个推荐的朋友列表,等等。我们已经写过函数了。
让我们假设有一个主控制器索引函数SignUp。
Sign up后,我们想将用户重定向到Dashboard控制器但在后台,我们想运行
- BuildNetwork
- FindMatches
- 发送电子邮件
我不知道如何在后台运行任务。所以我想也许我可以写一个shell脚本,一个接一个地调用这些函数。我们将通过Codeigniter调用该脚本并传递UserId。
然后,该脚本将调用各个函数并将userId传递给这些函数。
谁能告诉我
- 如果这是正确的方法
- 如何完成(以这种方式或以任何其他方式)
我建议您使用队列系统,签出像beanstalk这样的东西(以及PHP库pheanstalk)。
你可以为你想做的每件事在队列中放置一个作业,然后在你的后台进程(可能是cron)中,你可以获取作业并运行它们。
http://kr.github.com/beanstalkd/https://github.com/pda/pheanstalk/
因此,在您的SignUp函数中,您将创建一个作业并放入队列,您可以为每种不同类型的作业使用一个队列,然后为每个队列创建一个作业消费者。
// some pseudo code
function signUp()
{
$jobData = json_encode(array(
'template' => 'newUser',
'to' => 'john@example.com'
));
$this->pheanstalk->useTube('OutboundEmails')->put($jobData);
// add other jobs for other tasks here too
}
那么你可以有一个cron脚本来运行你的消费者脚本。
// pseudo for this mail consumer
$pheanstalk->watch('OutboundEmails')->ignore('default');
while ($job = $pheanstalk->reserve())
{
// send the email using the data in the job
$job->getData() // returns your JSON
}
希望对你有帮助。