Commit 9ecbf3e5 authored by smartcontracts's avatar smartcontracts Committed by GitHub

feat(cmn): BaseServiceV2 exposes internal options (#2688)

BaseServiceV2 will expose internal options (loop interval, metrics port,
etc) as environment variables or command line options.
parent 552cd641
---
'@eth-optimism/common-ts': patch
---
Expose service internal options as environment or cli options
......@@ -11,11 +11,18 @@ import prometheus, { Registry } from 'prom-client'
import { Logger } from '../common/logger'
import { Metric } from './metrics'
import { validators } from './validators'
export type Options = {
[key: string]: any
}
export type StandardOptions = {
loopIntervalMs?: number
metricsServerPort?: number
metricsServerHostname?: string
}
export type OptionsSpec<TOptions extends Options> = {
[P in keyof Required<TOptions>]: {
validator: (spec?: Spec<TOptions[P]>) => ValidatorSpec<TOptions[P]>
......@@ -77,7 +84,7 @@ export abstract class BaseServiceV2<
/**
* Service options.
*/
protected readonly options: TOptions
protected readonly options: TOptions & StandardOptions
/**
* Metrics.
......@@ -129,10 +136,30 @@ export abstract class BaseServiceV2<
metricsServerHostname?: string
}) {
this.loop = params.loop !== undefined ? params.loop : true
this.loopIntervalMs =
params.loopIntervalMs !== undefined ? params.loopIntervalMs : 0
this.state = {} as TServiceState
// Add default options to options spec.
;(params.optionsSpec as any) = {
...(params.optionsSpec || {}),
// Users cannot set these options.
loopIntervalMs: {
validator: validators.num,
desc: 'Loop interval in milliseconds',
default: params.loopIntervalMs || 0,
},
metricsServerPort: {
validator: validators.num,
desc: 'Port for the metrics server',
default: params.metricsServerPort || 7300,
},
metricsServerHostname: {
validator: validators.str,
desc: 'Hostname for the metrics server',
default: params.metricsServerHostname || '0.0.0.0',
},
}
/**
* Special snake_case function which accounts for the common strings "L1" and "L2" which would
* normally be split into "L_1" and "L_2" by the snake_case function.
......@@ -241,9 +268,11 @@ export abstract class BaseServiceV2<
// Create the metrics server.
this.metricsRegistry = prometheus.register
this.metricsServerPort = params.metricsServerPort || 7300
this.metricsServerHostname = params.metricsServerHostname || '0.0.0.0'
this.metricsServerPort = this.options.metricsServerPort
this.metricsServerHostname = this.options.metricsServerHostname
// Set up everything else.
this.loopIntervalMs = this.options.loopIntervalMs
this.logger = new Logger({ name: params.name })
// Gracefully handle stop signals.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment