I have a problem with one of my Jobs laravel. (This is the first time that i exepect this problem, i have anothers jobs who is working without problems...)
I have a listener who dispatch a job when one model is updating, so :
The job is executed but he go to failed jobs in my database with error "ErrorException: Undefined variable: ...." ...
It is very strange because all the variable was defined, and why does it work once in two?
If someone have an idea about that, i will take it :D
Thanks in advance!
Code of the Job :
<?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;
}
}
The complete exception :
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}
I found the problem, you always need to restart the queue when you done a modification in your job ...
So execute : php artisan queue:restart
each time that you edit your job.