php - Laravel 队列 :listen queue:work not working

标签 php mysql laravel laravel-5.3 digital-ocean

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

我已经使用创建了 jobs 表和 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');
                    });

最佳答案

我明白了,问题出在 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);

关于php - Laravel 队列 :listen queue:work not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48207362/

相关文章:

PHP + MySQL : Time as a string into numerics

php 重复输出

python - 为什么我的 Web 应用程序在尝试 POST INSERT 查询时生成 1064 (42000) 错误?

laravel - 将两列显示为一列

php - 在 Laravel 5.4 中安装 Laracast Generator 后出现 Trait 'Illuminate\Console\AppNamespaceDetectorTrait' not found 错误

php - 如何使用ajax一键插入两行Laravel

php - 如何使用 Ajax 和 jQuery 更改 Blade 表中检索到的数据顺序

php - 如何选择然后插入phpmyadmin?

使用 Gitlab CI 错误的 Laravel Dusk 测试,无法连接到端口 :9515: Connection refused

PHP opendir() 函数问题