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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序