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:
- first you have to set the queue driver in
.env
file not to besync
since if your job is delayed, it has to be queued. so sync option shouldn't be used
based on the queue driver (like database,redis, ...), you can make a queue.
- run a worker:
- for single thread run:
php artisan queue:work
or listen-aware worker on development :
php artisan queue:listen
for supervised queue
for multi-thread you can use laravel-horizon package