import { EventSpewer , Timers } from 'detritus-utils' ; import { createClient , RedisClient } from 'redis' ; import * as Sentry from '@sentry/node' ; import { RedisChannels } from './constants' ; import { RedisPayloads } from './types' ; export class RedisSpewer extends EventSpewer { client ! : RedisClient ; ended : boolean = false ; url : string ; constructor ( url ) { super ( ) ; this . url = url ; this . client = createClient ( this . url , { retry_strategy : ( options ) => { return Math . min ( options . attempt * 1000 , 3000 ) ; } , } ) ; this . client . on ( 'error' , ( error ) => { Sentry . captureException ( error ) ; } ) ; this . client . on ( 'message' , ( channel , message ) => { if ( channel in RedisChannels ) { try { const data = ( message ) ? JSON . parse ( message ) : null ; this . emit ( channel , data ) ; } catch ( error ) { Sentry . captureException ( error ) ; } } } ) ; for ( let channel of Object . values ( RedisChannels ) ) { this . client . subscribe ( channel ) ; } } end ( ) { if ( ! this . ended && this . client ) { this . client . end ( true ) ; this . client . removeAllListeners ( ) ; this . ended = true ; } } on ( event , listener ) ; on ( event , listener ) ; on ( event , listener ) { super . on ( event , listener ) ; return this ; } } export default new RedisSpewer ( process . env . REDIS_URL || '' ) ;