modusign-worker
sqs consumer for modusign services
Last updated 2 years ago by imione .
ISC · Original npm · Tarball · package.json
$ cnpm install modusign-worker 
SYNC missed versions from official npm registry.

Modusign-worker

Implementing worker app

Installation

Install with npm

npm install modusign-worker

Usage

const { Worker } = require('modusign-worker');

const subscriberName = 'bbcSqsConsumer';
const option = {};

const worker = new Worker({ subscriberName: '', option });

// message parser must be included as middleware
const parseMessage = (message) => {
	const { Subject: subejct, Body: body } = JSON.parse(message);
	return {
		subject,
		body,
	};
}

const middlewareOne = async (context) => {
	// process context #1
	return context;
}

const jobNumberOne = async (context) => {
	// do job #1
}
const jobNumberTwo = async (context) => {
	// do job #2
}

worker.setParser(parseMessage);
worker.use(middlewareOne);

worker.register('job.number.one', jobNumberOne);
worker.register('job.number.two', jobNumberTwo);

Configure worker

  1. choose subscriber and create option object
const { Worker } = require('modusign-worker');

const subscriberName = 'bbcSqsConsumer';
const option = {};

Reference supporting subscribers below which string you can assign subscriberName option must contain properties fit subscriber. Reference subscriber's own document.

  1. create worker instance
const worker = new Worker({ subscriberName: '', option });

Add parser (important)

Add message parser you want to process.
The parser get message and must return context which contains subject and so on.

  1. implement parser
const messageParser = (message) => {
	const { Subject: subejct, Body: body, MessageId: messageId, MD5OfBody: md5OfBody } = JSON.parse(message);
	const context = {
		messageId,
		md5OfBody
		subject,
		body,
	};
	return context;
}

subject is required and must be a string. Job handlers find the job it should handle using subject. (like routing path)

  1. set parser on your worker
worker.setParser(messageParser);

Add middlewares

Worker support web-framework style middlewares
Add functions that process all context before pass job handlers
Middlewares get and return context (if it should be replaced)

  1. implement middlewares
const middleware = (context) => {
	// process context
	return context
}

When middleware returns context, whole context will be replaced.

  1. use middlewares
worker.use(middleware);

Register job handlers

Job handlers can be implemented like controllers
Add handlers that contains your business logics.
Handlers get whole context, and are excuted following subject

  1. implement handler
const jobHandler = async (context) => {
	// business logic
};
  1. register handler
worker.register('subject', jobHandler);

Set error handler

Define how you handle error

  1. implement handler
const errorHandler = async (error) => {
	// process error
}
  1. set error handler
worker.setErrorHandler(errorHandler);

Supporting Queue Services and subscribers

  1. sqs-consumer Polling amazon sqs.
const worker = new Worker({ subscriberName: 'bbcSqsConsumer', option });

option: sqs-consumer document do not define option.handleMessage.

Current Tags

  • 0.2.3                                ...           latest (2 years ago)

5 Versions

  • 0.2.3                                ...           2 years ago
  • 0.2.2                                ...           2 years ago
  • 0.2.1                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 3
Last Day 0
Last Week 1
Last Month 9
Dependencies (3)
Dev Dependencies (9)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |