如何让函数在 Laravel 的后台运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31451787/
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
How to make function run in background in laravel
提问by Nilesh
I am developing a website in Laravel 5.0 and hosted in Windows Server2012.
我正在 Laravel 5.0 中开发一个网站,并托管在 Windows Server2012 中。
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 .
我遇到了一个问题,即我从另一个函数 A 调用控制器中的函数 B ,我希望调用另一个函数 B 的函数 A 不等待函数 B 的完成。而Function B获取后台完成页面的独立表单用户终止和函数A返回。
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.
我已经搜索过这个,发现这可以通过像 windows 中的作业、pcntl_fork() 和 laravel 中的队列功能的 cron 来实现。我是这一切的初学者。
Please help! thanks in advance.
请帮忙!提前致谢。
回答by UX Labs
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 :
正如文档所述http://laravel.com/docs/5.1/queues,首先您需要设置驱动程序 - 我会在开始时使用数据库:
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) {
//
});
}
}
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
Does that answer?
这能回答吗?