import * as IgniteClient from 'apache-ignite-client' ; import { Injectable } from '@nestjs/common' ; import { ConfigService } from '@fc/config' ; import { LoggerService } from '@fc/logger' ; import { ApacheIgniteConfig } from './dto' ; import { ApacheIgniteInvalidSocketException } from './exceptions' ; const { ObjectType , IgniteClientConfiguration , STATE } = IgniteClient ; @ Injectable ( ) export class ApacheIgniteService { private igniteClient : IgniteClient ; private static IgniteClientProxy = IgniteClient ; private static IgniteClientConfigurationProxy = IgniteClientConfiguration ; /**
     * Instantiate an apache ignite client and bind a callback to call when the
     * state change
     */ constructor ( private readonly config , private readonly logger ) { this . logger . setContext ( this . constructor . name ) ; this . igniteClient = new ApacheIgniteService . IgniteClientProxy ( this . onStateChanged . bind ( this ) ) ; } /**
     * Connect to the apache ignite server defined in the configuration and the
     * client initialized by the constructor
     */ async onModuleInit ( ) { const { endpoint , socketKeepAlive } = this . config . get < ApacheIgniteConfig > ( 'ApacheIgnite' ) ; this . logger . debug ( 'Connecting to apache-ignite cache...' ) ; await this . igniteClient . connect ( new ApacheIgniteService . IgniteClientConfigurationProxy ( endpoint ) ) ; this . logger . debug ( `[setSocketKeepAlive]: enable = ${ socketKeepAlive . enable } ; initialDelay = ${ socketKeepAlive . initialDelay }` ) ; this . setSocketKeepAlive ( socketKeepAlive . enable , socketKeepAlive . initialDelay ) ; } returns async onModuleDestroy ( ) { return this . igniteClient . disconnect ( ) ; } param name param keyType param valueType returns getCache ( name , keyType = ObjectType . PRIMITIVE_TYPE . STRING , valueType = ObjectType . PRIMITIVE_TYPE . STRING ) { return this . igniteClient . getCache ( name ) . setKeyType ( keyType ) . setValueType ( valueType ) ; } param state param reason private onStateChanged ( state , reason ? ) { if ( state === STATE . CONNECTED ) { this . logger . debug ( 'Apache ignite client is connected' ) ; } else if ( state === STATE . DISCONNECTED ) { todo see https this . logger . debug ( 'Apache ignite client disconnected' ) ; if ( reason ) { this . logger . debug ( reason ) ; } } } param enable param initialDelay private setSocketKeepAlive ( enable , initialDelay ) { if ( ! this . igniteClient . _socket ) { throw new ApacheIgniteInvalidSocketException ( ) ; } this . igniteClient . _socket . _socket . _socket . setKeepAlive ( enable , initialDelay ) ; } }