0.150.0 • Published 1 year ago

@curium.rocks/maestro v0.150.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Meastro

Quality Gate Status Coverage Security Rating npm

Manager of emitters and chroniclers. Intended to run as a service and house multiple emitters.

How to install

npm install --save @curium.rocks/maestro

API Documentation

You can view the API documentation here.

How to use

Create your configuration

The below example shows a full configuration, load, and save handlers can be provided to dynamically fetch the latest config on save/load calls.

/***
 * Create a logger facade wrapper winston
 * @param {string} serviceName 
 * @return {LoggerFacade}
 */
function getLoggerFacade(serviceName: string): LoggerFacade {
    const logger = winston.createLogger({
        level: 'info',
        format: winston.format.json(),
        defaultMeta: { service: serviceName },
        transports: [
          new winston.transports.Console()
        ],
      });
    return {
        info: logger.info.bind(logger),
        debug: logger.debug.bind(logger),
        trace: logger.silly.bind(logger),
        warn: logger.warn.bind(logger),
        error: logger.error.bind(logger),
        critical: logger.error.bind(logger)
    }
}

/**
 * Get the configuraiton obj, in this case it's mostly static for demo purposes,
 * but this could fetch from a file, fill properties in from a DB etc.
 * @param {string} logDir log directory for json chronicler
 * @param {string} logName log name for json chronicler
 * @return {IMaestroConfig} 
 */
function getConfig(logDir?: string, logName?: string) : IMaestroConfig {
    return {
        id: 'meastro-id',
        name: 'meastro-name',
        description: 'meastro description',
        formatSettings: {
            encrypted: false,
            type: 'N/A'
        },
        connections: [{
            emitters: [
                'ping-pong-1'
            ],
            chroniclers: [
                'json-chronicler-1'
            ]
        }],
        factories: {
            emitter: [{
                factoryType: PingPongEmitter.TYPE,
                factoryPath: 'PingPongEmitterFactory',
                packageName: '@curium.rocks/ping-pong-emitter',
            }],
            chronicler: [{
                factoryType: JsonChronicler.TYPE,
                factoryPath: 'JsonChroniclerFactory',
                packageName: '@curium.rocks/json-chronicler'
            }]
        },
        emitters: [{
            config: {
                type: PingPongEmitter.TYPE,
                id: 'ping-pong-1',
                name: 'My Ping Pong Emitter 1',
                description: "A ping pong emitter",
                emitterProperties: {
                    interval: 250
                }
            }
        }],
        chroniclers: [{
            config: {
                type: JsonChronicler.TYPE,
                id: 'json-chronicler-1',
                name: 'Chronicler 1',
                description: "A json chronicler",
                chroniclerProperties: {
                    logDirectory: logDir || './logs',
                    logName: logName || 'maestro',
                    rotationSettings: {
                        seconds: 300
                    }
                }
            }
        }]
    }
}

const maestroOptions = {
    config: getConfig(),
    logger: getLoggerFacade('maestro'),
    loadHandler: () => {
        return Promise.resolve(getConfig());
    }
}

Create the maestro

Once you have your configuration, you can create the maestro:

const maestro = new Maestro(maestroOptions);

Start the maestro

The meastro doesn't start automatically and you must call start, this refreshes it's configuration and starts any emitters as well.

await maestro.start();

Stop and cleanup on SIG INT

You can add a hook to clean up gracefully on SIG INT like so:

process.on('SIGINT', async () =>  {
    await maestro.disposeAsync();
})

Complete Example

import { IMaestro, LoggerFacade, ProviderSingleton } from "@curium.rocks/data-emitter-base";
import { JsonChronicler } from "@curium.rocks/json-chronicler";
import { PingPongEmitter } from "@curium.rocks/ping-pong-emitter";
import { IMaestroConfig, Maestro } from "@curium.rocks/maestro";
import winston from "winston";

/**
 * Create a logger facade wrapper winston
 * @param {string} serviceName 
 * @return {LoggerFacade}
 */
