如何使函数在Laravel中在后台运行 [英] How to make function run in background in laravel

查看:188
本文介绍了如何使函数在Laravel中在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Laravel 5.0开发一个网站,并在Windows Server2012中托管.

I am developing a website in Laravel 5.0 and hosted in Windows Server2012.

我陷入了一个问题,那就是我正在从另一个函数A调用控制器中的函数B,并且我希望调用另一个函数B的函数A不等待函数B的完成.并且功能B在后台完成并以独立的形式终止页面的用户终止,而功能A返回.

I am stuck at a problem which is I am calling a function B in controller from another function A and I want that the function A which calls the another function B does not wait for the completion of function B . And Function B gets completes in the background and independent form user termination of page and function A return .

我进行了搜索,发现可以通过Windows中的cron作业,larnt中的pcntl_fork()和Queue功能来实现.我是这一切的初学者.

I have searched this and found that this can be implemented through cron like jobs in windows, pcntl_fork() and Queue functionality in laravel. I am beginner in all this.

请帮助!提前致谢.

推荐答案

,如文档所述 http://laravel.com/docs/5.1/queues ,首先,您需要设置驱动程序-我将在一开始就使用数据库:

as the documentation states http://laravel.com/docs/5.1/queues, first you need to setup the driver - i would go for database in the beginning :

php artisan queue:table

php artisan migrate

然后创建要添加到队列中的作业

then create the Job that you want to add to the queue

<?php

namespace App\Jobs;

use App\User;
use App\Jobs\Job;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendEmail extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function handle(Mailer $mailer)
    {
        $mailer->send('emails.hello', ['user' => $this->user], function ($m) {
            //
        });
    }
}

然后在Controller中调度作业

then in the Controller dispatch the job

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use App\Jobs\SendReminderEmail;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Send a reminder e-mail to a given user.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function sendReminderEmail(Request $request, $id)
    {
        $user = User::findOrFail($id);

        $sendEmailJob = new SendEmail($user);

        // or if you want a specific queue

        $sendEmailJob = (new SendEmail($user))->onQueue('emails');

        // or if you want to delay it

        $sendEmailJob = (new SendEmail($user))->delay(30); // seconds

        $this->dispatch($sendEmailJob);
    }
}

要使其正常工作,您需要运行队列侦听器":

For that to work, you need to be running the Queue Listener:

php artisan queue:listen

这个答案吗?

这篇关于如何使函数在Laravel中在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