dongqian1028 2013-10-21 13:16
浏览 564

使用Laravel在后台执行PHP脚本

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.

  • 写回答

1条回答

  • doutongfu9484 2013-10-21 15:05
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条