function getLoggerFacade(serviceName: string): LoggerFacade {
    const logger = winston.createLogger({
        level: 'info',
        format: winston.format.json(),
        defaultMeta: { service: serviceName },
        transports: [
          new winston.transports.Console()
        ],
      });
    return {
        info: logger.info.bind(logger),
        debug: logger.debug.bind(logger),
        trace: logger.silly.bind(logger),
        warn: logger.warn.bind(logger),
        error: logger.error.bind(logger),
        critical: logger.error.bind(logger)
    }
}

/**
 * Get the configuraiton obj, in this case it's mostly static for demo purposes,
 * but this could fetch from a file, fill properties in from a DB etc.
 * @param {string} logDir log directory for json chronicler
 * @param {string} logName log name for json chronicler
 * @return {IMaestroConfig} 
 */
function getConfig(logDir?: string, logName?: string) : IMaestroConfig {
    return {
        id: 'meastro-id',
        name: 'meastro-name',
        description: 'meastro description',
        formatSettings: {
            encrypted: false,
            type: 'N/A'
        },
        connections: [{
            emitters: [
                'ping-pong-1'
            ],
            chroniclers: [
                'json-chronicler-1'
            ]
        }],
        factories: {
            emitter: [{
                factoryType: PingPongEmitter.TYPE,
                factoryPath: 'PingPongEmitterFactory',
                packageName: '@curium.rocks/ping-pong-emitter',
            }],
            chronicler: [{
                factoryType: JsonChronicler.TYPE,
                factoryPath: 'JsonChroniclerFactory',
                packageName: '@curium.rocks/json-chronicler'
            }]
        },
        emitters: [{
            config: {
                type: PingPongEmitter.TYPE,
                id: 'ping-pong-1',
                name: 'My Ping Pong Emitter 1',
                description: "A ping pong emitter",
                emitterProperties: {
                    interval: 250
                }
            }
        }],
        chroniclers: [{
            config: {
                type: JsonChronicler.TYPE,
                id: 'json-chronicler-1',
                name: 'Chronicler 1',
                description: "A json chronicler",
                chroniclerProperties: {
                    logDirectory: logDir || './logs',
                    logName: logName || 'maestro',
                    rotationSettings: {
                        seconds: 300
                    }
                }
            }
        }]
    }
}

const maestroOptions = {
    config: getConfig(),
    logger: getLoggerFacade('maestro'),
    loadHandler: () => {
        return Promise.resolve(getConfig());
    }
}
ProviderSingleton.getInstance().setLoggerFacade(maestroOptions.logger);
const maestro:IMaestro = new Maestro(maestroOptions);
maestro.start();

process.on('SIGINT', async () => {
    await maestro.disposeAsync();
});

Docs

For more information generate the docs using npm run doc, documentation for each version is also attached as an artifact of the build in CI.

0.148.0

1 year ago

0.150.0

1 year ago

0.149.0

1 year ago

0.118.0

2 years ago

0.137.0

2 years ago

0.114.0

2 years ago

0.133.0

2 years ago

0.125.0

2 years ago

0.129.0

2 years ago

0.140.0

2 years ago

0.144.0

2 years ago

0.121.0

2 years ago

0.1.1-28

2 years ago

0.136.0

2 years ago

0.117.0

2 years ago

0.132.0

2 years ago

0.128.0

2 years ago

0.147.0

2 years ago

0.124.0

2 years ago

0.143.0

2 years ago

0.120.0

2 years ago

0.139.0

2 years ago

0.116.0

2 years ago

0.131.0

2 years ago

0.135.0

2 years ago

0.127.0

2 years ago

0.142.0

2 years ago

0.146.0

2 years ago

0.123.0

2 years ago

0.138.0

2 years ago

0.119.0

2 years ago

0.115.0

2 years ago

0.130.0

2 years ago

0.134.0

2 years ago

0.126.0

2 years ago

0.141.0

2 years ago

0.145.0

2 years ago

0.122.0

2 years ago

0.85.0

2 years ago

0.62.0

2 years ago

0.43.0

2 years ago

0.20.0

2 years ago

0.81.0

2 years ago

0.59.0

2 years ago

0.78.0

2 years ago

0.36.0

2 years ago

0.55.0

