繁体   English   中英

Laravel 队列不在后台工作

[英]Laravel queue is not doing job in the background

我正在尝试使用 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变量的方式有关。

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

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

示例作业.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);
  }
  
}

示例控制器.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
    // ...
  }

}

值得注意的几点是:

  • 阅读我的代码片段中的注释
  • 如果您使用基于数据库的队列,请先使用php artisan queue:table && php artisan migrate
  • 使用 artisan 命令创建作业: php artisan make:job Sample
  • 不要忘记运行 queue worker: 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

如果您打算使用“数据库”连接来运行下一次迁移:

php artisan queue:table

php artisan migrate

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

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

了解与以下 queue:restart 命令相关的几点很重要

php artisan queue:restart

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

php artisan queue:listen

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM