Job never dispacthed inside Redis Subscribe Command

  laravel, php, queue, redis
  • Laravel Version: v6.20.4
  • PHP Version: 7.4
  • Database Driver & Version: MySQL & 15.1
  • Queue Connection: Redis

Description:

I’m working on a command that subscribe to a Redis channel and should dispatch a job after receiving a message. But for some reason the job was never dispatched. I use Redis for my Queue connection.

Steps To Reproduce:

Here is the job. It should be really simple. The job will accept a message and logs the message.

<?php

namespace AppJobs;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;

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

    /**
     * Message
     *
     * @param string $message
     */ 
    public $message;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        logger($this->message);
    }
}

Here is my artisan command:

<?php

namespace AppConsoleCommands;

use AppJobsDebugRedisJob;
use IlluminateConsoleCommand;
use IlluminateSupportFacadesRedis;

class RedisLwtDebugCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'debug:redis_lwt';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Debug redis subscribe';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Redis::connection('default')->subscribe('debug', function ($message)
        {
            $this->info('Message received '.$message);

            // This job was never dispacthed on Redis Queue
            DebugRedisJob::dispatch($message);
        });
    }
}

Run the command: php artisan redis:debug_lwt on terminal. The next step is to use redis-cli to manually publish a message to debug channel.

redis-cli

127.0.0.1:6379> publish debug "some message"

The message will be received but the job DebugRedisJob was never dispacthed.
When i changed the queue connection to database or beanstalkd, the job was successfully dispatched.

Source: Ask PHP

LEAVE A COMMENT