Как принудительно завершить работу Redis / ioredis, если хост не существует - PullRequest
0 голосов
/ 05 мая 2020

Можно ли запретить клиенту ioredis в NodeJS поиск, если хост не найден?

import {default as Redis} from 'ioredis'

interface ICacheProvider {
    host?: any;
}

interface CacheProviderProps {
    client: any;
}

class CacheProvider implements CacheProviderProps {
    client: any = null
    clientReady: boolean = false
    constructor(params?: ICacheProvider) {
        try {
            this.client = new Redis(params.host)
            this.client.ping((err: any, result: string) => {
                if (err) {
                    this.client.disconnect()
                }
                this.clientReady = result === 'PONG'
            })
        } catch (e) {
            this.client.disconnect()
            throw new Error('client not ready')
        }
    }

    isReady = (): boolean => this.clientReady
}

export const cache = new CacheProvider({ host: 'toto' })

Я бы хотел предотвратить что-то подобное в консоли:

[ioredis] Unhandled error event: Error: getaddrinfo ENOTFOUND toto
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)
[ioredis] Unhandled error event: Error: getaddrinfo ENOTFOUND toto
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)
[ioredis] Unhandled error event: Error: getaddrinfo ENOTFOUND toto
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)
[ioredis] Unhandled error event: Error: getaddrinfo ENOTFOUND toto
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)

Как динамически удалить этот процесс поиска, если кеш не существует или недоступен?

...