Skip to content

Commit

Permalink
feat: health check job ( main ) (#345)
Browse files Browse the repository at this point in the history
* feat: health check job

* feat: validator

* refactor: code
  • Loading branch information
phamphong9981 authored Sep 22, 2023
1 parent 7aa36bc commit 356db4d
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ci/config.json.ci
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@
"millisecondRepeatJob": 2000,
"blocksPerCall": 100
},
"servicesManager": {
"healthCheckLimit": 100
},
"crawlIbcApp": {
"key": "crawlIbcApp",
"millisecondRepeatJob": 2000,
Expand Down
3 changes: 3 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@
"millisecondRepeatJob": 2000,
"blocksPerCall": 100
},
"servicesManager": {
"healthCheckLimit": 100
},
"crawlIbcApp": {
"key": "crawlIbcApp",
"millisecondRepeatJob": 2000,
Expand Down
8 changes: 8 additions & 0 deletions src/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ export const SERVICE = {
path: 'v1.Cw20ReindexingService.reindexing',
},
},
ServicesManager: {
key: 'ServicesManager',
name: 'v1.ServicesManager',
HealthCheck: {
key: 'HealthCheck',
path: 'v1.ServicesManager.HealthCheck',
},
},
},
};

Expand Down
1 change: 1 addition & 0 deletions src/services/api-gateways/api_gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { bullBoardMixin } from '../../mixins/bullBoard/bullBoard.mixin';
'v2.graphql.*',
'v2.statistics.getDashboardStatisticsByChainId',
'v2.statistics.getTopAccountsByChainId',
'v1.services-manager.*',
],
},
{
Expand Down
51 changes: 51 additions & 0 deletions src/services/api-gateways/services-manager.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Get, Service } from '@ourparentcenter/moleculer-decorators-extended';
import { Context, ServiceBroker } from 'moleculer';
import networks from '../../../network.json' assert { type: 'json' };
import BaseService from '../../base/base.service';
import { BULL_JOB_NAME } from '../../common';

@Service({
name: 'services-manager',
version: 1,
})
export default class ServicesManagerService extends BaseService {
public constructor(public broker: ServiceBroker) {
super(broker);
}

@Get('/health-check', {
name: 'healthCheck',
params: {
chainid: {
type: 'string',
optional: false,
enum: networks.map((network) => network.chainId),
},
jobNames: {
type: 'array',
optional: false,
items: 'string',
enum: Object.values(BULL_JOB_NAME),
},
},
})
async healthCheck(
ctx: Context<
{
chainid: string;
jobNames: string[];
},
Record<string, unknown>
>
) {
const selectedChain = networks.find(
(network) => network.chainId === ctx.params.chainid
);
return this.broker.call(
`v1.ServicesManager.HealthCheck@${selectedChain?.moleculerNamespace}`,
{
jobNames: ctx.params.jobNames,
}
);
}
}
50 changes: 50 additions & 0 deletions src/services/manager/services-manager.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
Action,
Service,
} from '@ourparentcenter/moleculer-decorators-extended';
import { Context, ServiceBroker } from 'moleculer';
import config from '../../../config.json' assert { type: 'json' };
import BullableService from '../../base/bullable.service';
import { BULL_JOB_NAME, SERVICE } from '../../common';
import { BlockCheckpoint } from '../../models';

@Service({
name: SERVICE.V1.ServicesManager.key,
version: 1,
})
export default class ServicesManagerService extends BullableService {
public constructor(public broker: ServiceBroker) {
super(broker);
}

@Action({
name: SERVICE.V1.ServicesManager.HealthCheck.key,
params: {
jobNames: {
type: 'array',
optional: false,
items: 'string',
},
},
})
public async actionHealthCheck(ctx: Context<{ jobNames: string[] }>) {
const { jobNames } = ctx.params;
const result: Record<string, boolean> = {};
const jobBlock = await BlockCheckpoint.query()
.where('job_name', BULL_JOB_NAME.CRAWL_BLOCK)
.first()
.throwIfNotFound();
const jobsHeight = await BlockCheckpoint.query().whereIn(
'job_name',
jobNames
);
jobsHeight.forEach((job) => {
Object.assign(result, {
[job.job_name]:
jobBlock.height - job.height <
config.servicesManager.healthCheckLimit,
});
});
return result;
}
}

0 comments on commit 356db4d

Please sign in to comment.