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(EthereumRegistryWriter): health endpoint #956

Merged
merged 2 commits into from
Dec 4, 2019
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
2 changes: 2 additions & 0 deletions src/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface Configuration extends LoggingConfiguration, BitcoinRPCConfigura
readonly bitcoinFeeEstimateMode: 'CONSERVATIVE' | 'ECONOMICAL'
readonly bitcoinFeeRate: number

readonly ethereumRegistryWriterApiPort: number
readonly ethereumRpcUrl: string
readonly ethereumChainId: number
readonly ethereumRegistryContractAddress: string
Expand Down Expand Up @@ -137,6 +138,7 @@ export const DefaultConfiguration: Configuration = {

forceBlockHeight: undefined,

ethereumRegistryWriterApiPort: 18081,
ethereumRpcUrl: 'http://localhost:8545',
ethereumChainId: 4,
ethereumRegistryContractAddress: '',
Expand Down
10 changes: 10 additions & 0 deletions src/EthereumRegistryWriter/Business.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ export interface Arguments {
readonly configuration: Configuration
}

export interface Health {
readonly healthy: boolean
}

export interface Business {
readonly getHealth: () => Promise<Health>
readonly createDbIndices: () => Promise<void>
readonly insertClaimIdFilePair: (claimId: string, claimFile: string) => Promise<void>
readonly setBatchDirectory: (claimFiles: ReadonlyArray<string>, ipfsDirectoryHash: string) => Promise<void>
Expand Down Expand Up @@ -83,6 +88,10 @@ export const Business = ({
const businessLogger: Pino.Logger = childWithFileName(logger, __filename)
const claimAnchorReceiptsCollection: Collection<DbEntry> = db.collection('claimAnchorReceipts')

const getHealth = async () => ({
healthy: true,
})

const createDbIndices = async () => {
await claimAnchorReceiptsCollection.createIndex({ claimId: 1 }, { unique: true })
await claimAnchorReceiptsCollection.createIndex({ claimFile: 1 }, { unique: true })
Expand Down Expand Up @@ -351,6 +360,7 @@ export const Business = ({
}

return {
getHealth,
createDbIndices,
insertClaimIdFilePair,
setBatchDirectory,
Expand Down
7 changes: 6 additions & 1 deletion src/EthereumRegistryWriter/EthereumRegistryWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Router } from './Router'
import { Scheduler } from './Scheduler'

export interface EthereumRegistryWriterConfiguration extends LoggingConfiguration {
readonly apiPort: number
readonly mongodbUrl: string
readonly rabbitmqUrl: string
readonly exchanges: ExchangeConfiguration
Expand Down Expand Up @@ -74,7 +75,10 @@ export const EthereumRegistryWriter = async (configuration: EthereumRegistryWrit
business,
ethereumRegistryContract,
},
exchange: configuration.exchanges,
configuration: {
apiPort: configuration.apiPort,
exchange: configuration.exchanges,
},
})
await router.start()

Expand All @@ -95,6 +99,7 @@ export const EthereumRegistryWriter = async (configuration: EthereumRegistryWrit
return async () => {
logger.info('Stopping EthereumRegistryWriter...')
await scheduler.stop()
await router.stop()
logger.debug('Stopping EthereumRegistryWriter Messaging...')
await messaging.stop()
logger.info('EthereumRegistryWriter Messaging Stopped')
Expand Down
38 changes: 36 additions & 2 deletions src/EthereumRegistryWriter/Router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Server } from 'http'
import Koa from 'koa'
import KoaRouter from 'koa-router'
import Pino from 'pino'
import { promisify } from 'util'

import { EthereumRegistryContract } from 'Helpers/EthereumRegistryContract'
import { childWithFileName } from 'Helpers/Logging'
Expand All @@ -9,6 +13,11 @@ import { Messaging } from 'Messaging/Messaging'
import { Business } from './Business'
import { ExchangeConfiguration } from './ExchangeConfiguration'

export interface Configuration {
readonly apiPort: number
readonly exchange: ExchangeConfiguration
}

export interface Dependencies {
readonly logger: Pino.Logger
readonly messaging: Messaging
Expand All @@ -18,11 +27,12 @@ export interface Dependencies {

export interface Arguments {
readonly dependencies: Dependencies
readonly exchange: ExchangeConfiguration
readonly configuration: Configuration
}

export interface Router {
readonly start: () => Promise<void>
readonly stop: () => Promise<void>
}

export const Router = ({
Expand All @@ -32,9 +42,15 @@ export const Router = ({
business,
ethereumRegistryContract,
},
exchange,
configuration: {
apiPort,
exchange,
},
}: Arguments): Router => {
const routerLogger = childWithFileName(logger, __filename)
const koa = new Koa()
const koaRouter = new KoaRouter()
let server: Server

const start = async () => {
await messaging.consume(exchange.claimIpfsHash, onClaimIPFSHash)
Expand All @@ -61,6 +77,15 @@ export const Router = ({
.on('error', (error: any) => {
logger.error({ error }, 'onCidAdded error')
})

server = koa.listen(apiPort, '0.0.0.0')
}

const stop = async () => {
routerLogger.debug('Stopping API Router...')
server.unref()
await promisify(server.close.bind(server))
routerLogger.info('API Router stopped')
}

const onClaimIPFSHash = async (message: any) => {
Expand Down Expand Up @@ -152,7 +177,16 @@ export const Router = ({
}
}

const getHealth = async (context: KoaRouter.IRouterContext, next: () => Promise<any>) => {
context.body = await business.getHealth()
}

koaRouter.get('/health', getHealth)
koa.use(koaRouter.allowedMethods())
koa.use(koaRouter.routes())

return {
start,
stop,
}
}
1 change: 1 addition & 0 deletions src/EthereumRegistryWriter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const logger: Pino.Logger = Pino({
EthereumRegistryWriter({
loggingLevel: configuration.loggingLevel,
loggingPretty: configuration.loggingPretty,
apiPort: configuration.ethereumRegistryWriterApiPort,
mongodbUrl: configuration.mongodbUrl,
rabbitmqUrl: configuration.rabbitmqUrl,
ipfs: {
Expand Down