send Blog subcriber with Laravel Queue Email

Posted 1 year ago by mlazuardy

I want to send when my Blog already been updated to "PUBLISHED", so i create the mailable like this

<?php

namespace App\Mail;

use App\User;
use App\Blog;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendBlogNotification extends Mailable
{
    use Queueable, SerializesModels;
    public $blog;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($blog)
    {
    
        $this->blog = $blog;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('new Blog has been added')
        ->view('email.blognotification');
    }
}

and this mail is control by update method in controller named "StaffController" like this

    public function blogPublish($slug)
    {
        $blog = Blog::where('slug',$slug)->firstOrFail();
        $users = UserNotification::where('blog', 1)->get();
        $blog->status = "PUBLISHED";
        $blog->save();
        if($users){
            foreach ($users as $user) {
                Mail::to($user->user->email)->queue(new SendBlogNotification($blog));
            }
        }
        return redirect('dashboard/blog')->with('success','Blog Telah di Publish');
    }

UserNotification is 1 to 1 relationship between user and the subcribe page, when the UserNotification->blog is true, it will send the blog for blog subcriber,

for information, i already set up queue and supervisor like queue doc in laravel site ( im using ubuntu)

but when im updating blog to "published" the some of queue is failed call "SendBlogNotification has been attempted too many times or run too long. The job may have previously timed out"

what's wrong with this?

Please sign in or create an account to participate in this conversation.