2.0.6 • Published 8 years ago

serviced v2.0.6

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

Serviced

Build Status Coverage Status Dependency Status npm version

Microservice dependency readiness probe

Small utility to probe for backing-service availability using http calls and promises. This will likely evolve to be more compatible but, for now, it uses features available in Node 6+. Feel free to send pull requests or open issues to make it compatible with earlier versions.

Motivation

When working with microservices, we often find we need to be prepared for variable times in backing service availability. Most times these are available and ready before we even probe them but some times they are not like in the case of a service needing to apply some configuration specific to a consuming service, etc. Serviced provides a way to probe multiple services with varying probe and retry configurations wrapped in a promise so you can, for example, not start an API server until all its backing services are available and ready.

Usage

npm i serviced

Test that a single service returns status code 200.

const Serviced = require('serviced');

new Serviced('http://exmample.com').then(() => {
  // Your service is online and returns status code 200.
});

Test that multiple services return status code 200.

new Serviced('http://svc-a.com', 'http://svc-b.com').then(() => {
  // Both services are online and return status codes 200.
});

Test the response body from a service request with a test function.

new Serviced({
  url: 'http://svc-a.com',
  test(body) {
    return body.foo === true;
  },
}).then(() => {
  // Service is online and response passes supplied test.
});

Test the response body from multiple service requests with test functions.

new Serviced({
  url: 'http://svc-a.com',
  test(body) {
    return body.foo === true;
  },
}, {
  url: 'http://svc-b.com',
  test(body) {
    return body.bar === true;
  },
}).then(() => {
  // Services are online and responses pass supplied tests.
});

Different options for multiple services.

// http://svc-a.com is only tested for a 200 response.
// http://svc-b.com's response body is passed to your own test function.
new Serviced('http://svc-a.com', {
  url: 'http://svc-b.com',
  test(body) {
    return body.bar === true;
  },
}).then(() => {
  // Services are online and responses pass supplied tests.
});

Pass additional options to underlying request and retry modules.

The optional .request and .retry objects are passed through to the request and retry module instances respectively so you can control those as you deem necessary. It's a straight passthrough so please read their docs to find out what options are available for each.

new Serviced({
  url: 'http://svc-a.com',
  request: { // pass options to request
    auth: {
      user: 'username',
      pass: 'password',
    }
  },
  retry: { // pass options to retry
    retries: 2,
    maxTimeout: 2000,
  },
  test(body) {
    return body.foo === true;
  },
}).then(() => {
  // Service is online and response passes supplied test.
});

Contributing

  • Fork the repo.
  • npm i
  • npm start to work on the library. This runs the tests continuously watching for changes but without test output (debug output instead).
  • Make your changes
  • npm t to run the tests with no debug output (tap output instead).
  • npm run cov to run coverage report.
  • npm run linter to run eslint.
  • Submit a PR.

Change log

  • 2.0.0 Breaking changes from 1.x. I switched to retry over backoff primarily because of how it handles errors but also for simplicity.
2.0.6

8 years ago

2.0.5

8 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago