@rugo-vn/service

2.1.0-beta.0 • Public • Published

Rugo Service

Base for microservice system - Unit to build Rugo Platform.

Concept

Service

Service is a basic unit in Rugo Platform.

It can be write in any programming language.

It's an independent process managed by broker.

To spawn a service, do:

import { spawnService } from '@rugo-vn/service';

const service = spawnService(definition);

Below is an example of definition structure:

const serviceDefine = {
  /* required */
  name: 'service-name',
  exec: ['python', 'app.py'],

  /* optional */
  cwd: '/path/to/source/code',
  settings: {
    /* key-value settings */
  },
};

If your service managed by a centralization control service, you can pass config action of that service to settings:

const serviceDefine = {
  name: 'service-name',
  settings: 'control.config' /* await config action from control service */,
};

To start the service, run:

await service.start();

To stop the service, run:

await service.stop();

Communication

Between NodeJS process and Service process, we define a communication channel via UNIX socket.

const socketA = await createSocket('/path/to/socket');

await socketA.close();

When service.start run, it will send a start action to the service and wait for response.

When service.stop run, it will send a stop action to the service and wait for response.

Address

Each action have their own address by combinate service name and action name.

Addresses are unique and cannot be change over the platform.

Broker

Broker is a container to host services. It will create communication environment between services.

Usage:

import { createBroker } from '@rugo-vn/service';

const broker = await createBroker({
  port: /* tcp port for serve */
  endpoints: [ /* list of endpoint to connect */ ]
});

Helper

To build a service in the process quickly, we introduced helper.

NodeJS

import { defineAction } from '@rugo-vn/service';

defineAction('actionName', async function (args, opts) {
  /* do something */
  return /* return something */;
});

Concept

  • system is an entire program, which run by node command.
  • The system is devided into units, called service.
  • service identity is name. It's a unique value.
  • Super service to create, load and start other services, callced broker.
  • Services were created by same broker, called scope.
  • In scope, every services share a same variable called globals. You can access globals anywhere from the service through this.globals.
  • In scope, every services can be execute functions from another service by call method, which can access anywhere in the service through this.call.
  • In service, you can bind functions to this, these functions called method.
  • this.call will call a function, called action. The call must have a address of the action you want to execute by format <name>.<action>.
  • You can bind functions before, after and when error occur by hooks.

Service Structure

const serviceDefine = {
  name: /* ... */,
  settings: {
    /* ... */
  },
  methods: {
    async methodName(/* ... */) {
      /* ... */
    },

    /* ... */
  },
  actions: {
    async actionName(/* ... */) {
      /* ... */
    },

    /* ... */
  },
  hooks: {
    before: {
      async all(/* ... */) {
        /* ... */
      },

      /* ... */
    },

    after: {
      async all(/* ... */) {
        /* ... */
      },

      /* ... */
    },

    error: {
      async all(/* ... */) {
        /* ... */
      },

      /* ... */
    }
  },

  async started(/* ... */) {
    /* ... */
  },

  async closed(/* ... */) {
    /* ... */
  },
}

Usage

const settings = {
  _services: [
    '/path/to/service',
    /* ... */
  ],
  _globals: {
    /* global variables */
  },
};

const broker = createBroker(settings);

const service = broker.createService(serviceDefine);

await broker.loadServices();

await broker.start();
await broker.close();

License

MIT.

Readme

Keywords

none

Package Sidebar

Install

npm i @rugo-vn/service

Weekly Downloads

1

Version

2.1.0-beta.0

License

MIT

Unpacked Size

40.5 kB

Total Files

28

Last publish

Collaborators

  • haova