Skip to content

Commit

Permalink
add health check support
Browse files Browse the repository at this point in the history
  • Loading branch information
haseebrabbani committed Oct 20, 2023
1 parent 72b58e5 commit 7672f90
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
15 changes: 8 additions & 7 deletions cli/commands/run/server/agent.controller.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { GetAgentHandlers } from "../../../utils/get.agent.handlers";

export = AgentController
export = AgentController;

declare class AgentController {
constructor(getAgentHandlers: GetAgentHandlers)
Initialize(call: any, callback: any): void
EvaluateBlock(call: any, callback: any): void
EvaluateTx(call: any, callback: any): void
EvaluateAlert(call: any, callback: any): void
}
constructor(getAgentHandlers: GetAgentHandlers);
Initialize(call: any, callback: any): void;
EvaluateBlock(call: any, callback: any): void;
EvaluateTx(call: any, callback: any): void;
EvaluateAlert(call: any, callback: any): void;
HealthCheck(call: any, callback: any): void;
}
28 changes: 27 additions & 1 deletion cli/commands/run/server/agent.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ module.exports = class AgentController {
this.initializeResponse = {};
}

async HealthCheck(call, callback) {
let errors = [];
let status = "SUCCESS";

if (this.healthCheck) {
try {
const response = await this.healthCheck();
if (response && response.length > 0) {
status = "ERROR";
errors = response.map((r) => ({ message: r }));
}
} catch (e) {
console.log(`${new Date().toISOString()} healthCheck`);
console.log(e);
status = "ERROR";
errors = [{ message: e.message }];
}
}

callback(null, {
status,
errors,
});
}

async Initialize(call, callback) {
let status = "SUCCESS";

Expand All @@ -35,7 +60,7 @@ module.exports = class AgentController {
}

callback(null, {
status: status,
status,
alertConfig: this.initializeResponse
? this.initializeResponse.alertConfig
: undefined,
Expand Down Expand Up @@ -152,6 +177,7 @@ module.exports = class AgentController {
this.handleBlock = agentHandlers.handleBlock;
this.handleTransaction = agentHandlers.handleTransaction;
this.handleAlert = agentHandlers.handleAlert;
this.healthCheck = agentHandlers.healthCheck;
} catch (e) {
console.log(e);
}
Expand Down
3 changes: 2 additions & 1 deletion cli/utils/get.agent.handlers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { HandleBlock, HandleTransaction, HandleAlert, Initialize, InitializeResponse } from "../../sdk"
import { HandleBlock, HandleTransaction, HandleAlert, Initialize, InitializeResponse, HealthCheck } from "../../sdk"
import { assertExists, assertIsNonEmptyString } from "."
import { GetPythonAgentHandlers } from './get.python.agent.handlers'

type AgentHandlers = {
initialize?: Initialize,
initializeResponse?: InitializeResponse | void,
healthCheck?: HealthCheck,
handleTransaction?: HandleTransaction,
handleBlock?: HandleBlock,
handleAlert?: HandleAlert,
Expand Down
1 change: 1 addition & 0 deletions sdk/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { InitializeResponse } from "./initialize.response"
import { TransactionEvent } from "./transaction.event"

export type Initialize = () => Promise<InitializeResponse | void>
export type HealthCheck = () => Promise<string[] | void>
export type HandleTransaction = (txEvent: TransactionEvent) => Promise<Finding[]>
export type HandleBlock = (blockEvent: BlockEvent) => Promise<Finding[]>
export type HandleAlert = (alertEvent: AlertEvent) => Promise<Finding[]>
2 changes: 2 additions & 0 deletions sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
HandleTransaction,
HandleBlock,
HandleAlert,
HealthCheck
} from "./handlers";

interface DiContainer {
Expand All @@ -77,6 +78,7 @@ export {
HandleTransaction,
HandleBlock,
HandleAlert,
HealthCheck,
Finding,
FindingSeverity,
FindingType,
Expand Down

0 comments on commit 7672f90

Please sign in to comment.