Thursday, March 16, 2017

Class not found exception in job (queue)

We want to use laravel jobs for sending out invoices to customers. We have this job class. It expects the payment model in the constructor.

namespace App\Jobs;

use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendInvoiceEmail extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    public $paypalModel;

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

    /**
     * The number of times the job may be attempted.
     *
     * @var int
     */
    public $tries = 5;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $rechnung = new \App\Classes\Rechnung\Rechnung($this->paypalModel);
        $rechnung->platzhalterErsetzen();
        $rechnung->pdfErzeugen();
        \App\Classes\Mail::SendPHPMail([
                                        'email'=> $this->paypalModel->benutzer->email,
                                        'name' => $this->paypalModel->benutzer->vorname . ' ' .$this->paypalModel->benutzer->nachname,
                                        'type' => 'SendInvoice',
                                        'invoicePath'=> $rechnung->pdfTargetPath
                                       ]);
        $this->paypalModel->rechnungGesendet = true;
        $this->paypalModel->save();
    }
}

If we set the queue_driver in the env to sync it works perfectly. Now if we change the queue_driver to database and start a listener on the jobs, we always get this exception:

[2017-03-15 13:20:13] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Class 'app\Models\Payment\PayPal\PayPal' not found in d:\project\vendor\laravel\framework\src\Illuminate\Queue\SerializesModels.php:76 Stack trace:
#0 d:\project\vendor\laravel\framework\src\Illuminate\Queue\SerializesModels.php(42): App\Jobs\SendInvoiceEmail->getRestoredPropertyValue(Object(Illuminate\Contracts\Database\ModelIdentifier))
#1 [internal function]: App\Jobs\SendInvoiceEmail->__wakeup()
#2 d:\project\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(38): unserialize('O:25:"App\\Jobs\\...')
#3 d:\project\vendor\laravel\framework\src\Illuminate\Queue\Jobs\Job.php(130): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
#4 d:\project\vendor\laravel\framework\src\Illuminate\Queue\Jobs\DatabaseJob.php(49): Illuminate\Queue\Jobs\Job->resolveAndFire(Array)
#5 d:\project\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(213): Illuminate\Queue\Jobs\DatabaseJob->fire()
#6 d:\project\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(156): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), '0', '0')
#7 d:\project\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(125): Illuminate\Queue\Worker->pop(NULL, 'rechnung', '0', '3', '0')
#8 d:\project\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(78): Illuminate\Queue\Console\WorkCommand->runWorker(NULL, 'rechnung', '0', '128', false)
#9 [internal function]: Illuminate\Queue\Console\WorkCommand->fire()
#10 d:\project\vendor\laravel\framework\src\Illuminate\Container\Container.php(507): call_user_func_array(Array, Array)
#11 d:\project\vendor\laravel\framework\src\Illuminate\Console\Command.php(169): Illuminate\Container\Container->call(Array)
#12 d:\project\vendor\symfony\console\Command\Command.php(267): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#13 d:\project\vendor\laravel\framework\src\Illuminate\Console\Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#14 d:\project\vendor\symfony\console\Application.php(846): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#15 d:\project\vendor\symfony\console\Application.php(191): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 d:\project\vendor\symfony\console\Application.php(122): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 d:\project\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(107): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 d:\project\artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 {main}

Why does the listener not know the class and when running in sync mode, it works as expected? Does anybody have a hint, what the reason can be and where to search for the error reason? Thanks in advance!

UPDATE

I've added as proposed the class app\Models\Payment\PayPal\PayPal:

...
namespace App\Jobs;

use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use app\Models\Payment\PayPal\PayPal;

class SendInvoiceEmail extends Job implements ShouldQueue
{
...

This did not solve the problem. Still the same error message appears. Even when I delete old created jobs and did a queue:restart to load the new code. Any other hint?



via dns_nx

Advertisement