Reputation: 9800
I'm working on CakePHP project.
I have some task to execute in background which can take long time for its completion. Therefore I have used cakephp-queue plugin (Thanks to dev).
Now I have moved all my tasks in a shell and every time user clicks a button, a new job is created from controller using this function
$job = $this->QueuedJobs->createJob('Scan', [
'server_id' => $id,
'user' => $this->Auth->user('id'),
]);
This is working fine. But in order to execute the task, I need to run command from terminal
bin/cake queue runworker
It is not possible when the project is live and deployed. So, how can I execute this command from within controller just after creating the job?
Upvotes: 0
Views: 1456
Reputation: 25698
You haven't understood how tasks work. The whole point is to not have a controller waiting for something nor to run a shell from a web context.
Read the documentation again: https://github.com/dereuromark/cakephp-queue/tree/master/docs#setting-up-the-trigger-cronjob you're supposed to create a cron job.
*/10 * * * * cd /full/path/to/app && bin/cake queue runworker -q
Make sure you use crontab -e -u www-data to set it up as www-data user, and not as root etc.
This would start a new worker every 10 minutes. If you configure your max life time of a worker to 15 minutes, you got a small overlap where two workers would run simultaneously. If you lower the 10 minutes and raise the lifetime, you get quite a few overlapping workers and thus more "parallel" processing power. Play around with it, but just don't shoot over the top.
It will pick up the task and update the status of the task.
Upvotes: 2