maestrojs

0.1.2 • Public • Published

Build Status

Maestro.js

Maestro.js is a library that helps dynamically orchestrate different services using Redis. Services do not need to know any information about the service they depend on (only the name), this library will be responsible to resolve the options necessary to connect to all services and keep the service updated with the latest options.

Maestro.js goal is to be very simple to set up

Use case: You have 5 services using a cluster of redis machines for caching. You add a new redis and insert it into redis. Now all your services using that cache cluster will seamlessly update to use this new cache.

Example

var redis = require('redis');
var Maestro = require("maestrojs");
 
/**
 * Initialize Maestro with Redis endpoint
 */
var maestro = new Maestro("127.0.0.1", 6379); //default values
 
/**
 * Wait for Redis information and connect to it
 * (If another redis registers, then Maestro will automatically
 * update your application to use it)
 */
maestro.use('services.redis', function(entries) {
  var redis = redis.createClient(entries[0].endpoint);
  app.use('redis', redis);
})
 
 
/**
 * Register the interface for this service
 */
maestro.register("services.api", {
  endpoint: "198.176.45.68",
  version: 2.1,
  something: true
});
 
/**
 * Start the service
 * (it will only be executed after all dependencies are resolved)
 */
maestro.start(function() {
  // Start listening for requests
});

Install

npm install maestrojs

API

Maestro.use(serviceName, setupCallback)

Declare a dependency and a callback for setting up this dependency. The callback is called everytime that service is updated. (in the case of a cache cluster, if a new cache node registers, the setup callback will be run again)

maestro.use('services.queue', function(entries) {
  var queues = [], sqs;
  for (var i = 0; i < entries.length; i++) {
    sqs = new Sqs(entries[i].endpoint);
    queues.push(sqs);
  }
  app.use("queues", queues);
})

Maestro.register(serviceName_, options)

Declare a service's interface. It will automatically append the service to the list as soon as it is available. It is important that your service does not replace a service under the same name. This way you may have several nodes serving the same service. Options_ is an object containing as many attributes as desired. _(_id is a reserved attribute).

maestro.register("services.mailer", {
  endpoint: "198.167.14.1",
  supportHtml: true,
  version: 3
});

The service will be registered after the start function runs.

Maestro.start(callback)

Callback function called as soon as as all dependencies are resolved. Make sure to have all the dependencies declared before calling this function.

maestro.start(function() {
  var server = app.listen(3000);
})

You may register your service within the start callback:

maestro.start(function() {
  var server = app.listen(3000, function () {
    var host = server.address().address
    var port = server.address().port
 
    maestro.register("services.mailer", {
      host: host,
      port: port
    });
  })
})

Maestro.quit(callback)

Quitting allows you to automatically unregister your service and updating all services dependent on yours. Quitting will also close all connections to redis, so it won't receive more updates from other services, therefore use it only when you intend to close your service.

maestro.quit(function() {
  console.log("Service unregistered and connections closed")
});

Maestro.unregister(callback)

If you want to unregister your service but not stop listening to updates from other services. You may use Maestro.unregister, which will unregister your service and update all services dependent on it.

maestro.unregister(function() {
  console.log("Service unregistered")
});

Redis Configuration

It is important to notice that we require Redis to be configured to notify Maestro in case of key changes. You may set notify-keyspace-events "AKE" in redis.conf or start redis as ./redis-server --notify-keyspace-events AKE

TODO

  • Support Redis Sentinel (to remove SPoF)

Package Sidebar

Install

npm i maestrojs

Weekly Downloads

1

Version

0.1.2

License

none

Last publish

Collaborators

  • colex