system-service
v1.3.0
Published
> Provide the basic service framework to help initial service implementation. It can be easy to inject any message framework and has built-in logging mechanism.
Downloads
48
Readme
system-service
Provide the basic service framework to help initial service implementation. It can be easy to inject any message framework and has built-in logging mechanism.
Contents
^Install
Install via npm:
npm install system-service --save
^Definition
const systemService = require("system-service");
const { SystemService, Logger, MessageConsumer } = systemService;
MessageConsumer
MessageConsumer is base class, which uses for connecting with SystemService. The derived class can be overwritten by the following methods:
| Method | Description | | -------- | ---------------------------------------------------------------------------------------------------------------------------- | | create | Implement to connect the 3rd party consumer (e.g. RabbitMQ, kafka, etc) with callback to this.service().processMessage | | validate | Implement custom validation for any received message. If the message is invalid, then throw exception (or external handling) | | process | Implement how to prcoess the valid message | | start | Implement to start the 3rd party consumer | | stop | Implement how to stop the 3rd party consumer to pickup any message | | cleanup | Implement any cleanup after messageConsumer's stop method is triggered | | service | Get the currency SystemService instance |
SystemService
SystemService is a message engine, which handles start and terminate the consumer by the following methods:
| Method | Description | | ------ | -------------------------------- | | start | Service start to receive message | | stop | Service stop to receive message |
Logger
logger can be used in both MessageConsumer and SystemService's derived classes
this.logger.log(level, message, options)
| Parameter | Description |
| --------- | -------------------------------------------------------------------------------------------- |
| level | Logging level representing priorities (error
, warn
, info
, verbose
, debug
, silly
) |
| message | message |
| options | optional information |
^Diagram
General usage
: Create a dervied class as ATypeConsumer from MessageConsumer. Inside ATypeConsumer, configures it using the 3rd party consumer under create() and overrides any methods fitting for your use case.
| Layout | | --------------------------------------------------------------------------------------------------- | | |
- Inside handling
| Workflow | | ----------------------------------------------------------------------------------------------- | | |
^Get Start
- Setup message cosumer
const mq = require('amqplib/callback_api')
const URI = 'amqp://guest:guest@localhost:5672//'
const QUEUENAME = 'Demo'
const systemService = require('system-service')
const { MessageConsumer } = systemService
function errHandler (err) {
// TODO: logging or exist program ...
}
function MQConnect (conn, queueName, handler) {
const mQConn = function (err, ch) {
if (err !== null) {
errHandler(err)
} else {
ch.assertQueue(queueName)
ch.consume(queueName, function (msg) {
if (msg !== null) {
handler(msg)s
ch.ack(msg)
}
})
}
}
return mQConn
}
class DemoConsumer extends MessageConsumer {
constructor () {
super()
this.uri = URI
this.queueName = QUEUENAME
this.conn = null
this.on_open = null
}
create () {
super.create()
mq.connect(this.uri, function (err, conn) {
if (err !== null) {
errHandler(err)
} else {
this.conn = conn
// config the mq consume to call this.service().processMessage
this.on_open = MQConnect(conn, this.queueName, this.service().processMessage)
}
})
}
validate (message) {
super.validate(message)
if ((message.cId === null) || (typeof message.cId === 'undefined')) {
this.logger.log('error', 'message is missing cId', message)
throw new Error('Missing Correlation Id')
}
}
// process will only be called, when message is valid
process (message) {
super.process(message)
// TODO: Implement handle message
this.logger.log('verbose', 'Start process', message)
}
start () {
super.start()
const ok = this.conn.createChannel(this.on_open)
if (ok === null) {
errHandler(new Error('Fail: To create MQ channel'))
}
}
}
module.exports.DemoConsumer = DemoConsumer
- Spin up Service
const systemService = require("system-service");
const { SystemService, Logger } = systemService;
const config = { log: { config: { level: Logger.Level.error } } };
const service = new SystemService(config, new DemoConsumer());
service.start();
- Stop Service
service.stop();
^Advance
Create custom system service
Cache
: Add service cache logicSecurity
: Inject the security logic and apply at consumer / use between system services communicationMultiple services
: Work with other system services (TRY NOT TO
by Single Responsibility Principle)
| Layout | | ------------------------------------------------------------------------------------------------------------ | | |
^License
MIT