Namespace: Services

Services

Services is the namespace for the services library. It contains functions for registering service implementations and starting/stopping services.

Classes

Service

Methods

staticServices.ready(){Q.promise}

/Users/justin/src/service.js/service.js, line 220

Indicates that a set of services is ready to be used.

This function should be used to declare that a bit of code depends on services being ready to be used. As such it will fail if any of the services asked for have failed to start.

Returns:
promise that resolves to a list of the services requested, or is rejected if any of the services haven't been started or failed to start.
Example
Services.ready('MyService', 'AnotherService')
  .spread(function(myService, anotherService) {
    myService.doThings();
    anotherService.doOtherThings();
  }, function() {
    throw Error("Couldn't do the things I wanted to do :(");
  });

staticServices.register(name, service){number}

/Users/justin/src/service.js/service.js, line 45

Registers a service implementation.

Name Type Description
name string

The name of the service you're registering an implementation for.

service object

The implementation of the service. Should have Services.Service as its prototype or implement all the same methods.

Returns:
priority of this implementation (aka the number of implementations available).
Example
var DefaultImplementation = Object.create(Services.Service);
Services.register('MyService', DefaultImplementation);

staticServices.start(){Q.promise}

/Users/justin/src/service.js/service.js, line 118

Starts all the services or a subset of services.

For each service specified, this function will walk through the list of registered services and attempt to start them if they report that they are usable. If an implementation reports that it is not usable, the function will attempt to start the next registered service or will fail to start the service at all.

Returns:
promise that is resolved when the services have started or failed to start. The result of the returned promise is a list of promise snapshots that indicate which services succeeded and which failed. Calls the onStart delegate for the first usable implementation of a service. The onStart delegate can return a promise and start will not resolve its promise until all promises returned are resolved.
Example
// Let's say that MyService succeeds and AnotherService fails...
Services.start('MyService', 'AnotherService')
  .spread(function(myServiceP, anotherServiceP) {
    // => fullfilled rejected
    console.log(myServiceP.state, anotherServiceP.state)
    // =>  
    console.log(myServiceP.value, anotherServiceP.reaston)
  });

// You can also call the function without arguments.
Services.start(); // Attempts to start all registered services.

staticServices.status(){object}

/Users/justin/src/service.js/service.js, line 267

A function that allows you to inspect the current state of the various running services.

Returns:
object with two properties: registered and running. This is useful information to contain in error reporting. registered is an object with the registered service names as its properties and the number of implementations registered as the values. running returns an object with the started or starting service names as its properties and a state snapshot as its value. The state snapshot contains a state property indicating whether the service has started yet or not and, if it has, the implementation that's running.
Example
{
   registered: {
     MyService: 1,
     AnotherService: 2,
     UnusedService: 1
   },
   running: {
     MyService: { state: "pending" },
     AnotherService: {
       state: "running",
       value: 
     }
   }
}

staticServices.stop()

/Users/justin/src/service.js/service.js, line 184

Stops running services. Takes a variable number of service names as arguments or stops all services if no arguments are passed.

Waits until all services that are being started are finished starting and then calls the onStop delegate of each running instance. Does not care what the onStop delegate does.

This function will not fail if it's called twice or if a service that's requested to be stopped is not running.

Example
Services.stop(); // Stops everything
Services.stop('MyService'); // Stops MyService if it's running.

staticServices.unregister(){number}

/Users/justin/src/service.js/service.js, line 68

Unregisters a particular service. Can also unregister all of a particular service's implementations if not passed a particular service.

Returns:
number of remaining implementations.
Example
// Unregisters one implementation of the service called `service`
Services.unregister('MyService', service);

// Unregisters all services implementing MyService
Services.unregister('MyService');