【问题标题】:Class not found exception in job (queue)在作业(队列)中找不到类异常
【发布时间】:2017-08-06 05:33:41
【问题描述】:

我们希望使用laravel 作业向客户发送发票。 我们有这个工作班。它期望构造函数中的支付模型。

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();
    }
}

如果我们将env 中的queue_driver 设置为sync,它将完美运行。现在,如果我们将 queue_driver 更改为 database 并在作业上启动一个侦听器,我们总是会得到这个异常:

[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}

为什么侦听器不知道类,并且在sync 模式下运行时,它按预期工作?有没有人有提示,可能是什么原因以及在哪里搜索错误原因? 提前致谢!


更新

我已按照@niraj-shah 的建议添加了课程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
{
...

这并没有解决问题。仍然出现相同的错误消息。即使我删除旧创建的作业并执行queue:restart 来加载新代码。还有其他提示吗?

【问题讨论】:

    标签: laravel laravel-5.2 queue jobs


    【解决方案1】:

    您的代码找不到PayPal 类。请在代码顶部添加一个use 语句(在其他use 语句之后)`,指向PayPal 类的位置。例如

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

    【讨论】:

    • 抱歉,您的解决方案没有解决问题。我按照您的描述添加了课程。我还做了一个queue:restart 命令,删除了旧工作并创建了新的付款以在没有旧数据的情况下开始。错误信息还是一样。
    • 将此(并检查路径)添加到与此作业相关的所有文件中。
    • 主要错误是Class 'app\Models\Payment\PayPal\PayPal' not found。您是否需要添加代码来告诉文件在哪里可以找到PayPal 类。将链接更改为(App 中的大写 a):use App\Models\Payment\PayPal\PayPal;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多