简体   繁体   English

从控制器 CakePHP 3.x 执行 shell

[英]Execute shell from controller CakePHP 3.x

I have a specific task in a CakePHP Shell and it's executed by a CRON job.我在 CakePHP Shell 中有一个特定任务,它由 CRON 作业执行。 But I want the users to be able to execute it from a web interface (like a button or something like this) whenever he wants.但我希望用户能够随时从 Web 界面(如按钮或类似的东西)执行它。

So, my question is, is this possible to execute a shell from a controller ?所以,我的问题是,这可以从控制器执行 shell 吗?

Emulate this in a controller:在控制器中模拟这个:

bin/cake MyShell

I know it was possible in the previous versions of CakePHP, but I didn't find something related to this in the newest version.我知道在以前版本的 CakePHP 中是可能的,但是在最新版本中我没有找到与此相关的内容。 And use exec("bin/cake MyShell") seems really dirty to me.使用exec("bin/cake MyShell")对我来说似乎很脏。

create a shell object , the call any of its function to want to excute创建一个shell对象,调用它想要执行的任何函数

 $myShell = new \App\Shell\MyShell;
 $myShell->anyShellFun();

in order to call shell from your controller function you need to do this in your controller function :为了从您的控制器函数调用 shell,您需要在控制器函数中执行此操作:

namespace App\Controller;

use App\Controller\AppController;
use Cake\Console\ShellDispatcher; //use this shell dispacher

/**
* Example Controller
*
*/
class ExampleController extends AppController
{

/**
 * Index method
 *
 * @return \Cake\Network\Response|null
 */
public function index()
{
    $shell = new ShellDispatcher(); //create object of your Shell Dispatcher
    $output = $shell->run(['cake', 'foo']); //here foo is your shell name

    if (0 === $output) {
        $this->Flash->success('Success from shell command.');
    } else {
        $this->Flash->error('Failure from shell command.');
    }

    return $this->redirect('/');
  }
}

hope this answer your question, if any problem leave a comment.希望这能回答您的问题,如果有任何问题请发表评论。

thank you.谢谢你。

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).由于您的用户正在执行的 PHP 代码发生在 Apache 启动的请求期间,因此执行的任何代码都将停止该请求,直到它完成(除非您达到 Apache 的请求超时)。

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).如果上述情况对您的应用程序不可接受,那么您将需要在 Apache 请求之外(即从命令行)触发 PHP。

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.从一条消息告诉他们他们可以稍后再检查到旋转进度条,它通过 ajax 轮询您的应用程序以检测作业完成。

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).最简单的方法是让 cronjob 以某个时间间隔(至少每分钟一次)执行 PHP 脚本(即 CakePHP shell)。 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?如果它没有在 cron 间隔内完成怎么办.. 会发生竞争条件吗?

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).队列守护进程立即获取作业(它不按时间间隔运行,因此它一直在等待)并将其交给工作进程(例如 CakePHP shell)。 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:有许多可用的插件,例如BeanstalkddroprGearmanRabbitMQ等。 还有许多 CakePHP 插件(不同年龄)可以提供帮助:

I have had experience using CakePHP with both Beanstalkd (+ the PHP Pheanstalk library) and the CakePHP Queue plugin (first one above).我有将 CakePHP 与 Beanstalkd(+ PHP Pheanstalk库)和 CakePHP Queue 插件(上面的第一个)一起使用的经验。 I have to credit Beanstalkd (written in C) for being very lightweight, simple and fast.我必须感谢 Beanstalkd(用 C 编写),因为它非常轻量、简单和快速。 However, with regards to CakePHP development, I found the plugin faster to get up and running because:但是,关于 CakePHP 开发,我发现该插件启动和运行速度更快,因为:

  • The plugin comes with all the PHP code you need to get started.该插件附带了您开始使用所需的所有 PHP 代码。 With Beanstalkd, you need to write more code (such as a PHP daemon that polls the queue looking for jobs)使用 Beanstalkd,您需要编写更多代码(例如轮询队列寻找作业的 PHP 守护程序)
  • The Beanstalkd server infrastructure becomes more complex. Beanstalkd 服务器基础结构变得更加复杂。 I had to install multiple instances of beanstalkd for dev/test/prod, and install supervisord to look after the processes).我必须为 dev/test/prod 安装多个beanstalkd实例,并安装supervisordsupervisord进程)。
  • Developing/testing is a bit easier since it's a self-contained CakePHP + MySQL solution.开发/测试更容易一些,因为它是一个自包含的 CakePHP + MySQL 解决方案。 You simply need to type cake queue add user signup and cake queue runworker .你只需要输入cake queue add user signupcake queue runworker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM