npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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.

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:

| 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 | | --------------------------------------------------------------------------------------------------- | | 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