我如何在 Laravel 上使用 Queue::after ?

问题描述

我正在使用 Queue 在我的应用程序上发送邮件,而且效果很好:

class SendMail implements ShouldQueue
{
    use dispatchable,InteractsWithQueue,Queueable,SerializesModels;

    private $user_mail;
    private $person_data;
    private $title;
    private $company_name;
    private $html;
    private $email_sender;
    private $email_reply;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user_mail,$person_data,$title,$company_name,$html,$email_sender,$email_reply)
    {
        $this->user_mail = $user_mail;
        $this->person_data = $person_data;
        $this->title = $title;
        $this->company_name = $company_name;
        $this->html = $html;
        $this->email_sender = $email_sender;
        $this->email_reply = $email_reply;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Mail::to($this->user_mail)
          ->queue(new DocumentMessage($this->person_data,$this->title,$this->company_name,$this->html,$this->email_sender,$this->email_reply));
    }
}

现在我想获取队列发送电子邮件时的日志,并按照文档,将此代码放入我的 AppServiceProvider 中进行测试:

class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    Carbon::setLocale('pt_BR');
    setlocale(LC_ALL,'pt_BR');
    Carbon::setUtf8(true);
    Paginator::useBootstrapThree();
    Blade::withoutDoubleEncoding();

    Queue::after(function (JobProcessing $event) {
      DB::raw("INSERT INTO activity_log (log_name,description,subject_id,subject_type,causer_id,causer_type,properties)
                      VALUES ('email_sent',Now(),null,1111,'App\Models\User','');");
    });
  }

  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    //
  }
}

但是在我使用我的队列发送邮件后没有任何反应。修改 AppServideProvider 后我应该重新启动我的队列作业还是做一些事情?

解决方法

根据 documentationLOAD DATA LOCAL INFILE "/path/ZNRrecords.txt" INTO TABLE znrTake1 FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\' LINES TERMINATED BY '\r' STARTING BY '' (mtgo_id,card_name,mana_cost,cmc,card_type,oracle_text,@vcard_power,@vcard_toughness,rarity) SET card_power = NULLIF(@vcard_power,'\N'),card_toughness = NULLIF(@vcard_toughness,'\N'); 是要使用的正确事件类。您可能想要更新此内容:

JobProcessed

顺便说一下,我建议采用更简洁的方法来更好地利用框架。 Laravel 已经包含了开箱即用的 Queue::after(function (JobProcessed $event) { ... }); 。所以you can listen to the mail event