在 Laravel 5 中设置队列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30466161/
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 11:38:29  来源:igfitidea点击:

Setting up queue in Laravel 5

phplaravelqueuelaravel-5

提问by Streetlamp

I am trying to run code after returning a HTTP response. I know that Laravel 5 has support for queues, but I still find them confusing. I am trying to run code after the user has registered which requires the user's password and username. This answerseems interesting, but is not directly applicable to Laravel.

我试图在返回 HTTP 响应后运行代码。我知道 Laravel 5 支持queues,但我仍然觉得它们令人困惑。我试图在用户注册后运行代码,这需要用户的密码和用户名。这个答案看起来很有趣,但并不直接适用于 Laravel。

  • How do I create a job in a queue?
  • How can I pass data to the new job?
  • 如何在队列中创建作业?
  • 如何将数据传递给新作业?

I know this sounds lazy and all, but I really don't understand the documentation.

我知道这听起来很懒惰,但我真的不明白文档。

回答by Bogdan

Setting up queues requires, as the very first step, to choose what driver you'll be using. Because it's the quickest to get running, I'll explain how to begin with the databasedriver, as it doesn't require any other services to be installed on the server (as it's the case for beanstalkdfor example). Here's how to get this set up:

作为第一步,设置队列需要选择您将使用的驱动程序。因为它运行起来最快,所以我将解释如何从database驱动程序开始,因为它不需要在服务器上安装任何其他服务(例如,就是这种情况beanstalkd)。以下是如何进行此设置:

1.Set the QUEUE_DRIVERin your .envfile:

1.QUEUE_DRIVER在您的.env文件中设置:

QUEUE_DRIVER=database

2.Run this command to generate the migration file for the jobstable, that will be used to store job information:

2.运行此命令为jobs表生成迁移文件,该文件将用于存储作业信息:

php artisan queue:table

3.Now run the migration to create the table:

3.现在运行迁移以创建表:

php artisan migrate

A jobstable was created that will store data when jobs are pushed on the queue.

jobs创建了一个表,当作业被推入队列时,该表将存储数据。



You can push both commands and clojures onto queues. For the sake of brevity, I'll show an example of how to push a closure onto a queue:

您可以将命令和 clojure 都推送到队列中。为简洁起见,我将展示如何将闭包推入队列的示例:

$username = Request::input('username');
$password = Request::input('password');

// Do your registration stuff

// Push a job onto the queue
\Queue::push(function($job) use ($username, $password)
{
    // Do the stuff you need here with $username and $password

    // Delete the job from the queue
    $job->delete();
});


The final step to making this work is to run the queue listener. Jobs will not be processed automatically unless the queue listener is running. So run this command:

完成这项工作的最后一步是运行队列侦听器。除非队列侦听器正在运行,否则不会自动处理作业。所以运行这个命令:

php artisan queue:listen

There are further steps you can take, such as setting up Supervisorto monitor and restart the queue listener should it crash, but this should be enough to get you started.

您还可以采取进一步的步骤,例如设置Supervisor以监视并在崩溃时重新启动队列侦听器,但这应该足以让您开始。

回答by Mashpy Rahman

Generally we pass data on the queue like this -

通常我们像这样在队列上传递数据 -

On the controller we have written -

在我们写的控制器上 -

$this->dispatch(new videoToAudioConvert($video_id))

On the job section you have to write like this -

在工作部分,你必须这样写——

protected $video_id

public function __contructor($video_id){
  $this->video_id = $video_id
}

public function handle(){
  $this->video_id
}

You can get more idea how to create jobs in queue and how to pass variable from here.

您可以从这里获得更多关于如何在队列中创建作业以及如何传递变量的想法。

回答by Eng Tareq Dokhan

what is the requirement data to store in jobs table i use this to send email with queue and i will Schedule it i do the first 3 steps. Set the QUEUE_DRIVER in your .env file:

存储在作业表中的要求数据是什么我用它来发送带有队列的电子邮件,我将安排它我执行前 3 个步骤。在 .env 文件中设置 QUEUE_DRIVER :

QUEUE_DRIVER=database 2. Run this command to generate the migration file for the jobs table, that will be used to store job information:

QUEUE_DRIVER=database 2. 运行此命令以生成作业表的迁移文件,该文件将用于存储作业信息:

php artisan queue:table 3. Now run the migration to create the table:

php artisan queue:table 3. 现在运行迁移以创建表:

php artisan migrate A jobs table was created that will store data when jobs are pushed on the queue.

php artisan migrate 创建了一个作业表,当作业被推入队列时,该表将存储数据。