Class: Service<ConfigType>
PURISTA API / Modules / @purista/core / Service
Class: Service<ConfigType>
@purista/core.Service
Base class for all services. This class provides base functions to work with the event bridge, logging and so on
Every service should extend this class and should not directly access the eventbridge or other service
class MyService extends Service {
async start() {
await super.start()
// your custom implementation
}
async destroy() {
// your custom implementation
await super.destroy()
}
}
Type parameters
Name | Type |
---|---|
ConfigType | unknown | undefined |
Hierarchy
ServiceBaseClass
↳
Service
Implements
ServiceClass
<ConfigType
>
Table of contents
Constructors
Properties
- commandDefinitionList
- commands
- config
- configStore
- eventBridge
- info
- logger
- secretStore
- spanProcessor
- stateStore
- subscriptionDefinitionList
- subscriptions
- traceProvider
Accessors
Methods
- destroy
- emit
- executeCommand
- executeSubscription
- getContextFunctions
- getEmitFunction
- getInvokeFunction
- getTracer
- initializeEventbridgeConnect
- off
- on
- registerCommand
- registerSubscription
- removeAllListeners
- sendServiceInfo
- start
- startActiveSpan
- wrapInSpan
Constructors
constructor
• new Service<ConfigType
>(config
): Service
<ConfigType
>
Type parameters
Name | Type |
---|---|
ConfigType | unknown |
Parameters
Name | Type |
---|---|
config | ServiceConstructorInput <ConfigType > |
Returns
Service
<ConfigType
>
Overrides
ServiceBaseClass.constructor
Defined in
core/Service/Service.impl.ts:84
Properties
commandDefinitionList
• commandDefinitionList: CommandDefinitionList
<any
>
Defined in
core/Service/Service.impl.ts:80
commands
• Protected
commands: Map
<string
, CommandDefinition
>
Defined in
core/Service/Service.impl.ts:78
config
• config: ConfigType
Implementation of
Defined in
core/Service/Service.impl.ts:82
configStore
• configStore: ConfigStore
Inherited from
ServiceBaseClass.configStore
Defined in
core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:35
eventBridge
• eventBridge: EventBridge
Inherited from
ServiceBaseClass.eventBridge
Defined in
core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:26
info
• Readonly
info: ServiceInfoType
Inherited from
ServiceBaseClass.info
Defined in
core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:24
logger
• logger: Logger
Inherited from
ServiceBaseClass.logger
Defined in
core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:28
secretStore
• secretStore: SecretStore
Inherited from
ServiceBaseClass.secretStore
Defined in
core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:34
spanProcessor
• spanProcessor: undefined
| SpanProcessor
Inherited from
ServiceBaseClass.spanProcessor
Defined in
core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:30
stateStore
• stateStore: StateStore
Inherited from
ServiceBaseClass.stateStore
Defined in
core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:36
subscriptionDefinitionList
• subscriptionDefinitionList: SubscriptionDefinitionList
<any
>
Defined in
core/Service/Service.impl.ts:81
subscriptions
• Protected
subscriptions: Map
<string
, SubscriptionDefinition
>
Defined in
core/Service/Service.impl.ts:77
traceProvider
• traceProvider: NodeTracerProvider
Inherited from
ServiceBaseClass.traceProvider
Defined in
core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:32
Accessors
name
• get
name(): string
Returns
string
Defined in
core/Service/Service.impl.ts:100
serviceInfo
• get
serviceInfo(): ServiceInfoType
Get service info
Returns
Inherited from
ServiceBaseClass.serviceInfo
Defined in
core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:94
Methods
destroy
▸ destroy(): Promise
<void
>
Stop and destroy the current service
Returns
Promise
<void
>
Implementation of
Overrides
ServiceBaseClass.destroy
Defined in
core/Service/Service.impl.ts:785
emit
▸ emit<K
>(eventName
, parameter?
): void
Type parameters
Name | Type |
---|---|
K | extends EventKey <ServiceEvents > |
Parameters
Name | Type |
---|---|
eventName | K |
parameter? | ServiceEvents [K ] |
Returns
void
Inherited from
ServiceBaseClass.emit
Defined in
core/types/GenericEventEmitter.ts:24
executeCommand
▸ executeCommand(message
): Promise
<Readonly
<Omit
<{ contentEncoding
: "utf-8"
; contentType
: "application/json"
; correlationId
: string
; eventName?
: string
; id
: string
; isHandledError
: boolean
; messageType
: CommandErrorResponse
; otp?
: string
; payload
: { data?
: unknown
; message
: string
; status
: StatusCode
} ; principalId?
: string
; receiver
: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; sender
: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; tenantId?
: string
; timestamp
: number
; traceId?
: string
}, "instanceId"
>> | { contentEncoding
: string
; contentType
: string
; correlationId
: string
; eventName?
: string
; id
: string
; messageType
: CommandSuccessResponse
; otp
: string
; payload
: unknown
; principalId?
: string
; receiver
: { instanceId
: string
; serviceName
: string
; serviceTarget
: string
; serviceVersion
: string
} ; sender
: { instanceId
: string
; serviceName
: string
; serviceTarget
: string
; serviceVersion
: string
} ; tenantId?
: string
; timestamp
: number
; traceId?
: string
}>
Called when a command is received by the service
Parameters
Name | Type |
---|---|
message | Readonly <{ contentEncoding : string ; contentType : string ; correlationId : string ; eventName? : string ; id : string ; messageType : Command ; otp? : string ; payload : { parameter : unknown ; payload : unknown } ; principalId? : string ; receiver : EBMessageAddress ; sender : { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; tenantId? : string ; timestamp : number ; traceId? : string }> |
Returns
Promise
<Readonly
<Omit
<{ contentEncoding
: "utf-8"
; contentType
: "application/json"
; correlationId
: string
; eventName?
: string
; id
: string
; isHandledError
: boolean
; messageType
: CommandErrorResponse
; otp?
: string
; payload
: { data?
: unknown
; message
: string
; status
: StatusCode
} ; principalId?
: string
; receiver
: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; sender
: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; tenantId?
: string
; timestamp
: number
; traceId?
: string
}, "instanceId"
>> | { contentEncoding
: string
; contentType
: string
; correlationId
: string
; eventName?
: string
; id
: string
; messageType
: CommandSuccessResponse
; otp
: string
; payload
: unknown
; principalId?
: string
; receiver
: { instanceId
: string
; serviceName
: string
; serviceTarget
: string
; serviceVersion
: string
} ; sender
: { instanceId
: string
; serviceName
: string
; serviceTarget
: string
; serviceVersion
: string
} ; tenantId?
: string
; timestamp
: number
; traceId?
: string
}>
Defined in
core/Service/Service.impl.ts:426
executeSubscription
▸ executeSubscription(message
, subscriptionName
): Promise
<undefined
| Omit
<{ contentEncoding
: string
; contentType
: string
; correlationId?
: string
; eventName
: string
; id
: string
; messageType
: CustomMessage
; otp?
: string
; payload?
: unknown
; principalId?
: string
; receiver?
: EBMessageAddress
; sender
: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; tenantId?
: string
; timestamp
: number
; traceId?
: string
}, "id"
| "timestamp"
>>
Parameters
Name | Type |
---|---|
message | Readonly <EBMessage > |
subscriptionName | string |
Returns
Promise
<undefined
| Omit
<{ contentEncoding
: string
; contentType
: string
; correlationId?
: string
; eventName
: string
; id
: string
; messageType
: CustomMessage
; otp?
: string
; payload?
: unknown
; principalId?
: string
; receiver?
: EBMessageAddress
; sender
: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; tenantId?
: string
; timestamp
: number
; traceId?
: string
}, "id"
| "timestamp"
>>
Defined in
core/Service/Service.impl.ts:587
getContextFunctions
▸ getContextFunctions(logger
): ContextBase
Parameters
Name | Type |
---|---|
logger | Logger |
Returns
Defined in
core/Service/Service.impl.ts:253
getEmitFunction
▸ getEmitFunction(serviceTarget
, traceId
, principalId?
, tenantId?
): <Payload>(eventName
: string
, eventPayload?
: Payload
, contentType
: string
, contentEncoding
: string
) => Promise
<void
>
Parameters
Name | Type |
---|---|
serviceTarget | string |
traceId | string |
principalId? | string |
tenantId? | string |
Returns
fn
▸ <Payload
>(eventName
, eventPayload?
, contentType?
, contentEncoding?
): Promise
<void
>
Type parameters
Name |
---|
Payload |
Parameters
Name | Type | Default value |
---|---|---|
eventName | string | undefined |
eventPayload? | Payload | undefined |
contentType | string | 'application/json' |
contentEncoding | string | 'utf-8' |
Returns
Promise
<void
>
Defined in
core/Service/Service.impl.ts:218
getInvokeFunction
▸ getInvokeFunction(serviceTarget
, traceId
, principalId?
, tenantId?
): (receiver
: EBMessageAddress
, eventPayload
: unknown
, parameter
: unknown
, contentType
: string
, contentEncoding
: string
) => Promise
<any
>
Parameters
Name | Type |
---|---|
serviceTarget | string |
traceId | string |
principalId? | string |
tenantId? | string |
Returns
fn
▸ (receiver
, eventPayload
, parameter
, contentType?
, contentEncoding?
): Promise
<any
>
Parameters
Name | Type | Default value |
---|---|---|
receiver | EBMessageAddress | undefined |
eventPayload | unknown | undefined |
parameter | unknown | undefined |
contentType | string | 'application/json' |
contentEncoding | string | 'utf-8' |
Returns
Promise
<any
>
Defined in
core/Service/Service.impl.ts:182
getTracer
▸ getTracer(name?
, version?
): Tracer
Returns open telemetry tracer of this service
Parameters
Name | Type |
---|---|
name? | string |
version? | string |
Returns
Tracer
Tracer
Implementation of
Inherited from
ServiceBaseClass.getTracer
Defined in
core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:103
initializeEventbridgeConnect
▸ initializeEventbridgeConnect(commandDefinitionList
, subscriptions
): Promise
<void
>
Connect service to event bridge to receive commands and command responses
Parameters
Name | Type |
---|---|
commandDefinitionList | CommandDefinitionList <any > |
subscriptions | SubscriptionDefinition [] |
Returns
Promise
<void
>
Defined in
core/Service/Service.impl.ts:128
off
▸ off<K
>(eventName
, fn
): void
Type parameters
Name | Type |
---|---|
K | extends EventKey <ServiceEvents > |
Parameters
Name | Type |
---|---|
eventName | K |
fn | EventReceiver <ServiceEvents [K ]> |
Returns
void
Inherited from
ServiceBaseClass.off
Defined in
core/types/GenericEventEmitter.ts:20
on
▸ on<K
>(eventName
, fn
): void
Type parameters
Name | Type |
---|---|
K | extends EventKey <ServiceEvents > |
Parameters
Name | Type |
---|---|
eventName | K |
fn | EventReceiver <ServiceEvents [K ]> |
Returns
void
Inherited from
ServiceBaseClass.on
Defined in
core/types/GenericEventEmitter.ts:16
registerCommand
▸ registerCommand(commandDefinition
): Promise
<void
>
Registers a new command for the service
Parameters
Name | Type | Description |
---|---|---|
commandDefinition | CommandDefinition | the service command definition |
Returns
Promise
<void
>
Implementation of
Defined in
core/Service/Service.impl.ts:560
registerSubscription
▸ registerSubscription(subscriptionDefinition
): Promise
<void
>
Registers a new subscription for the service
Parameters
Name | Type | Description |
---|---|---|
subscriptionDefinition | SubscriptionDefinition | the subscription definition |
Returns
Promise
<void
>
Implementation of
ServiceClass.registerSubscription
Defined in
core/Service/Service.impl.ts:749
removeAllListeners
▸ removeAllListeners(): void
Returns
void
Inherited from
ServiceBaseClass.removeAllListeners
Defined in
core/types/GenericEventEmitter.ts:28
sendServiceInfo
▸ sendServiceInfo(infoType
, target?
, payload?
): Promise
<Readonly
<EBMessage
>>
Broadcast service info message
Parameters
Name | Type | Description |
---|---|---|
infoType | InfoMessageType | type of info message |
target? | string | function name is need in messages like InfoServiceFunctionAdded |
payload? | Record <string , unknown > | - |
Returns
Promise
<Readonly
<EBMessage
>>
Defined in
core/Service/Service.impl.ts:162
start
▸ start(): Promise
<void
>
It connects to the event bridge and subscribes to the topics that are in the subscription list.
Returns
Promise
<void
>
Implementation of
Defined in
core/Service/Service.impl.ts:107
startActiveSpan
▸ startActiveSpan<F
>(name
, opts
, context?
, fn
): Promise
<F
>
Start a child span for opentelemetry tracking
Type parameters
Name |
---|
F |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
name | string | undefined | name of span |
opts | SpanOptions | undefined | span options |
context | undefined | Context | undefined | optional context |
fn | (span : Span ) => Promise <F > | undefined | function to be executed within the span |
Returns
Promise
<F
>
return value of fn
Implementation of
Inherited from
ServiceBaseClass.startActiveSpan
Defined in
core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:118
wrapInSpan
▸ wrapInSpan<F
>(name
, opts
, fn
, context?
): Promise
<F
>
Start span for opentelemetry tracking on same level. The created span will not become the "active" span within opentelemetry!
This means during logging and similar the spanId of parent span is logged.
Use wrapInSpan for marking points in flow of one bigger function, but not to trace the program flow itself
Type parameters
Name |
---|
F |
Parameters
Name | Type | Description |
---|---|---|
name | string | name of span |
opts | SpanOptions | span options |
fn | (span : Span ) => Promise <F > | function te be executed in the span |
context? | Context | span context |
Returns
Promise
<F
>
return value of fn
Implementation of
Inherited from
ServiceBaseClass.wrapInSpan