PassinateDev
PassinateDev

Reputation: 51

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'),

Upvotes: 4

Views: 5834

Answers (2)

Brian Kisese
Brian Kisese

Reputation: 1

You could try clearing your cache as advised above. Additionally, also try to restart the queue

php artisan queue:restart

As for the queue:work question. Yes if you want the queue to run permanently supervisor is a good option to use. A sample config is as below.

[program:my-laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/project/artisan queue:work --queue=QUEUE_NAME--sleep=1 --tries=1
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/log/file.log

Upvotes: 0

Snapey
Snapey

Reputation: 4110

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.

Upvotes: 1

Related Questions