I'm trying to dispatch jobs from a controller, but sometimes the queue works, and sometimes it will execute the code immediately. On the other hand, dispatching from a command works fine, so I wonder what the difference could be.
Things that I have tried:
vendor/laravel/framework/src/Illuminate/Foundation/Bus/Dispatchable.php
public static function dispatch()
{
die('xxx');
return new PendingDispatch(new static(...func_get_args()));
}
I'm trying to find what happened before the dispatch function, but I cannot find any function that executes before the static handle function. I have the feeling that something is crashing in PHP and alternatively execute the job immediately, but I cannot find where that happens in the core.
Controller
class CreateTestJob
{
public function __invoke(Request $request)
{
TestJob::dispatch(rand(0, 999999));
}
}
Job
class TestJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct($test)
{
$this->test = $test;
}
public function handle()
{
sleep(30);
echo 'done';
}
}
I also tried to create a syntax error in de handle function of the TestJob so that I can see the stack trace, but it shows "1 unknown frame". I wonder what that is
you can check fail_jobs table if there is any error while running your job.
The job will be immediately executed only when you use synchronous driver or synchronous dispatch(for use in local development).
<?php Somejob::dispatchSync();
Jobs on Redis and database connection will be executed only when you run queue worker.
php artisan queue:work