@@ -18,47 +18,59 @@ class ApiHandler extends EventEmitter {
1818 static isConnected = false ;
1919 private healthCheckInProgress = false ;
2020 private _currentEndpoint ?: string ;
21-
21+ public upSince : number = Date . now ( ) ;
2222 constructor ( endpoints : string [ ] ) {
2323 super ( ) ;
2424 this . _endpoints = endpoints . sort ( ( ) => Math . random ( ) - 0.5 ) ;
25+ this . upSince = Date . now ( ) ;
2526 }
2627
27- async healthCheck ( ) {
28- try {
29- logger . info (
30- `Performing health check for WS Provider for rpc: ${ this . _currentEndpoint } ` ,
31- apiLabel ,
32- ) ;
33- this . healthCheckInProgress = true ;
34-
35- const chain = await this . _api ?. rpc . system . chain ( ) ;
28+ async healthCheck ( retries = 0 ) : Promise < boolean > {
29+ if ( retries < 50 ) {
30+ try {
31+ // logger.info(
32+ // `Performing health check for WS Provider for rpc: ${this._currentEndpoint} try: ${retries}`,
33+ // apiLabel,
34+ // );
35+ this . healthCheckInProgress = true ;
36+ let chain ;
37+
38+ const isConnected = this . _wsProvider ?. isConnected ;
39+ if ( isConnected ) {
40+ try {
41+ chain = await this . _api ?. rpc . system . chain ( ) ;
42+ } catch ( e ) {
43+ logger . error ( `Cannot query chain in health check. ${ e } ` , apiLabel ) ;
44+ }
45+ }
3646
37- if ( this . _wsProvider ?. isConnected && chain ) {
38- logger . info (
39- `All good. Connected to ${ this . _currentEndpoint } ` ,
47+ if ( isConnected && chain ) {
48+ // logger.info(
49+ // `All good. Connected to ${this._currentEndpoint}`,
50+ // apiLabel,
51+ // );
52+ this . healthCheckInProgress = false ;
53+ return true ;
54+ } else {
55+ await sleep ( API_PROVIDER_TIMEOUT ) ;
56+ logger . info ( `api still disconnected, disconnecting.` , apiLabel ) ;
57+ await this . _wsProvider ?. disconnect ( ) ;
58+ await this . getProvider ( this . _endpoints ) ;
59+ await this . getAPI ( ) ;
60+ return false ;
61+ }
62+ } catch ( e : unknown ) {
63+ const errorMessage =
64+ e instanceof Error ? e . message : "An unknown error occurred" ;
65+ logger . error (
66+ `Error in health check for WS Provider for rpc. ${ errorMessage } ` ,
4067 apiLabel ,
4168 ) ;
4269 this . healthCheckInProgress = false ;
43- return true ;
44- } else {
45- await sleep ( API_PROVIDER_TIMEOUT ) ;
46- logger . info ( `api still disconnected, disconnecting.` , apiLabel ) ;
47- await this . _wsProvider ?. disconnect ( ) ;
48- throw new Error (
49- `ERROR: rpc endpoint still disconnected after ${ API_PROVIDER_TIMEOUT } seconds.` ,
50- ) ;
70+ return await this . healthCheck ( retries ++ ) ;
5171 }
52- } catch ( e : unknown ) {
53- const errorMessage =
54- e instanceof Error ? e . message : "An unknown error occurred" ;
55- logger . error (
56- `Error in health check for WS Provider for rpc. ${ errorMessage } ` ,
57- apiLabel ,
58- ) ;
59- this . healthCheckInProgress = false ;
60- throw e ;
6172 }
73+ return false ;
6274 }
6375
6476 public currentEndpoint ( ) {
@@ -115,7 +127,7 @@ class ApiHandler extends EventEmitter {
115127 } ) ;
116128 }
117129
118- async getAPI ( retries : number ) : Promise < ApiPromise > {
130+ async getAPI ( retries = 0 ) : Promise < ApiPromise > {
119131 if ( this . _wsProvider && this . _api && this . _api ?. isConnected ) {
120132 return this . _api ;
121133 }
0 commit comments