import redis = require ( 'redis' ) ; import winston = require ( 'winston' ) ; import RedisClientError from './error/RedisClientError' ; let logger = winston . loggers . get ( 'default' ) ; let redisClient = global [ 'redisClient' ] ; function checkRedisClient ( ) { if ( ! redisClient ) { if ( global [ 'redisClient' ] ) { redisClient = global [ 'redisClient' ] ; } else { throw new RedisClientError ( "Redis not connected" ) ; } } } export async function pollRedisClient ( ) { if ( redisClient ) return redisClient ; function checkRedisClientTimeoutWithoutReject ( timeout ) { return new Promise < redis . RedisClient > ( function ( resolve , reject ) { setTimeout ( function ( ) { try { checkRedisClient ( ) ; resolve ( redisClient ) ; } catch ( err ) { } } , timeout ) ; } ) ; } function checkRedisClientTimeout ( timeout ) { return new Promise < redis . RedisClient > ( function ( resolve , reject ) { setTimeout ( function ( ) { try { checkRedisClient ( ) ; resolve ( redisClient ) ; } catch ( err ) { reject ( err ) ; } } , timeout ) ; } ) ; } return await Promise . race ( [ checkRedisClientTimeoutWithoutReject ( 100 ) , checkRedisClientTimeoutWithoutReject ( 200 ) , checkRedisClientTimeoutWithoutReject ( 500 ) , checkRedisClientTimeout ( 1000 ) ] ) ; } export function configGet ( key ) { checkRedisClient ( ) ; return new Promise ( function ( resolve , reject ) { redisClient . sendCommand ( "config" , [ "get" , key ] , function ( err , values ) { if ( err ) { reject ( err ) ; } else { resolve ( values [ 1 ] ) ; } } ) ; } ) ; } export function flushall ( ) { checkRedisClient ( ) ; return new Promise ( function ( resolve , reject ) { redisClient . flushall ( function ( err , value ) { if ( err ) { reject ( err ) ; } else { resolve ( value ) ; } } ) ; } ) ; } export function get ( key ) { checkRedisClient ( ) ; return new Promise ( function ( resolve , reject ) { redisClient . get ( key , function ( err , value ) { if ( err ) { reject ( err ) ; } else { resolve ( value ) ; } } ) ; } ) ; } export function set ( key , value ) { checkRedisClient ( ) ; return new Promise ( function ( resolve , reject ) { redisClient . set ( key , value , function ( err ) { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } export function mget ( keyList ) { checkRedisClient ( ) ; return new Promise ( function ( resolve , reject ) { redisClient . mget ( ... keyList , function ( err , value ) { if ( err ) { reject ( err ) ; } else { resolve ( value ) ; } } ) ; } ) ; }