0

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Martijn
  • 2,268
  • 3
  • 25
  • 51

1 Answers1

5

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.

Community
  • 1
  • 1
fideloper
  • 12,213
  • 1
  • 41
  • 38
  • I now have a Beanstalkd setup for Laravel. But the problem is, it can only handle one job at a time. I want to process all jobs at once. Or as soon as they come in. Does that make sense? – Martijn Oct 21 '13 at 15:32
  • 1
    Yep - You can setup multiple workers listening to the same queue. In the linked article, it describes using the `queue:listen` command and monitoring it with Supervisor. You can create multiple listeners and have Supervisor monitor all of them. **Note: If you need to create Listeners dynamically** (one per user), then I don't have good advice for you. That's not a great use of queues necessarily. – fideloper Oct 21 '13 at 18:34
  • How can I do that? Can I also have multiple workers on different servers etc? – Martijn Oct 21 '13 at 20:15
  • 1
    You can do that by running the `queue:listen` command multiple times. In its simplest form, you can literally just type that command into your terminal multiple times. Yes this can (and often should) be done on different servers. Note that the server listening for jobs in the queue needs to have the code available to run the job. You may therefore end up with copies of your app on multiple servers. That's fine. Lastly, based on your questions, I think you need to [read more on how queues work](https://github.com/kr/beanstalkd/wiki/faq). – fideloper Oct 21 '13 at 20:22
  • One question, If I open multiple queue:listen's I notice that the same script can't be active multiple times. Is that true? – Martijn Oct 22 '13 at 20:52
  • Beanstalkd should take care of making sure that the multiple workers don't try to run the same **JOB**. Whether the same script can be used at the same time depends on your code details. Should be fine unless your jobs overwrite each others work in some way. That's an implementation detail of your code. – fideloper Oct 22 '13 at 21:21
  • The only thing my script does is make a connection to a site, edit some model details, and log some info to a database. Do you have skype or something similar because you seem like the guy that can help me. – Martijn Oct 22 '13 at 21:28