UNPKG

@mdf.js/service-registry

Version:

MMS - API - Service Registry

156 lines 7.1 kB
/** * Copyright 2024 Mytra Control S.L. All rights reserved. * * Use of this source code is governed by an MIT-style license that can be found in the LICENSE file * or at https://opensource.org/licenses/MIT. */ import { Health, Layer } from '@mdf.js/core'; import { LoggerInstance } from '@mdf.js/logger'; import { CommandJobHandler } from '@mdf.js/openc2'; import EventEmitter from 'events'; import { ErrorRecord } from './observability'; import { BootstrapOptions, CustomSetting, ServiceRegistryOptions, ServiceRegistrySettings, ServiceSetting } from './types'; export declare interface ServiceRegistry { /** * Add a listener for the `command` event, emitted when a new command is received * @param event - `command` event * @param listener - Command event listener * @event */ on(event: 'command', listener: (job: CommandJobHandler) => void): this; /** * Add a listener for the `command` event, emitted when a new command is received * @param event - `command` event * @param listener - Command event listener * @event */ addListener(event: 'command', listener: (job: CommandJobHandler) => void): this; /** * Add a listener for the `command` event, emitted when a new command is received. This is a * one-time event, the listener will be removed after the first emission. * @param event - `command` event * @param listener - Command event listener * @event */ once(event: 'command', listener: (job: CommandJobHandler) => void): this; /** * Removes the specified listener from the listener array for the `command` event. * @param event - `command` event * @param listener - Command event listener * @event */ off(event: 'command', listener: (job: CommandJobHandler) => void): this; /** * Removes the specified listener from the listener array for the `command` event. * @param event - `command` event * @param listener - Command event listener * @event */ removeListener(event: 'command', listener: (job: CommandJobHandler) => void): this; /** * Removes all listeners, or those of the specified event. * @param event - `command` event */ removeAllListeners(event?: 'command'): this; } export declare class ServiceRegistry<CustomSettings extends Record<string, CustomSetting> = Record<string, CustomSetting>> extends EventEmitter { /** Service Settings manager */ private readonly _settingsManager; /** Resources attached to the service registry observability */ private readonly _resources; /** Service Registry observability instance */ private readonly _observability; /** Service Registry control manager */ private readonly _consumer; /** Flag to indicate if the service has performed the bootstrap */ private _booted; /** Flag to indicate if the service has started */ private _started; /** Logger instance */ private readonly _logger; /** * Create a new instance of the Service Registry * @param bootstrapOptions - Bootstrap settings, define how the Custom and the Service Registry * settings should be loaded. * @param serviceRegistryOptions - Service Registry settings, used as a base for the Service * Registry configuration manager. * @param customSettings - Custom settings provided by the user, used as a base for the Custom * configuration manager. */ constructor(bootstrapOptions?: BootstrapOptions, serviceRegistryOptions?: ServiceRegistryOptions<CustomSettings>, customSettings?: Partial<CustomSettings>); /** @returns Default resolver map for the OpenC2 Consumer interface */ private get resolverMap(); /** Return the health information from the observability instance */ private readonly onHealthCommand; /** Return the service stats from the observability instance */ private readonly onStatsCommand; /** Return the errors stored in the registry from the observability instance */ private readonly onErrorsCommand; /** Return the custom settings from the configuration manager */ private readonly onConfigCommand; /** * Perform the finish of the service engine and exit the process * @param signal - The signal received */ private readonly onFinishCommand; /** Handle the command event from the OpenC2 consumer */ private readonly onCommandEvent; /** * Wrap the start method of the resource to avoid errors * @param resource - the resource to be wrapped */ private readonly wrappedStart; /** * Wrap the stop method of the resource to avoid errors * @param resource - the resource to be wrapped * @returns */ private readonly wrappedStop; /** Perform the bootstrap of all the service registry resources */ private readonly bootstrap; /** Perform the shutdown of all the service registry resources */ private readonly shutdown; /** @returns The retry options used for starting resources and service */ private get retryOptions(); /** @returns Service Register health information */ get errors(): ErrorRecord[]; /** @returns Service Register health information */ get health(): Layer.App.Health; /** @returns Service Register status */ get status(): Health.Status; /** @returns Service Register settings */ get serviceRegistrySettings(): ServiceRegistrySettings<CustomSettings>; /** @returns Custom settings */ get customSettings(): CustomSettings; /** @returns Service settings */ get settings(): ServiceSetting<CustomSetting>; /** @returns The logger instance */ get logger(): LoggerInstance; /** @return The application identification string */ private get identification(); /** * Register a resource within the service observability * @param resource - The resource or resources to be register */ register(resource: Layer.Observable | Layer.Observable[]): void; /** * Gets the value at path of object. If the resolved value is undefined, the defaultValue is * returned in its place. * @param path - path to the property to get * @param defaultValue - default value to return if the property is not found * @template T - Type of the property to return */ get<T>(path: string | string[], defaultValue?: T): T | undefined; /** * Gets the value at path of object. If the resolved value is undefined, the defaultValue is * returned in its place. * @param key - path to the property to get * @param defaultValue - default value to return if the property is not found */ get<P extends keyof CustomSettings>(key: P, defaultValue?: CustomSettings[P]): CustomSettings[P] | undefined; /** Perform the initialization of all the service resources that has been attached */ readonly start: () => Promise<void>; /** Perform the stop of all the service resources that has been attached */ readonly stop: () => Promise<void>; } //# sourceMappingURL=ServiceRegistry.d.ts.map