CakePHP:从控制器运行shell作业

编程入门 行业动态 更新时间:2024-10-21 09:13:37
本文介绍了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-queue (MySQL)
  • CakePHP-Queue-Plugin (MySQL)
  • CakeResque (Redis)
  • cakephp-gearman (Gearman)
  • 和其他人。
我已经使用CakePHP与Beanstalkd Pheanstalk 库)和CakePHP队列插件(上面的第一个)。我必须信用Beanstalkd(写在C)为非常轻量级,简单和快速。但是,对于CakePHP的开发,我发现插件更快的启动和运行,因为:

  • 插件附带所有的PHP代码你需要开始。使用Beanstalkd,您需要编写更多的代码(例如轮询队列的PHP守护程序查找作业)。
  • Beanstalkd服务器基础架构变得更加复杂。我必须为dev / test / prod安装 beanstalkd 的多个实例,并安装
  • 开发/测试有点简单,因为它是一个自包含的CakePHP + MySQL解决方案。您只需输入蛋糕队列添加用户注册和蛋糕队列runworker 。

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:

  • cakephp-queue (MySQL)
  • CakePHP-Queue-Plugin (MySQL)
  • CakeResque (Redis)
  • cakephp-gearman (Gearman)
  • and others.

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作业

本文发布于:2023-11-27 09:18:06,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   控制器   CakePHP   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!