Skip to content
Advertisement

Handle Method not being called in Laravel Redis Queue

When adding an item to the queue, for some reason the handle method is not being called.

The Log entry in __construct is appearing but when attempting to log in handle(), nothing appears.

The method i’m using to dispatch is ProcessImport::dispatch($path, $task->task_id);

My queue service is configured to use Redis, and redis is storing all the data accordingly.

Redis keys

I am using Laravel 8. What could be wrong?

<?php

namespace AppJobs;

use AppModelsTasks;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldBeUnique;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;
use AppHttpControllersProductsProducts;
use IlluminateSupportFacadesCache;
use IlluminateSupportFacadesLog;
use IlluminateSupportFacadesQueue;
use IlluminateQueueEventsJobProcessing;
use IlluminateQueueEventsJobProcessed;
use Throwable;
class ProcessImport implements ShouldQueue, ShouldBeUnique
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $file_path;
    protected $response;
    protected $task;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($path, $task_id)
    {
        Log::info("Importing products (construct)");
        $this->task = Tasks::where('task_id', $task_id)->first();
        $this->file_path = $path;
        Log::info('Importing ' . $path);
    }

    private function getFilePath() {
        return $this->file_path;
    }

    /**
     * Handle a job failure.
     *
     * @param  Throwable  $exception
     * @return void
     */
    public function failed(Throwable $exception)
    {
        $this->task->failed($exception->getMessage());
    }

    /**
     * Get the cache driver for the unique job lock.
     *
     * @return IlluminateContractsCacheRepository
     */
    public function uniqueVia()
    {
        return Cache::driver('redis');
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info("Importing products (handle)");
        $this->task->start();
        $products = new Products();
        $products->importProductsFromCSV($this->getFilePath());
        $this->task->success();
        Log::info("End of importing products..");
    }
}

Advertisement

Answer

You’ve just pushed the jobs onto the queue but haven’t started a worker to process them. You can run the worker with:

php artisan queue:work
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement