CakePHP: Run shell job from controller

2020-02-04 06:51发布

问题:

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

回答1:

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.


回答2:

I was able to run consolle from controller/action, see the example below.

App::uses('ShellDispatcher', 'Console');
...
public function aco_sync() {
    $command = '-app '.APP.' AclExtras.AclExtras aco_sync -r adminControllers -p UserAdmin';
    $args = explode(' ', $command);
    $dispatcher = new ShellDispatcher($args, false);
    if($dispatcher->dispatch()) {
        $this->Session->flash('OK');
    } else {
        $this->Session->flash('Error');
    }
    return $this->redirect(array('action' => 'index'));
}


回答3:

anything is possible, but why would you want to. If you find you need to do something in a shell and the actual application look at using libs.

you stick the code in the lib and then call the lib from both your app and the shell.



回答4:

In CakePHP-3 you can dispatch shells from the controller & do it almost the same as in CakePHP-2. The documentation does not mention this.

// in your controller:
$shell = new \Cake\Console\Shell;
$shell->dispatchShell('shell_class param1 param2');
// or how the docs suggest
$shell->dispatchShell('shell_class', 'param1', 'param2');

Beware of stdout & stderr in unit tests.

Dispatching a shell turns on stdout and stderr logging with ConsoleLogger, and will give you all the logging in your console if you have something like the code snippet above in code that you are testing from phpunit.



回答5:

If this is to intialize AclExtras the best way is:

App::import('Console/Command', 'AppShell');
App::import('Plugin/AclExtras/Console/Command', 'AclExtrasShell');

$job = new AclExtrasShell();
$job->startup();
$job->dispatchMethod('aco_sync');

But avoid this unless you have no possibilities to run the console script.



回答6:

function getEbayOrder(){
    $this->autoRender = false;

    App::import('Console/Command', 'AppShell');
    App::import('Console/Command', 'EbayShell');

    $job = new EbayShell();
    $job->dispatchMethod('get_orders');

    echo "REPONSE";
}


标签: shell cakephp