2

Symfomaniacs!

Let's say I have this code in my controller:

        for ($i = 0; $i <= 3; $i++) 
        {
            $command = 'php ' . $kernel->getRootDir() . '/console Educa:ExecuteJob ' 
                                         . $i . ' --env=' . $kernel->getEnvironment();
            $logger->addInfo("Executing command: ".$command);
            $process = new Process($command);
            $process->disableOutput();
            $process->start();
        }
        //sleep(5);
        return $this->render(...);

and the ExecuteJob takes about 5 seconds to execute. Inside the console command, I have a logger->addInfo() at both the beginning and end script.

If I uncomment the sleep(5) line they both print. If I don't, only the initial line prints in the log, which must mean that when the controller scripts end, it stops all processes, am I wrong?

Is there any way to workaround this? I'm open to change the design, I just want to execute background stuff without having to wait to render the page (is not even relevant for that page)

PS: I'm looking for a way to run background tasks that plays nice with Symfony2, not weird php hacks or cron jobs. I know about those. I also don't want to setup a MQ queue. It's kind of overkill for what I'm trying.

3 Answers 3

2

In your situation the perfect solution would be to use the kernel.terminate Event, it's triggered after the response is streamed to the client so you can do some heavy operations there.

Sign up to request clarification or add additional context in comments.

4 Comments

I was just answering myself saying that I've read that one, but I'm not sure if this is good practice or just a messy source of potential pain that I will regret forever.
@DanielParejoMuñoz It depends on what you want to do. If it's a little operation kernel.terminate is perfectly fine. If you want to do really heavy operations that need multiple minutes to process think about Crons and that stuff.
So let's assume it is lightweight. How do I atach it to a controller? I mean, in the example here link you can see it attachs it to every kernel.exception event. I don't want that. I want this code to just execute when the "Invitations Controller" terminates (ideally, when one of his methods). Is there a way to be more specific like that?
@DanielParejoMuñoz You can have some kind of message/event pool which you can process on kernel.terminate and put messages to the pool when you need to run your late processing.
0

You will need something like this https://github.com/schmittjoh/JMSJobQueueBundle but you will need to setup cron too

1 Comment

Nope. Thanks, but, as I said, no cron. TRelying in a cron is not cross-platform, for starters; you may have no control over the server to setup a cron and... a third reason. So no cron.
-1

Use & in command to send it to the background, like this

$command = 'php ' . $kernel->getRootDir() . '/console Educa:ExecuteJob &'

1 Comment

Could someone explain what's wrong with this approach?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.