2.0.5 • Published 7 years ago
micro-nervous v2.0.5
Install
npm install micro-nervous
Note: Node version 6.0.0
or higher is required.
Overview
const { Nerve, Stats, Service } = require('micro-nervous');
// 'Nerves' are simple class wrappers for Resources or Connections
class RedisNerve extends Nerve {
init() {
// Connection logic goes here:
this.redis = new Redis('redis://:password@my.redis.resource:6379');
// When our connection is ready, just fire the `ready` event
this.redis.once('ready', () => this.fire('ready'));
}
exit() {
// Disconnection logic goes here, as expected:
redis.once('end', () => this.fire('end'));
redis.disconnect();
}
}
// Nerves are re-usable, so can be shared between different microservices
const nerves = [
new RedisNerve('pub'),
new RedisNerve('sub'),
];
// A stats instance serves healthchecks and stats over http
const stats = new Stats({ port: 3000 });
// Instance of the Service class just ties together Nerve instances and stats
const service = new Service({ nerves, stats });
// This gives you an event emitter, nothing too special
service.on('ready', () => console.log('Your service is ready!'));
// But this can be useful
service.on('end', code => process.exit(code));
// Events fired by your Nerves are prefixed with their name
service.on('pub-ready', () => console.log('Publish Nerve is ready');
// Service#poweroff shuts down your connections gracefully
process.once('SIGTERM', () => service.poweroff());
// Just like Service#connect gets your connections started
service.connect();
// You also get access to the Nerve instances
service.nerve.sub.redis.subscribe('channel', (message) => {
// And a tasking system to prevent unwanted shutdowns
const task = service.newTask();
// Now Service#poweroff will wait...
// You can do lots of stuff... async stuff...
// And even if you get a SIGTERM, from say, kubernetes...
// Your task be allowed to finish before your connections are dropped!
console.log('Yay, message:', message);
task.done();
});
// The Nerve instances are attached using the keys used in the constructor
service.nerve.pub.redis.publish('channel', 'hello world!');
Healthchecks
Once you call Service#start
you can hit http://host:3000/ok
(or the port of your choosing) for that 200 OK
feeling!
Obviously that goes away the moment something goes wrong. Or when you call Service#poweroff
, I guess.
Stats
You can also hit http://host:3000/stats
for some sweet, sweet json. For example:
{
"ok": true,
"currentTasks": 0
}
The stats json just comes from service.getStats()
, which can be easily overwritten
class MyService extends Service {
getStats() {
return {
ok: this.ok,
currentTasks: this.stats.currentTasks,
answer: askDeepThought(),
};
}
}
Incidentally, stats.ok
is used for the healthcheck: true
for 200
, anything else gives you a 503
.
Roadmap
Some features that might make it into future versions, in no particular order:
- Example Nerves for common resources, e.g. Redis, Mongo, etc.
- Type annotations for Closure Compiler
Contributing
All help is appreciated! Feel free to fork, branch master
and open a PR. Thanks!