Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly run a Symfony task in the background from an action?

What is the correct way to run Symfony tasks in a separate process. My first guess would be to use fork/exec, but according to this, you can't do it with anything that keeps open file descriptors or connections (like MySQL). So that doesn't sound like its an option. Another alternative is to do exec('symfony taskname &'), but that seems like a hack. Is that the best I can do? Is there a third way?

like image 801
Alex Grin Avatar asked Nov 25 '22 16:11

Alex Grin


2 Answers

The way this is generally handled is to use a task queue. When you want to do a background process, add it to a queue of some kind (you could use your database, or you could use an actual queue daemon like beanstalkd). You then have some daemonized procces(es) whose job is to pull work out of the queue and perform it.

like image 90
ryeguy Avatar answered Nov 28 '22 06:11

ryeguy


Here's how I ended up doing it:

exec('nohup ' . sfConfig::get('sf_root_dir') . '/symfony TASKNAME >/dev/null &');

You have to redirect STDOUT, or else it won't run in the background (though you don't have to use /dev/null if you want the actual output). In my case I set up all my tasks to use Symfony's file logger, so it wasn't an issue.

I'm still looking for a better solution though. This seems like a hack.

like image 43
Alex Grin Avatar answered Nov 28 '22 07:11

Alex Grin