質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

解決済

1回答

1652閲覧

Laravel Notification が非同期でうまく実行されない

sola

総合スコア12

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2021/04/18 17:13

編集2021/04/18 17:15

やりたい事

  1. Laravel Auth でのユーザー登録 (register) 時に Registered イベントを発火
  2. Registered イベントに登録した UserRegisteredListener にて Notifiable トレイト notify() を実行。引数に指定した UserRegisterdNotifier を呼び出す
  3. UserRegisterdNotifier から mail, slack チャンネル通知をキューに追加し非同期で実行

課題・状況

  • 同期処理(キューに追加しない)の場合うまく行くが、非同期で実行すると失敗する
  • ワーカー上では「Processed」と表示され、正常に実行された事になっている
  • notify() ではなく Notification ファザードの route() 関数で通知先を指定し非同期で実行すると、意図通り通知される
  • Notifiable オブジェクト (User) からユーザー名や ID を取得してメッセージを組み立てたいため、Notification ファザードの route() 関数を利用した通知ではなく、 Notifiable トレイトの notify() もしくは Notification ファザードの send() を用いた通知としたい
  • ちなみに Notification ファザードの send() を用いた通知でも Notifiable トレイト notify() の時と同様の挙動となる
  • laravel.log でエラーが出力されず、状況が曖昧

環境 (Homestead)

  • Laravel 7.30.4
  • PHP 7.4.5
  • MariaDB 14.14
  • jobs テーブル作成済み、ワーカー(artisan queue:work)実行中

コード

app/Models/User.php

PHP

1<?php 2 3namespace App\Models; 4 5use Illuminate\Foundation\Auth\User as Authenticatable; 6use Illuminate\Notifications\Notifiable; 7use Illuminate\Notifications\Notification; 8use Auth; 9 10class User extends Authenticatable 11{ 12 use Notifiable; 13 14 public function routeNotificationForSlack(Notification $notification) 15 { 16 return config('app.slack_webhook_url'); 17 } 18} 19

app/Providers/EventServiceProvider.php

PHP

1<?php 2 3namespace App\Providers; 4 5use Illuminate\Auth\Events\Registered; 6use Illuminate\Auth\Listeners\SendEmailVerificationNotification; 7use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; 8use Illuminate\Support\Facades\Event; 9 10class EventServiceProvider extends ServiceProvider 11{ 12 protected $listen = [ 13 Registered::class => [ 14 SendEmailVerificationNotification::class, 15 'App\Listeners\UserRegisteredListener', 16 ] 17 ]; 18 19 public function boot() 20 { 21 parent::boot(); 22 } 23}

app/Listeners/UserRegisterdListener.php

PHP

1<?php 2 3namespace App\Listeners; 4 5use Illuminate\Auth\Events\Registered; 6use Illuminate\Contracts\Queue\ShouldQueue; 7use Illuminate\Queue\InteractsWithQueue; 8use App\Notifications\UserRegisterdNotifier; 9use Illuminate\Support\Facades\Notification; 10 11class UserRegisteredListener 12{ 13 public function handle(Registered $event) 14 { 15 //$event->user->notify(new UserRegisterdNotifier); // 失敗 16 // 以下成功 17 Notification::route('mail', $event->user->email) 18 ->route('slack', config('app.slack_webhook_url')) 19 ->notify(new UserRegisterdNotifier); 20 } 21} 22

app/Notifications/UserRegisterdNotifier.php

PHP

1<?php 2 3namespace App\Notifications; 4 5use Illuminate\Bus\Queueable; 6use Illuminate\Contracts\Queue\ShouldQueue; 7use Illuminate\Notifications\Messages\MailMessage; 8use Illuminate\Notifications\Messages\SlackMessage; 9use Illuminate\Notifications\Notification; 10use App\Models\User; 11 12class UserRegisterdNotifier extends Notification implements ShouldQueue 13{ 14 use Queueable; 15 16 public function via($notifiable) 17 { 18 return ['mail', 'slack']; 19 } 20 21 public function toMail($notifiable) 22 { 23 return (new MailMessage) 24 ->subject('[' . config('app.name') . '] ユーザー登録ありがとうございます') 25 ->greeting('ユーザー登録ありがとうございます') 26 ->line('引き続きよろしくお願い致します。'); 27 } 28 29 public function toSlack($notifiable) 30 { 31 if (!property_exists($notifiable, 'id')) { 32 $notifiable = User::orderBy('id', 'desc')->first(); // Notification ファザード用仮コード 33 } 34 return (new SlackMessage) 35 ->success() 36 ->content('ユーザー登録されました') 37 ->attachment(function ($attachment) use ($notifiable) { 38 $attachment->title($notifiable->username, url('/admin/users/' . $notifiable->id)) 39 ->content('Laravel Admin'); 40 }); 41 } 42}

コードは内容を一部省略しています。

状況が分かりづらく恐縮ですが、アドバイスいただけると助かります。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

結論から先にお伝えすると、無事解決いたしました。

原因ですが User モデルにて getKey() を追加実装していたものの、
ワーカー実行時に紐付け処理が行われることが考慮されておらず、User が復元できていなかったようです。

ワーカー実行中に適切に紐付けが行われるよう改善を行い、
キューでの非同期通知の場合でも Notifiable トレイトを用いた notify() を実現する事が出来ました。

気にしてくださったみなさまありがとうございました。

別の手段で質問を並行して行っていましたが、
「Queue から User が復元が出来ていないのでは」というご指摘があり、なんとか気づくことが出来ました。
ご指摘下さった方、大変ありがとうございました。

なお、具体的な実装手順について触れることを検討しましたが、
元々の処理とは別の箇所での対応が多いこと、
実装が込み入っていることから、今回は割愛させて下さい。

投稿2021/04/20 06:35

sola

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問