使用 Laravel 在后台执行 PHP 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19495621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:35:08  来源:igfitidea点击:

Execute a PHP script in the Background using Laravel

phpbackgroundlaravelworker

提问by Martijn

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.

我有一个使用 Laravel 4 编写的 Web 应用程序,它在用户请求某个页面或单击某个按钮时运行一个脚本。这应该会在后台激活脚本。

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.

现在,我的整个问题是,当多个用户请求运行此脚本时,前一个作业完成前需要 10 分钟。我不想要那个。我希望这些脚本在后台运行,直到用户停止它。当用户请求运行脚本时,它会立即运行。

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.

我想我需要带走队列,但我的问题是,页面将继续加载加载,这也不是我想要的东西,因为在Queue::push's 之后,有一个正常的视图。所以我正在寻找一个 PHP 程序或一个函数,它可以让我执行脚本,传入几个变量,并且不会阻塞控制器中的其余代码。

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.

我确实找到了一些解决方案,如 Gearman 和 Supervisord,但我无法真正判断这些是否适合我,因为我不太了解文档。

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.

简而言之:我需要一个在后台执行我的脚本的程序,使我能够继续使用 Laravel 函数并在程序执行后不阻塞代码的情况下传入变量。

回答by fideloper

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.

所以我正在寻找一个 PHP 程序或一个函数,它可以让我执行脚本,传入几个变量,并且不会阻塞控制器中的其余代码。

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

这正是队列的用途。但是,开箱即用,Laravel中的队列将被同步调用,因此会阻塞控制器中的其余代码。

This is because setting up Queues requires some third party software to function, such as installing beanstalkdor 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.

这是因为设置队列需要一些第三方软件的功能,如安装beanstalkd或使用iron.io。Laravel 默认只在你创建队列时运行队列,因为它不能假设你提前设置了任何队列处理功能。

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.

我不能 100% 确定您的业务目标是什么(能够在处理过程中停止队列作业?),但我认为您在应用程序中队列的功能/功能方面缺少一些所需的信息。