2 years ago

0.110.0

2 years ago

0.13.0

2 years ago

0.17.0

2 years ago

0.74.0

2 years ago

0.32.0

2 years ago

0.97.0

2 years ago

0.51.0

2 years ago

0.70.0

2 years ago

0.93.0

2 years ago

0.29.0

2 years ago

0.48.0

2 years ago

0.67.0

2 years ago

0.25.0

2 years ago

0.44.0

2 years ago

0.106.0

2 years ago

0.102.0

2 years ago

0.7.0

2 years ago

0.63.0

2 years ago

0.21.0

2 years ago

0.86.0

2 years ago

0.40.0

2 years ago

0.82.0

2 years ago

0.37.0

2 years ago

0.79.0

2 years ago

0.56.0

2 years ago

0.33.0

2 years ago

0.113.0

2 years ago

0.14.0

2 years ago

0.18.0

2 years ago

0.90.0

2 years ago

0.10.0

2 years ago

0.98.0

2 years ago

0.75.0

2 years ago

0.52.0

2 years ago

0.94.0

2 years ago

0.71.0

2 years ago

0.109.0

2 years ago

0.49.0

2 years ago

0.26.0

2 years ago

0.105.0

2 years ago

0.68.0

2 years ago

0.45.0

2 years ago

0.22.0

2 years ago

0.8.0

2 years ago

0.101.0

2 years ago

0.87.0

2 years ago

0.64.0

2 years ago

0.41.0

2 years ago

0.83.0

2 years ago

0.60.0

2 years ago

0.19.0

2 years ago

0.38.0

2 years ago

0.99.0

2 years ago

0.57.0

2 years ago

0.34.0

2 years ago

0.11.0

2 years ago

0.112.0

2 years ago

0.15.0

2 years ago

0.95.0

2 years ago

0.53.0

2 years ago

0.76.0

2 years ago

0.30.0

2 years ago

0.91.0

2 years ago

0.72.0

2 years ago

0.108.0

2 years ago

0.27.0

2 years ago

0.88.0

2 years ago

0.46.0

2 years ago

0.104.0

2 years ago

0.69.0

2 years ago

0.23.0

2 years ago

0.9.0

2 years ago

0.100.0

2 years ago

0.5.0

2 years ago

0.84.0

2 years ago

0.42.0

2 years ago

0.65.0

2 years ago

0.80.0

2 years ago

0.61.0

2 years ago

0.39.0

2 years ago

0.77.0

2 years ago

0.58.0

2 years ago

0.35.0

2 years ago

0.111.0

2 years ago

0.12.0

2 years ago

0.16.0

2 years ago

0.96.0

2 years ago

0.73.0

2 years ago

0.54.0

2 years ago

0.31.0

2 years ago

0.92.0

2 years ago

0.50.0

2 years ago

0.107.0

2 years ago

0.28.0

2 years ago

0.103.0

2 years ago

0.89.0

2 years ago

0.66.0

2 years ago

0.47.0

2 years ago

0.24.0

2 years ago

0.6.0

2 years ago

0.1.1-27

2 years ago

0.1.1-6

2 years ago

0.1.1-5

2 years ago

0.1.1-23

2 years ago

0.1.1-24

2 years ago

0.1.1-25

2 years ago

0.1.1-26

2 years ago

0.1.1-20

2 years ago

0.1.1-21

2 years ago

0.1.1-22

2 years ago

0.1.1-8

2 years ago

0.1.1-7

2 years ago

0.1.1-9

2 years ago

0.1.1-16

2 years ago

0.1.1-17

2 years ago

0.3.0

2 years ago

0.1.1-18

2 years ago

0.2.0

2 years ago

0.1.1-19

2 years ago

0.1.1-12

2 years ago

0.1.1-13

2 years ago

0.1.1-14

2 years ago

0.1.1-15

2 years ago

0.1.1-10

2 years ago

0.1.1-11

2 years ago

0.4.0

2 years ago

0.1.1-4

2 years ago

0.1.1-3

2 years ago

0.1.1-2

2 years ago

0.1.1-1

3 years ago

0.1.1-0

3 years ago