【问题标题】:Laravel queue is not doing job in the backgroundLaravel 队列没有在后台工作
【发布时间】:2020-12-10 03:24:53
【问题描述】:

我正在尝试使用 laravel 队列发送批量电子邮件。到目前为止,我已经写下了逻辑并且工作正常,但问题是当我在控制器中编写逻辑时需要很长时间,所以我想使用作业,但问题仍然存在。

我的问题

我的问题是,即使我使用队列,我也无法在后台发送电子邮件。

控制器

    public function newsletter(Request $request)
    {
        //dd($request->all());
        dispatch(new SendEmail($request));

        Session::flash('message', 'Email Sent');
        Session::flash('class', 'success');
        return redirect()->route('news');
    }

工作

    public function handle(Request $request)
    {
        //
        $data = array(
            'message' => $request->message,
            'subject' => $request->subject,
            'file' => $request->file("file")
        );
        
        $teachingLevel = $request->highest_teaching_level;
        $school = $request->school;
        $province = $request->province;
        $district = $request->district;

        $subject = $request->subject;

        if ($teachingLevel != "" && $school != "" && $province != "" && $district != "") {
            $email = User::where('highest_teaching_level', $teachingLevel)->where('current_school_name', $school)->where('address','LIKE', '%'.$province.'%')->where('address','LIKE', '%'.$district.'%')->pluck('email');
        }else{
        $email = User::pluck('email');
        }
        
        foreach($email as $e)
        {
            Mail::to($e)->send(new NewsLetter($data, $subject));
        }
    }

电子邮件已发送,但不会在后台发生。也许这与我在handle() 函数中传递$request 变量的方式有关。

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 您是否尝试过使用“同步”队列驱动程序?
  • @TariqImtinan 不,我使用过数据库队列。
  • 然后尝试使用“同步”驱动程序,希望它能帮助您找出问题的根本原因。

标签: php laravel eloquent laravel-queue


【解决方案1】:

这是我在项目中使用 Laravel 作业的方式:

SampleJob.php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Services\SampleService;

class SampleJob implements ShouldQueue {
  use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  
  // if you omit this value, you'll be in trouble, believe me
  public $tries = 1; 

  private $param;

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

  public function handle(SampleService $service) {
    // do something with $this->param
    // Also, notice that you can type hint classes in handle function declaration for DI
    $service->doSomething($this->param);
  }
  
}

SampleController.php


namespace App\Http\Controllers;

use App\Jobs\SampleJob;


class SampleController extends Controller {
    
  public function sampleMethod(Request $request) {
    $param = $request->input('param');
    SampleJob::dispatch($param); // $param will be passed to SampleJob constructor
    // ...
  }

}

需要注意的几点是:

  • 阅读我的代码 sn-ps 中的 cmets
  • 如果您使用基于 db 的队列,请先使用 php artisan queue:table && php artisan migrate 迁移
  • 使用 artisan 命令创建作业:php artisan make:job Sample
  • 别忘了运行队列工作者:php artisan queue:work。让它在后台运行:sudo nohup php artisan queue:work > ./storage/logs/queue-worker.log &
  • 强烈推荐:在部署中,使用Supervisor 保持php artisan queue:work 在后台运行
  • 如果您设法使作业正常工作,所有延迟(排队但由于配置错误或未启动队列工作器而未处理)的工作将立即执行。

常见陷阱:

  • 如果您没有设置 $tries 参数,并且您的作业以某种方式抛出错误,laravel 将尝试一次又一次地重试该作业,直到您的数据库关闭:(
  • 如果 http 用户和 php 用户不同,并且如果您在工作中使用了Log,那么十分之九的情况下您会遇到storage 目录的权限问题。为避免此问题,请将'permission' => '0666' 添加到config/logging.php 中的日志通道设置中
  • 队列工作器未检测到您的代码更改,因此在您对代码库进行一些更改后,通过php artisan queue:restart 重新启动队列工作器。

我的 laravel 版本:5.8

【讨论】:

    【解决方案2】:

    如果您要使用“数据库”连接,则需要运行下一次迁移:

    php artisan queue:table
    
    php artisan migrate
    

    还有一个事件和一个实现“ShouldQueue”接口的监听器,最后在“providers/EventProvider.php”路径和“EventProvider.php”文件中注册与监听器或监听器关联的事件,添加你的以下一个表示法为例的事件和侦听器:

    protected $listen = [
      Registered::class => [
        SendEmailVerificationNotification::class,
      ],
    ];
    

    了解与以下队列相关的几点很重要:重启命令

    php artisan queue:restart
    

    为此,您需要运行队列侦听器:

    php artisan queue:listen
    

    参考:https://medium.com/ariel-mejia-dev/run-queues-in-the-background-on-development-in-laravel-8d697d812f29

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 2018-06-20
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      相关资源
      最近更新 更多