5

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 from @niraj-shah 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?

dns_nx
  • 3,651
  • 4
  • 37
  • 66

1 Answers1

0

Your code cannot find the PayPal class. Please add a use statement to the top of your code (after the other use statements)` pointing to the location of the PayPal class. E.g.

use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Models\Payment\PayPal\PayPal;
....
Niraj Shah
  • 15,087
  • 3
  • 41
  • 60
  • 1
    Sorry, your solution did not solve the problem. I added the class as you described. I did also a `queue:restart` command, deleted old jobs and created new payments to start without old data. The error message is still the same. – dns_nx Mar 15 '17 at 19:07
  • Add this (and check the path) to all files related to this job. – Niraj Shah Mar 15 '17 at 21:17
  • The main error is `Class 'app\Models\Payment\PayPal\PayPal' not found`. Do you need to add code to tell the file where the `PayPal` class can be found. Change the link to (capital a in App): `use App\Models\Payment\PayPal\PayPal;` – Niraj Shah Mar 17 '17 at 10:03