Search code examples
laraveldelayed-jobdispatch

I can't dispatch delayed job in laravel


The code below doesn't work. I think I have done all things correctly, but somehow I doesn't work.

The job is dispatched like this:

MyJob::dispatch($job)->onQueue('processing')->delay(Carbon::now()->addSeconds(30));

MyJob.php

<?php

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

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

    public function __construct($job)
    {
        // I described a logging code here and yes, there was change, but then...
    }

    public function handle()
    {
        // I described a logging code here, but there wasn't change
    }
}

The problem is that dispatchNow() did work, but dispatch with delay didn't work.

I also set .env correctly(I guess)

.env file


    QUEUE_CONNECTION=database

config/queue.php


    'default' => env('QUEUE_CONNECTION', 'sync'),


Solution

  • Diagnostic steps;

    1. Open tinker and run config('queue') and check that the queue settings are as expected

    2. Without running a queue worker, dispatch your job to the queue. Open your database tools and check that the jobs table contains one new record.

    3. run the queue worker with php artisan queue:work after the delay, the job should run.

    4. Check that the job has gone from the jobs table, and that nothing is in the failed_jobs table.