【问题标题】:Laravel queue:listen queue:work not workingLaravel 队列:监听队列:工作不工作
【发布时间】:2018-06-20 19:12:34
【问题描述】:

我正在尝试在后台发送电子邮件以减少服务器响应时间。

我已经创建了作业表和 failed_jobs 表使用 php artisan queue:tablephp artisan queue:failed-table 命令。并在 .env 文件中设置QUEUE_DRIVER=database

当我执行以下代码时,它会在作业表中创建一个作业。

\Mail::later(5, 'email.new-friend-request', ['data'=>$friend_request], function($message) use (&$friend_request){
                    $message->to($friend_request->notifiable->email, $friend_request->notifiable->name)->from('info@example.com','ABC')->subject('New Friend Request');
                });

但是当我执行php artisan queue:listenphp artisan queue:work 命令时。它既不处理保存在作业表中的作业,也不在控制台上提供任何输出。

但是,当我检查作业表时,作业的尝试字段不断增加。但是作业没有得到处理。

另外,当我使用以下代码直接发送邮件时,即不将其添加到队列中。邮件发送没有任何问题。

\Mail::send('email.new-friend-request', ['data'=>$friend_request], function($message) use (&$friend_request){
                        $message->to($friend_request->notifiable->email, $friend_request->notifiable->name)->from('info@example.com','ABC')->subject('New Friend Request');
                    });

更新

我尝试发送没有任何数据的电子邮件,它也可以正常工作。 即

\Mail::later(5, 'email.new-friend-request', [], function($message) use (&$friend_request){
                        $message->to($friend_request->notifiable->email, $friend_request->notifiable->name)->from('info@example.com','ABC')->subject('New Friend Request');
                    });

【问题讨论】:

  • 如果可能,将您的 QUEUE_DRIVER 设置为同步,然后编写一个相当命令来调度作业,这样您就可以看到可能正在停止作业的错误。您还为您的工作添加了失败的功能吗?
  • 相当命令是什么意思?
  • storage/logs/laravel.log 听起来您的某些逻辑在创建电子邮件时失败了。
  • storage/logs/laravel.log 文件为空白
  • 意思是快速命令,拼写错误

标签: php mysql laravel laravel-5.3 digital-ocean


【解决方案1】:

我明白了,问题出在 Eloquent 关系上。 当邮件被查询时,Eloquent 模型对象需要被序列化。因此,一旦模型对象被序列化,就无法访​​问关系。

所以我只是尝试立即加载模型关系,并使用toArray() 方法将我的模型对象转换为数组,然后开始处理作业。

那是在调用之前

\Mail::later(5, 'email.new-friend-request', ['data'=>$friend_request], function($message) use (&$friend_request){
                    $message->to($friend_request->notifiable->email, $friend_request->notifiable->name)->from('info@example.com','ABC')->subject('New Friend Request');
                });

我渴望在$friend_request 对象上加载所有关系。

例如:-

$friend_request = FriendRequest::with('notifiable')->find($request_id);

【讨论】:

    猜你喜欢
    • 2016-09-21
    • 2018-03-15
    • 2014-11-10
    • 2018-07-30
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    相关资源
    最近更新 更多