'Keeping laravel command always running
I wrote a command for Laravel inside App\Console. My goal in creating this command is to subscribe to channel Redis and work according to the commands I receive.
class RedisListener extends Command
{
protected $signature = 'redis:listen';
protected $description = 'Listen for events on redis channel';
CONST ExternalChannel = "RedisPrefixMainMicroServiceChannel" ;
CONST InternalChannel = "MainMicroServiceChannel" ;
public function handle()
{
Redis::subscribe([self::InternalChannel],function ( $message ){
/// TODO
});
}
}
And I run this command with the supervisor, and everything goes well.
[program:microRedis]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/api/laravel/artisan redis:listen
user=octane
autostart=true
autorestart=true
numprocs=1
stdout_logfile=/var/log/redis.log
stdout_logfile_maxbytes=1024
But I found a problem. This command that I put in the supervisor will be terminated after a minute, or let's consider an error in the application. This command will terminate when autostart = true. It wants to raise this command again for about a second. It takes time to subscribe again to channel Redis. Do you have any ideas for me to cover this for a second?
Solution 1:[1]
i found the issue , in /config/database.php command is a 'redis.default' config i duplicated config and addedd 'read_write_timeout' => 0 it means connection should not be terminated after client didnt recieved any message on subscription
/config/database.php
'redis-permanent-connection' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
'read_write_timeout' => 0 , // default is 60s in laravel 9
],
Redis::connection("redis-permanent-connection")->subscribe(["channel"],function ( $message ){
/// TODO
}) ;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | amir ranjbar |
