Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: health check job ( main ) #345

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading