【问题标题】:How to create a job for queue:work如何为队列创建工作:工作
【发布时间】:2017-04-11 05:46:12
【问题描述】:

当用户在应用程序中注册时,我的工作是发送电子邮件。

SendWelcomeEmail.php

<?php

namespace App\Jobs;

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

class SendWelcomeEmail extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $user;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Mailer $mailer)
    {
        $user = &$this->user;

        $message = sprintf('Hello %s', $user->name);

        $mailer->raw($message, function ($m) use ($user){
            $m->from('lucas.nuck@gmail.com', 'Lucas Lopes');
            $m->to($user->email, $user->name);
        });
    }
}

我想创建一个作业来执行 php artisan queue: work 命令每分钟发送队列中的电子邮件。

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    最简单的方法是设置主管。

    https://laravel.com/docs/5.4/queues#supervisor-configuration,如果失败,它会自动重启你的 queue:work 进程。

    为您解惑:

    app/Console/Kernel.php 文件中,在 schedule 函数中,您可以添加一个您希望以特定时间间隔运行的命令。一个例子是: $schedule-&gt;command('SendFailedLoginsReport')-&gt;weekly()-&gt;mondays()-&gt;at('03:00');

    在你的服务器上,你可以像这样添加你的 cron * * * * * php /path-to-your-project/artisan schedule:run &gt;&gt; /dev/null 2&gt;&amp;1

    这个 Cron 会每分钟调用一次 Laravel 命令调度器

    【讨论】:

      【解决方案2】:

      您想要运行队列守护程序,而不是运行命令。您需要在服务器环境中执行此操作,而不是使用 laravel 预定命令。

      你可以使用类似 Linux 的 screen 来放置你的命令。

      这将持续轮询您的队列服务器以获取新作业,然后对其进行处理。

      【讨论】:

      • 不需要在app/Console/Kernel.php中设置命令,多久执行一次?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 2018-07-15
      • 2012-06-30
      • 2012-04-12
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多