skip to Main Content

On one of the systems that I take care of, some times some jobs don’t get dispatched due to a connection problem with Redis and this ends up returning an error to the user, on our side we can ignore this error and just miss this job, I looked for how to deal with it on Google and I didn’t find anything about it.

public function sendMessage(Request $request, Model $model)
{
    // Do the necessary stuff
    
    ResolveMessageBilling::dispatch($model, $request->all());

    return response()->json([
        'message' => 'The message was succesfully sent'
    ], 200);
}

This is the error we are getting: RedisException - socket error on read socket

How to ignore the error if it occurs? A simple try/catch can resolve the issue?

public function sendMessage(Request $request, Model $model)
{
    // Do the necessary stuff

    try {
        ResolveMessageBilling::dispatch($model, $request->all());
    } catch(Exception $e) {}

    return response()->json([
        'message' => 'The message was succesfully sent'
    ], 200);
}

2

Answers


  1. If you want to bypass ANY error, you should use Throwable instead of Exception

    public function sendMessage(Request $request, Model $model)
    {
        // Do the necessary stuff
    
        try {
            ResolveMessageBilling::dispatch($model, $request->all());
        } catch(Throwable $e) {}
    
        return response()->json([
            'message' => 'The message was succesfully sent'
        ], 200);
    }
    

    see Error Hierarchy: https://www.php.net/manual/en/language.errors.php7.php

    If you want to bypass only the RedisException, you should be able to use:

    public function sendMessage(Request $request, Model $model)
    {
        // Do the necessary stuff
    
        try {
            ResolveMessageBilling::dispatch($model, $request->all());
        } catch(RedisException $e) {}
    
        return response()->json([
            'message' => 'The message was succesfully sent'
        ], 200);
    }
    
    Login or Signup to reply.
  2. If you don’t want to setup Redis just want to fixed/remove errors only, follow this article: https://laravel.com/docs/7.x/errors

    IF you want to Setup Redis(config -> detabase.php) properly, follow few step like this:

    'redis' => [
    
        'client' => 'predis',
    
        // Keep Default as is you want to use both redis and sentinel for different service(cache, queue)'
        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],
    
        // Create a custom connection to use redis sentinel
        'cache_sentinel' => [
            // Set the Sentinel Host from Environment (optinal you can hardcode if want to use in prod only)
            env('CACHE_REDIS_SENTINEL_1'),
            env('CACHE_REDIS_SENTINEL_2'),
            env('CACHE_REDIS_SENTINEL_3'),
            'options' => [
                'replication' => 'sentinel',
                'service' => 'cachemaster'),
                'parameters' => [
                    'password' => env('REDIS_PASSWORD', null),
                    'database' => 0,
                ],
            ],
        ],
    ],
    

    if you needs to Redis sentinal cache, can create new cache connection to use the above sentinal connection like this:

    ‘stores’ = [

    //Default config
    'redis' => [
        'driver'     => 'redis',
        'connection' => 'default',
    ],
    
    // Custom cache connection(according to you)
    'sentinel_redis' => [
        'driver'     => 'redis',
        'connection' => 'cache_sentinel',
    ],
    

    In laravel app, you can easily use via cache facade:

    Cache::store('sentinel_redis')->get('key');
    

    After config Redis properly test again with the clear server cache

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search