11<?php
2+
23namespace App \Jobs ;
34
45use App \Mail \NewPostsNotification ;
56use App \Models \Post ;
67use App \Models \User ;
78use Carbon \Carbon ;
8- use Exception ;
99use Illuminate \Bus \Queueable ;
1010use Illuminate \Contracts \Queue \ShouldQueue ;
1111use Illuminate \Foundation \Bus \Dispatchable ;
1212use Illuminate \Queue \InteractsWithQueue ;
1313use Illuminate \Queue \SerializesModels ;
14- use Illuminate \Support \Facades \Log ;
1514use Illuminate \Support \Facades \Mail ;
1615
1716class SendPostDigestEmail implements ShouldQueue
1817{
1918 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2019
20+ public int $ tries = 3 ;
21+ public int $ timeout = 30 ;
22+ public array $ backoff = [60 , 180 , 300 ];
23+
2124 public function __construct (public User $ user )
2225 {
2326 }
2427
2528 public function handle (): void
2629 {
27- $ user = User:: find ( $ this ->user ->id );
30+ $ user = $ this ->user ->fresh ( );
2831
2932 if (!$ user || !$ user ->receives_notifications ) {
30- Log::info ("Skipping post digest for user {$ this ->user ->id } because they have unsubscribed or were deleted. " );
3133 return ;
3234 }
3335
@@ -41,14 +43,9 @@ public function handle(): void
4143 $ mainPost = $ newPosts ->sortByDesc ('total_votes ' )->first ();
4244 $ gridPosts = $ newPosts ->where ('id ' , '!= ' , $ mainPost ->id )->take (4 );
4345
44- try {
45- Mail::to ($ user )->send (new NewPostsNotification ($ user , $ mainPost , $ gridPosts ));
46+ Mail::to ($ user )->send (new NewPostsNotification ($ user , $ mainPost , $ gridPosts ));
4647
47- $ user ->last_notified_at = Carbon::now ();
48- $ user ->save ();
49- } catch (Exception $ e ) {
50- Log::error ("Failed to queue post digest for user {$ user ->id }: " . $ e ->getMessage ());
51- $ this ->release (300 );
52- }
48+ $ user ->last_notified_at = Carbon::now ();
49+ $ user ->save ();
5350 }
5451}
0 commit comments