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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With