CakePHP:从控制器运行shell作业 [英] CakePHP: Run shell job from controller

查看:183
本文介绍了CakePHP:从控制器运行shell作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从控制器使用dispatchShell?



我的任务是在用户注册后启动shell作业。



我正在使用CakePHP 2.0

解决方案

如果你不能减轻需要



因此,你有一个(可能)长时间运行的作业,你不想让用户等待。 / p>

由于您的用户正在执行的PHP代码发生在由Apache启动的请求期间,所执行的任何代码都会阻止该请求,直到完成为止(除非您打开Apache请求超时)。



如果上面的是你的应用程序不能接受的,那么你将需要使用Apache请求(即从命令行)触发PHP。 / p>

可用性,在这一点上,通知用户您正在后台处理数据是有意义的。来自一个消息的任何信息告诉他们,他们可以稍后再回到一个旋转进度条,通过ajax轮询您的应用程序以检测作业完成。



最简单的方法是使用cronjob它在一些时间间隔上执行PHP脚本(即CakePHP shell)(至少每分钟一次)。在这里您可以在后台执行此类任务。



但是,后台作业出现了一些问题。你怎么知道他们失败了?你怎么知道什么时候你需要重试?如果它不在cron间隔内完成如果会出现竞争条件?



正确但更复杂的设置将是使用工作/消息队列系统。它们允许您更加优雅地处理上述问题,但通常需要您在服务器上运行后台守护程序来捕获和处理任何传入的作业。



这种方式是,在您的代码(当用户注册时)将作业插入队列。队列守护进程立即拾取作业(它不会在一个间隔上运行,所以它总是等待),并将其传递给一个工作进程(例如CakePHP shell)。它是即时的,如果你告诉它,它知道它是否工作,它知道它是否失败,它可以重试,如果你想,它不会意外处理相同的工作两次。



其中有一些可用,例如 Beanstalkd dropr Gearman RabbitMQ 等。还有一些CakePHP插件(不同年龄)可以帮助:





我已经使用CakePHP与Beanstalkd Pheanstalk 库)和CakePHP队列插件(上面的第一个)。我必须信用Beanstalkd(写在C)为非常轻量级,简单和快速。但是,对于CakePHP的开发,我发现插件更快的启动和运行,因为:




Is it possible to use dispatchShell from a Controller?

My mission is to start a shell job when the user has signed up.

I'm using CakePHP 2.0

解决方案

If you can't mitigate the need to do this as dogmatic suggests then, read on.

So you have a (potentially) long-running job you want to perform and you don't want the user to wait.

As the PHP code your user is executing happens during a request that has been started by Apache, any code that is executed will stall that request until it completion (unless you hit Apache's request timeout).

If the above isn't acceptable for your application then you will need to trigger PHP outwith the Apache request (ie. from the command line).

Usability-wise, at this point it would make sense to notify your user that you are processing data in the background. Anything from a message telling them they can check back later to a spinning progress bar that polls your application over ajax to detect job completion.

The simplest approach is to have a cronjob that executes a PHP script (ie. CakePHP shell) on some interval (at minimum, this is once per minute). Here you can perform such tasks in the background.

Some issues arise with background jobs however. How do you know when they failed? How do you know when you need to retry? What if it doesn't complete within the cron interval.. will a race-condition occur?

The proper, but more complicated setup, would be to use a work/message queue system. They allow you to handle the above issues more gracefully, but generally require you to run a background daemon on a server to catch and handle any incoming jobs.

The way this works is, in your code (when a user registers) you insert a job into the queue. The queue daemon picks up the job instantly (it doesn't run on an interval so it's always waiting) and hands it to a worker process (a CakePHP shell for example). It's instant and - if you tell it - it knows if it worked, it knows if it failed, it can retry if you want and it doesn't accidentally handle the same job twice.

There are a number of these available, such as Beanstalkd, dropr, Gearman, RabbitMQ, etc. There are also a number of CakePHP plugins (of varying age) that can help:

I have had experience using CakePHP with both Beanstalkd (+ the PHP Pheanstalk library) and the CakePHP Queue plugin (first one above). I have to credit Beanstalkd (written in C) for being very lightweight, simple and fast. However, with regards to CakePHP development, I found the plugin faster to get up and running because:

  • The plugin comes with all the PHP code you need to get started. With Beanstalkd, you need to write more code (such as a PHP daemon that polls the queue looking for jobs)
  • The Beanstalkd server infrastructure becomes more complex. I had to install multiple instances of beanstalkd for dev/test/prod, and install supervisord to look after the processes).
  • Developing/testing is a bit easier since it's a self-contained CakePHP + MySQL solution. You simply need to type cake queue add user signup and cake queue runworker.

这篇关于CakePHP:从控制器运行shell作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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