Search code examples
laravelqueuelaravel-10

Laravel 10: dispatchSync method throws an exception: Unresolvable dependency resolving [Parameter #0 [ <required> $app ]]


Upon dispatching job synchronously, it throws an exception:

Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\Support\ServiceProvider

This exception appears only if job fails. Removing "Queueable" trait fixes this issue, but I need to run this job both synchronously and in the queue.

In previous Laravel versions it worked.

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SomeJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private array $data;

    /**
     * Create a new job instance.
     *
     * @param array $data
     */
    public function __construct( array $data = [] )
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        // ...
        $this->fail();
    }
}

--

SomeJob::dispatch($data); // OK
SomeJob::dispatchSync($data) // Exception :((

Solution

  • I've figured out it. The reason is the following line in the AppServiceProvider class, boot method:

    Queue::failing([ AppServiceProvider::class, 'handleFailedJob' ]);
    

    Which refers to this method:

    public static function handleFailedJob(JobFailed $event)
    {
        // ..
    }