Laravel Jobs(队列)不起作用

时间:2019-07-30 22:10:38

标签: php laravel laravel-5.8 laravel-queue laravel-jobs

我的一个Jobs laravel有问题。 (这是我第一次看到这个问题,我有另一个工作没有问题的工作...)

我有一个侦听器,当一个模型正在更新时,他会派遣工作,所以:

  • 一名模型观察员(正在工作)
  • 观察员发起一个事件(正在工作)
  • 一个监听上一个事件的监听器(正在运行)
  • 由听众分派的一个工作(不工作...)

该作业已执行,但是他转到我的数据库中失败的作业,错误为“ ErrorException:未定义的变量:....” ...

这很奇怪,因为所有变量都已定义,为什么它每两个工作一次?

如果有人对此有所了解,我会接受:D

谢谢!

工作代码:

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\Transaction;
use App\Repositories\TransactionRepository;
use App\Repositories\InvoiceModelRepository;
use App\Repositories\BalanceRepository;
use Scheme;
use Log;
use Config;
use App\Helpers\HelperNumber;
use Symfony\Component\Console\Output\ConsoleOutput;

/*
    sudo apt-get install supervisor
    https://laravel.com/docs/5.4/queues#supervisor-configuration
    php artisan queue:work --queue compute_fees --retry_after 30 --timeout 30
*/
class ComputeFees implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $id;

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

    /**
     * Get the best match of invoice models
     *
     * @param \App\Models\Transaction $transaction
     * @return \App\Models\InvoiceModel
     */
     private function getFees($transaction) {
         $invoiceModelRepository = new InvoiceModelRepository();

         //Get the fees for this site and payment type (And country if needed)
         $params = [
             'sites_id' => $transaction->site->id
         ];

          return $params;
     }

    /**
     * Execute the job.
     *
     * @return boolean
     */
    public function handle()
    {
        $output = new ConsoleOutput();

        //Init repos
        $transactionRepository = new TransactionRepository();
        $balanceRepository = new BalanceRepository();

        //Get the transaction
        $transaction = $transactionRepository->getBy(['id' => $this->id]);

        $output->writeln('tx : '.$transaction->id);

        return true;
    }
}

完整的例外:

ErrorException: Undefined variable: transaction in /var/www/testproject/app/Jobs/ComputeFees.php:53
Stack trace:
#0 /var/www/testproject/app/Jobs/ComputeFees.php(53): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined varia...', '/var/www/test....', 53, Array)
#1 /var/www/testproject/app/Jobs/ComputeFees.php(106): App\Jobs\ComputeFees->getFees()
#2 [internal function]: App\Jobs\ComputeFees->handle()
#3 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#4 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#5 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#6 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(580): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#7 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): Illuminate\Container\Container->call(Array)
#8 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(App\Jobs\ComputeFees))
#9 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(App\Jobs\ComputeFees))
#10 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#11 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(413): Illuminate\Bus\Dispatcher->dispatchNow(Object(App\Jobs\ComputeFees), false)
#12 /var/www/testproject/app/Listeners/ComputeFees.php(36): dispatch_now(Object(App\Jobs\ComputeFees))
#13 [internal function]: App\Listeners\ComputeFees->handle(Object(App\Events\TransactionChangeState))
#14 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Events/CallQueuedListener.php(86): call_user_func_array(Array, Array)
#15 [internal function]: Illuminate\Events\CallQueuedListener->handle(Object(Illuminate\Foundation\Application))
#16 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#17 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#18 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#19 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(580): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#20 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): Illuminate\Container\Container->call(Array)
#21 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(Illuminate\Events\CallQueuedListener))
#22 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Events\CallQueuedListener))
#23 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#24 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(49): Illuminate\Bus\Dispatcher->dispatchNow(Object(Illuminate\Events\CallQueuedListener), false)
#25 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(86): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
#26 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(327): Illuminate\Queue\Jobs\Job->fire()
#27 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(277): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions))
#28 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(118): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions))
#29 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(102): Illuminate\Queue\Worker->daemon('database', 'default', Object(Illuminate\Queue\WorkerOptions))
#30 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(86): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'default')
#31 [internal function]: Illuminate\Queue\Console\WorkCommand->handle()
#32 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#33 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#34 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#35 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(580): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#36 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\Container\Container->call(Array)
#37 /var/www/testproject/vendor/symfony/console/Command/Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#38 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#39 /var/www/testproject/vendor/symfony/console/Application.php(908): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#40 /var/www/testproject/vendor/symfony/console/Application.php(269): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#41 /var/www/testproject/vendor/symfony/console/Application.php(145): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#42 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Console/Application.php(90): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#43 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#44 /var/www/testproject/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#45 {main}

1 个答案:

答案 0 :(得分:0)

我发现了问题,在完成工作修改后,您总是需要重新启动队列...

因此,每次编辑作业时都要执行php artisan queue:restart