@@ -11,6 +11,7 @@ import type { Config } from "../config/index.js";
1111import { DI } from "../di.js" ;
1212import type { ILogger } from "../log/index.js" ;
1313import { Logger } from "../log/index.js" ;
14+ import { maxStatusCode , type StatusCode } from "../utils/index.js" ;
1415import version from "../version.js" ;
1516import type Client from "./Client.js" ;
1617import type { Scanner } from "./Scanner.js" ;
@@ -47,39 +48,10 @@ export default class HealthCheckerService {
4748 }
4849
4950 const server = createServer ( async ( req , res ) => {
50- const timestamp = Number ( this . sdk . timestamp ) ;
51- const now = Math . ceil ( Date . now ( ) / 1000 ) ;
52- const threshold = this . config . staleBlockThreshold ;
5351 // Routing
5452 if ( req . url === "/" ) {
5553 res . writeHead ( 200 , { "Content-Type" : "application/json" } ) ;
56- res . end (
57- json_stringify ( {
58- startTime : this . #start,
59- version,
60- network : this . config . network ,
61- family : "liquidators" ,
62- liquidationMode : this . config . liquidationMode ,
63- address : this . client . address ,
64- balance : this . client . balance ,
65- currentBlock : this . sdk . currentBlock ,
66- timestamp : {
67- value : timestamp ,
68- healthy : ! ! threshold && now - timestamp <= threshold ,
69- } ,
70- marketsConfigurators :
71- this . sdk . marketRegister . marketConfigurators . map ( mc => mc . address ) ,
72- pools : this . sdk . marketRegister . pools . map ( p => p . pool . address ) ,
73- creditManagers : this . sdk . marketRegister . creditManagers . map (
74- cm => cm . creditManager . address ,
75- ) ,
76- providers : (
77- this . sdk . provider . publicClient as PublicClient <
78- Transport < "revolver" , RevolverTransportValue >
79- >
80- ) . transport . statuses ( ) ,
81- } ) ,
82- ) ;
54+ res . end ( json_stringify ( this . #healthStatus) ) ;
8355 } else if ( req . url === "/metrics" ) {
8456 try {
8557 res . writeHead ( 200 , { "Content-Type" : "text/plain" } ) ;
@@ -106,6 +78,54 @@ export default class HealthCheckerService {
10678 this . log . info ( "launched" ) ;
10779 }
10880
81+ get #healthStatus( ) {
82+ const timestamp = Number ( this . sdk . timestamp ) ;
83+ const now = Math . ceil ( Date . now ( ) / 1000 ) ;
84+ const threshold = this . config . staleBlockThreshold ;
85+ const result = {
86+ status : "healthy" as StatusCode ,
87+ startTime : this . #start,
88+ version,
89+ network : this . config . network ,
90+ family : "liquidators" ,
91+ liquidationMode : this . config . liquidationMode ,
92+ address : this . client . address ,
93+ balance : this . client . balance ,
94+ currentBlock : this . sdk . currentBlock ,
95+ timestamp : {
96+ value : timestamp ,
97+ status : ( ! ! threshold && now - timestamp <= threshold
98+ ? "healthy"
99+ : "alert" ) as StatusCode ,
100+ } ,
101+ marketsConfigurators : this . sdk . marketRegister . marketConfigurators . map (
102+ mc => mc . address ,
103+ ) ,
104+ pools : this . sdk . marketRegister . pools . map ( p => p . pool . address ) ,
105+ creditManagers : this . sdk . marketRegister . creditManagers . map (
106+ cm => cm . creditManager . address ,
107+ ) ,
108+ liquidatableAccounts : {
109+ value : this . scanner . liquidatableAccounts ,
110+ status : ( this . scanner . liquidatableAccounts > 0
111+ ? "alert"
112+ : "healthy" ) as StatusCode ,
113+ } ,
114+ providers : (
115+ this . sdk . provider . publicClient as PublicClient <
116+ Transport < "revolver" , RevolverTransportValue >
117+ >
118+ ) . transport . statuses ( ) ,
119+ } ;
120+ result . status = maxStatusCode (
121+ result . status ,
122+ result . timestamp . status ,
123+ result . balance ?. status ,
124+ result . liquidatableAccounts . status ,
125+ ) ;
126+ return result ;
127+ }
128+
109129 public async stop ( ) : Promise < void > {
110130 this . log . info ( "stopping" ) ;
111131 return new Promise ( resolve => {
0 commit comments