'Unhandled error event: Error: connect ECONNREFUSED - Unable to handle this error gracefully

I am looking for a solution where in I can gracefully handle redis connection issue. Suppose my redis service get down, what will be best way to handle the error. I tried to put my code in try catch but unfortunately redis is not throwing exception and unable to catch the exception in my nodejs code. I tried various setting in package like ioredis(i.e maxRetriesPerRequest: 0) or node-redis but failed. Can you please provide me some reference or help to handle connection issue gracefully?



Solution 1:[1]

try {
    const client = createClient({
        url: 'redis://redis.xyz.com>:6379'
    });
    client.on('error', (err) => console.log('Redis Client Error', err));
    await client.connect();
    await client.set('key', 'value');
    const value = await client.get('key');
    console.log("Value", value);
}
catch (err: any) {
     //Unable to reach here. Redis is not throwing error on redis connection issue
    //Want to call api to fetch info when server is down
}
    
In this, I want to gracefully handle connection timeout if redis connection fails and want that if connection fails than I want to fetch the info from the api in place of redis keep trying to connect to redis server if connection fails.In current settings, if connection fails, system keep trying to connnect to redis server and somewhat blocks the event loop in nodejs.Please suggest how can we avoid this.

Solution 2:[2]

First I don't see that your script is looping, so it runs once and finishes. Then I don't think the bit at the end is needed unless you are specifically wanting to be able to manually press 1, 2 or 3 and have them do something, but that was not mentioned as being your intention.

Try something like this:

stopit = 0
While stopit = 0
{
    Loop, 50
    {
        if stopit = 1
        {
            ExitApp
        }
        Random, Rand, 1, 3
        if (Rand == 1)
        {
             Send {1}
             Send {Enter} 
        }
        if (Rand == 2)
        {
             Send {2}
             Send {Enter} 
        }
        if (Rand == 3)
        {
             Send {3}
             Send {Enter} 
        }
        Sleep, 5000
    }
}
return


F10::
    stopit = 1
return

Esc::
    ExitApp

Solution 3:[3]

To repeat an action periodically (at a specified time interval) you need a timer or a Loop as in the other answer.

AFAIK SetTimer has higher performance than a Loop and consumes less resources.

#NoEnv
#SingleInstance Force
#UseHook

SetTimer, send_random_text, 10000 ; every 10 seconds
Return 

send_random_text: 
    Random, Rand, 1, 3
    if (Rand == 1)
        GoSub 1 ; jump to this hotkey label (1::)
    if (Rand == 2)
        GoSub 2
    if (Rand == 3)
        GoSub 3
Return

1::send, test 1{Enter} 
2::send, test 2{Enter}    
3::send, test 3{Enter} 

Esc::ExitApp

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 cedar
Solution 2 Don 'FreihEitner' Eitner
Solution 3 user3419297