-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: health check job ( main ) (#345)
* feat: health check job * feat: validator * refactor: code
- Loading branch information
1 parent
7aa36bc
commit 356db4d
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |