Martijn
Martijn

Reputation: 2324

Execute a PHP script in the Background using Laravel

I have a web application written using Laravel 4 that runs a script when a user requests a certain page or clicks a button. This should activate the script in the background.

Currently, my setup looks like this,

Controller portion:

public function getWorkerPage() {           

    Queue::push('UpdateUserVar1', array('id' => $login->id));

    Queue::push('UpdateUserVar2', array('id' => $login->id));

    Queue::push('Worker', array(
        'login_id' => $login->id
    ));

    .... // Some more stuff

    View::make('workerpage');
}

Worker:

class Worker {

    public function fire($job, $data) {
        ...
        //Process (Takes around 10 minutes)

        // Release the job which makes the worker work until it deletes itself by a piece of logic in the Process bit above.
        $job->release();
    }

}

Now, my whole problem is that when more than one user requests this script to be run, it takes 10 minutes before the previous job is finished. I don't want that. I would like those scripts to just run in the background until the user stops it. And when the user requests the script to be run, that it just runs immediately.

I guess I need to take away the queue then but my problem then is, is that the page will keep loading loading and that's not something that I want either since after the Queue::push's, there is a normal View. So I'm searching for a PHP program or a function that lets me execute the script, pass in a couple variables and that without blocking the rest of the code in the Controller.

I did find some solutions like Gearman and Supervisord but I couldn't really judge if these were options for me since I didn't quite grasp the documentation.

If something is not totally clear about my question, please do not hesitate to ask.

Nutshell: I need a program that executes my script in the background and enables me to keep using Laravels function and pass in variables without blocking the code after the program execute.

Upvotes: 0

Views: 8091

Answers (1)

fideloper
fideloper

Reputation: 12293

So I'm searching for a PHP program or a function that lets me execute the script, pass in a couple variables and that without blocking the rest of the code in the Controller.

That's exactly what Queues are for. However, out of the box, Queues in Laravel will be called synchronously and will therefore block the rest of the code in your controller.

This is because setting up Queues requires some third party software to function, such as installing beanstalkd or using iron.io. Laravel defaults to just running the queue when you create them because it can't assume you have any queue-processing functionality setup ahead of time.

I'm ot 100% sure what your business goals are (being able to stop a queue job mid-processing?), but I think you're missing some needed information in what/how Queues function within an application.

Upvotes: 5

Related Questions