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, calledservice
. -
service
identity isname
. It's a unique value. - Super
service
to create, load and start other services, callcedbroker
. - Services were created by same
broker
, calledscope
. - In
scope
, every services share a same variable calledglobals
. You can accessglobals
anywhere from the service throughthis.globals
. - In
scope
, every services can be execute functions from another service bycall
method, which can access anywhere in the service throughthis.call
. - In
service
, you can bind functions tothis
, these functions calledmethod
. -
this.call
will call a function, calledaction
. The call must have aaddress
of theaction
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.