Skip to content

kacao/services

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Services

Running async services with dependencies in node.js

Installation

npm install kacao/services

Usage

This creates 5 services: config, db, settings, backend and frontend, calling run() will load services in this order (services will wait for dependencies to be ready before starting to run):

  • config
  • db
  • settings
  • backend and frontend (asynchronously without waiting for each other) 
const {Service, Services} = require('./index');

let services = new Services();
services.add([
  new Service('config', []),
  new Service('db', ['config']);                         // db depends on config
  new Service('settings', ['config', 'db']),             // settings depends on config and db
  new Service('backend', ['config', 'db', 'settings'])   // backend depends on config, db and settings
  new Service('frontend', ['config', 'db', 'settings'])  // frontend depends on config, db and settings
  
]);

services.run().then( () => {
  console.log('done');
});

The run() function

Services have async run() which we can override to run a service after all dependencies are ready. 

let db = new Service('db', ['config']);
services.add(db);

db.run = async (ready, fail) => {
  console.log('db service init');
  // sleep for 3s then set to ready
  (new Promise(resolve => setTimeout(resolve, 3000))).then( () => {
    console.log('db ready');
    ready();
  });
}

Calling ready() will let depending services to start running, calling fail() will cause Services to stop loading services and return an Error.

About

Running services asynchronously

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published