Laravel 8.24 listeners/events 没有加入队列
Laravel 8.24 listeners/events doesn't adds to queue
我想为某些事件实现电子邮件通知。我还使用 Laravel 数据库队列异步处理此事件。
这是我拥有的:
事件 class:
<?php
namespace App\Events;
use App\Models\ServerReviewVote;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class NewServerReviewVote
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(public ServerReviewVote $serverReviewVote)
{
}
}
监听器class:
<?php
namespace App\Listeners;
use App\Events\NewServerReviewVote;
use App\Notifications\NewServerReviewVote as NewServerReviewVoteNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendNewServerReviewVoteNotification implements ShouldQueue
{
/**
* Handle the event.
*
* @param NewServerReviewVote $event
* @return void
*/
public function handle(NewServerReviewVote $event)
{
$event->serverReviewVote->serverReview->author
->notify(new NewServerReviewVoteNotification($event->serverReviewVote->serverReview));
}
}
通知class:
<?php
namespace App\Notifications;
use App\Models\ServerReview;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class NewServerReviewVote extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(private ServerReview $serverReview)
{
}
/**
* Get the notification's delivery channels.
*
* @param User $notifiable
* @return array
*/
public function via(User $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param User $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail(User $notifiable): MailMessage
{
return (new MailMessage)
->subject('New vote for your review!')
->greeting("Hello, {$notifiable->name}!")
->line("We got a new vote for your review for {$this->serverReview->server->name} server!")
->line("Your review currently have {$this->serverReview->votesUpCount()} upvotes and {$this->serverReview->votesDownCount()} downvotes.")
->line("Click the button below to navigate to the server page:")
->action($this->serverReview->server->name, route('servers.show', ['server' => $this->serverReview->server->slug]));
}
}
我正在从这个观察者那里触发事件:
<?php
namespace App\Observers;
use App\Events\NewServerReviewVote;
use App\Models\ServerReviewVote;
class ServerReviewVoteObserver
{
/**
* @param ServerReviewVote $serverReviewVote
*/
public static function created(ServerReviewVote $serverReviewVote)
{
event(new NewServerReviewVote($serverReviewVote));
}
}
我配置了队列数据库驱动程序,我的数据库有 jobs
table。
我的期望是此事件将添加到此 table,然后我可以使用 php artisan queue:work
处理它。但出于某种原因,电子邮件是同步发送的,而不是添加到队列中。我错过了什么?
好的,我一直在尝试不同的方法并找到了如何将侦听器添加到队列中。
我将 public $connection = 'database'
属性 添加到监听器 class。
这是现在的样子:
<?php
namespace App\Listeners;
use App\Events\NewServerReviewVote;
use App\Notifications\NewServerReviewVote as NewServerReviewVoteNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendNewServerReviewVoteNotification implements ShouldQueue
{
/**
* Connection for queue
* @var string
*/
public string $connection = 'database';
/**
* Handle the event.
*
* @param NewServerReviewVote $event
* @return void
*/
public function handle(NewServerReviewVote $event)
{
$event->serverReviewVote->serverReview->author
->notify(new NewServerReviewVoteNotification($event->serverReviewVote->serverReview));
}
}
据我从 Laravel 文档中了解到,这个 属性 不是必需的,但出于某种原因,如果没有它,侦听器不会分派到队列中。现在效果很好!
我还清除了通知 class,因为它不需要实现 ShouldQueue
接口和使用 Queueable
特征。
这是现在的样子:
<?php
namespace App\Notifications;
use App\Models\ServerReview;
use App\Models\User;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class NewServerReviewVote extends Notification
{
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(private ServerReview $serverReview)
{
}
/**
* Get the notification's delivery channels.
*
* @param User $notifiable
* @return array
*/
public function via(User $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param User $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail(User $notifiable): MailMessage
{
return (new MailMessage)
->subject('New vote for your review!')
->greeting("Hello, {$notifiable->name}!")
->line("We got a new vote for your review for {$this->serverReview->server->name} server!")
->line("Your review currently have {$this->serverReview->votesUpCount()} upvotes and {$this->serverReview->votesDownCount()} downvotes.")
->line("Click the button below to navigate to the server page:")
->action($this->serverReview->server->name, route('servers.show', ['server' => $this->serverReview->server->slug]));
}
}
现在侦听器已成功调度到 jobs
table 并且可以由 运行 php artisan queue:work database
处理
我想为某些事件实现电子邮件通知。我还使用 Laravel 数据库队列异步处理此事件。
这是我拥有的:
事件 class:
<?php
namespace App\Events;
use App\Models\ServerReviewVote;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class NewServerReviewVote
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(public ServerReviewVote $serverReviewVote)
{
}
}
监听器class:
<?php
namespace App\Listeners;
use App\Events\NewServerReviewVote;
use App\Notifications\NewServerReviewVote as NewServerReviewVoteNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendNewServerReviewVoteNotification implements ShouldQueue
{
/**
* Handle the event.
*
* @param NewServerReviewVote $event
* @return void
*/
public function handle(NewServerReviewVote $event)
{
$event->serverReviewVote->serverReview->author
->notify(new NewServerReviewVoteNotification($event->serverReviewVote->serverReview));
}
}
通知class:
<?php
namespace App\Notifications;
use App\Models\ServerReview;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class NewServerReviewVote extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(private ServerReview $serverReview)
{
}
/**
* Get the notification's delivery channels.
*
* @param User $notifiable
* @return array
*/
public function via(User $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param User $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail(User $notifiable): MailMessage
{
return (new MailMessage)
->subject('New vote for your review!')
->greeting("Hello, {$notifiable->name}!")
->line("We got a new vote for your review for {$this->serverReview->server->name} server!")
->line("Your review currently have {$this->serverReview->votesUpCount()} upvotes and {$this->serverReview->votesDownCount()} downvotes.")
->line("Click the button below to navigate to the server page:")
->action($this->serverReview->server->name, route('servers.show', ['server' => $this->serverReview->server->slug]));
}
}
我正在从这个观察者那里触发事件:
<?php
namespace App\Observers;
use App\Events\NewServerReviewVote;
use App\Models\ServerReviewVote;
class ServerReviewVoteObserver
{
/**
* @param ServerReviewVote $serverReviewVote
*/
public static function created(ServerReviewVote $serverReviewVote)
{
event(new NewServerReviewVote($serverReviewVote));
}
}
我配置了队列数据库驱动程序,我的数据库有 jobs
table。
我的期望是此事件将添加到此 table,然后我可以使用 php artisan queue:work
处理它。但出于某种原因,电子邮件是同步发送的,而不是添加到队列中。我错过了什么?
好的,我一直在尝试不同的方法并找到了如何将侦听器添加到队列中。
我将 public $connection = 'database'
属性 添加到监听器 class。
这是现在的样子:
<?php
namespace App\Listeners;
use App\Events\NewServerReviewVote;
use App\Notifications\NewServerReviewVote as NewServerReviewVoteNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendNewServerReviewVoteNotification implements ShouldQueue
{
/**
* Connection for queue
* @var string
*/
public string $connection = 'database';
/**
* Handle the event.
*
* @param NewServerReviewVote $event
* @return void
*/
public function handle(NewServerReviewVote $event)
{
$event->serverReviewVote->serverReview->author
->notify(new NewServerReviewVoteNotification($event->serverReviewVote->serverReview));
}
}
据我从 Laravel 文档中了解到,这个 属性 不是必需的,但出于某种原因,如果没有它,侦听器不会分派到队列中。现在效果很好!
我还清除了通知 class,因为它不需要实现 ShouldQueue
接口和使用 Queueable
特征。
这是现在的样子:
<?php
namespace App\Notifications;
use App\Models\ServerReview;
use App\Models\User;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class NewServerReviewVote extends Notification
{
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(private ServerReview $serverReview)
{
}
/**
* Get the notification's delivery channels.
*
* @param User $notifiable
* @return array
*/
public function via(User $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param User $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail(User $notifiable): MailMessage
{
return (new MailMessage)
->subject('New vote for your review!')
->greeting("Hello, {$notifiable->name}!")
->line("We got a new vote for your review for {$this->serverReview->server->name} server!")
->line("Your review currently have {$this->serverReview->votesUpCount()} upvotes and {$this->serverReview->votesDownCount()} downvotes.")
->line("Click the button below to navigate to the server page:")
->action($this->serverReview->server->name, route('servers.show', ['server' => $this->serverReview->server->slug]));
}
}
现在侦听器已成功调度到 jobs
table 并且可以由 运行 php artisan queue:work database