1.3.0 • Published 4 years ago

system-service v1.3.0

Weekly downloads
10
License
MIT
Repository
github
Last release
4 years ago

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.

License Build Status Coverage Status Dependency Status devDependency Status semantic-release JavaScript Style Guide Greenkeeper badge Known Vulnerabilities npm badge

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:

MethodDescription
createImplement to connect the 3rd party consumer (e.g. RabbitMQ, kafka, etc) with callback to this.service().processMessage
validateImplement custom validation for any received message. If the message is invalid, then throw exception (or external handling)
processImplement how to prcoess the valid message
startImplement to start the 3rd party consumer
stopImplement how to stop the 3rd party consumer to pickup any message
cleanupImplement any cleanup after messageConsumer's stop method is triggered
serviceGet the currency SystemService instance

SystemService

SystemService is a message engine, which handles start and terminate the consumer by the following methods:

MethodDescription
startService start to receive message
stopService stop to receive message

Logger

logger can be used in both MessageConsumer and SystemService's derived classes this.logger.log(level, message, options)

ParameterDescription
levelLogging level representing priorities (error, warn, info, verbose, debug, silly)
messagemessage
optionsoptional 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
Layout
  • Inside handling
Workflow
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 logic
  • Security : Inject the security logic and apply at consumer / use between system services communication
  • Multiple services : Work with other system services (TRY NOT TO by Single Responsibility Principle)
Layout
Advance

^License

MIT

1.2.4

4 years ago

1.3.0

4 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago