-3

I am working on CakePHP 3.4 project.

I have to execute some command to scan through the files and directories of a particular directory. This might take long time depending on the size of the directory, therefore I want to run it in background and mark running label in view until it executed successfully.

How can I run a Shell Task in the background from Controller and update database on execution?

I'm new to Shell tasks.

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
  • why do people do not comment the reason for down voting? I had googled it before posting here, I found some plugins but asked here just to know whether there is some way to do it without use of plugins. – Anuj TBE Mar 30 '17 at 11:17

2 Answers2

3

Your thinking along good lines about running this in the background if it is a time consuming task. You will need to use some form of queuing system that allows you to add jobs to a queue that can then get run in the background by running the queue from a cronjob. Take a look at the Queue plugin for doing this.

You'll basically need to create a queue task that contains the functionality that you need running in the background and then add a job to the queue that will run that task in the background. The Queue plugin's documentation shows how to do this and there are a load of example queue tasks included with the plugin.

If you need to indicate the status of the queued job you could save the job's ID in a session and check if it is complete when loading a page.

drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
-1

You can dispatch a Shell task from the controller. If you want to run this in the background you could, for example, run this controller action via JavaScript/Ajax.

// maybe this task runs looooong
set_time_limit(0);

$shell = new ShellDispatcher();
$output = $shell->run(['cake', 'bake', 'model', 'Products']);

if ($output === 0) {
    $this->Flash->success('Yep!');
} else {
    $this->Flash->error('Nope!');
}

But you could indeed have googled this at least. ;-)

EDIT Forget this one, go for drmonkeyninja’s answer.

Marijan
  • 1,825
  • 1
  • 13
  • 18