laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.46k stars 11.01k forks source link

Mail Queue with redis gives error The script tried to execute a method or access a property of an incomplete object #30627

Closed classicalguss closed 4 years ago

classicalguss commented 4 years ago

Description:

I am sending a simple email as per the documentation in Laravel 5.5

Mail::to($customer)
    ->queue(new WelcomeMail($customer));

Customer is a user object with an email field. The code for Welcome email is the following

<?php

namespace App\Mail;

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

class WelcomeMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public $user;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('security::emails.welcome');
    }
}

Here are the relevant env variables

CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
QUEUE_DRIVER=redis

The redis caching is otherwise working fine. The email gets sent sometimes, but most of the time the error is happening.

Steps To Reproduce:

Mail::to($customer)
    ->queue(new WelcomeMail($customer));

Here is the complete error Illuminate\Mail\SendQueuedMailable::handle(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "App\Mail\WelcomeMail" of the object you are trying to operate on was loaded before unserialize() gets called or provide an autoloader to load the class definition in /var/www/html/vendor/laravel/framework/src/Illuminate/Mail/SendQueuedMailable.php

Line: 52

I appreciate any help.

driesvints commented 4 years ago

Hey there,

Unfortunately we don't support this version anymore. Please check out our support policy on which versions we are currently supporting. Can you please try to upgrade to the latest version and see if your problem persists? We'll help you out and re-open this issue if so.

Thanks!

jicao commented 4 years ago

@classicalguss Did you find any solution to this problem ? I'm facing the exact same problem.

Thanks

classicalguss commented 4 years ago

@classicalguss Did you find any solution to this problem ? I'm facing the exact same problem.

Thanks

Our issue was that more than 1 queue from different servers and environments were accessing the same redis instance. So we had the same redis locally and on staging environments which was wrong. If you queued something locally and then tried to read it and serialize it from somewhere else, it'll result in this error for us. Hope you find your issue.

njoguamos commented 9 months ago

I had this problem. I had two Laravel applications sharing the same Redis instance in production.

I solved it by setting a unique' REDIS_PREFIX` for each application.