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

问题描述

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

Is it possible to use dispatchShell from a Controller?

我的任务是在用户注册后开始一个 shell 作业.

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

我使用的是 CakePHP 2.0

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.

由于您的用户正在执行的 PHP 代码发生在 Apache 启动的请求期间,因此执行的任何代码都将暂停该请求,直到它完成(除非您遇到 Apache 的请求超时).

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).

如果您的应用程序不能接受上述内容,那么您需要在 Apache 请求之外(即从命令行)触发 PHP.

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).

在可用性方面,此时通知您的用户您正在后台处理数据是有意义的.从一条消息告诉他们他们可以稍后再检查到旋转进度条,通过 ajax 轮询您的应用程序以检测作业完成情况.

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.

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

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.

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

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.

它的工作方式是,在您的代码中(当用户注册时),您将一个作业插入到队列中.队列守护进程立即获取作业(它不按时间间隔运行,因此它一直在等待)并将其交给工作进程(例如 CakePHP shell).它是即时的,而且 - 如果你告诉它 - 它知道它是否有效,它知道它是否失败,它可以根据需要重试,并且不会意外地处理相同的工作两次.

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.

有许多可用的,例如 BeanstalkddroprGearmanRabbitMQ 等.还有许多 CakePHP 插件(不同年龄)可以提供帮助:

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.

我有将 CakePHP 与 Beanstalkd(+ PHP Pheanstalk 库)和 CakePHP Queue 插件(上面的第一个)一起使用的经验).我必须感谢 Beanstalkd(用 C 编写),因为它非常轻量、简单和快速.但是,关于 CakePHP 开发,我发现该插件启动和运行速度更快,因为:

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:

  • 该插件附带了您开始使用所需的所有 PHP 代码.使用 Beanstalkd,您需要编写更多代码(例如轮询队列以查找作业的 PHP 守护程序)
  • Beanstalkd 服务器基础架构变得更加复杂.我必须为 dev/test/prod 安装多个 beanstalkd 实例,并安装 supervisord 照看流程).
  • 开发/测试更容易一些,因为它是一个自包含的 CakePHP + MySQL 解决方案.你只需要输入cake queue add user signupcake queue runworker.
  • 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 作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:03