Home > Mobile >  Laravel Queued job execution with delay
Laravel Queued job execution with delay

Time:11-22

I am using Laravel queued jobs to simply create a copy of my tables in JSON format and delete the created file afterwards. Here is my handle method:

public function handle()
{
    unlink($this->file);
}

The code works fine as long as I dispatch the file right after response:

DeleteCreatedFiles::dispatchAfterResponse(/* File path */);

However I want to keep the file for some time before deleting so I used this code:

DeleteCreatedFiles::dispatch(/* file path */)
        ->delay(now()->addSeconds(30));

The trouble is that the job is never executed no matter how much I wait. I tried using the telescope and it keeps showing the pending status. Please help me figure out why this is happening. Thanks!

CodePudding user response:

  1. first you have to set the queue driver in .env file not to be sync

since if your job is delayed, it has to be queued. so sync option shouldn't be used

  1. then create a queue

based on the queue driver (like database,redis, ...), you can make a queue.

  1. run a worker:
  • for single thread run:
php artisan queue:work

or listen-aware worker on development :

php artisan queue:listen
  • Related