UNPKG

2.19 kBJavaScriptView Raw
1/**
2 * @module monitoring api
3 */
4
5var EventEmitter = require('events').EventEmitter;
6var _ = require('lodash');
7
8var application = require('./application');
9var properties = require('./properties');
10var logger = require('./logger')('hubiquitus:core:monitoring');
11var actors = require('./actors');
12var discovery = require('./discovery');
13var adapters = {
14 inproc: require('./adapters/inproc'),
15 remote: require('./adapters/remote')
16};
17
18exports.__proto__ = new EventEmitter();
19exports.setMaxListeners(0);
20
21/*
22 * Listeners setup
23 */
24
25actors.on('actor added', function (aid, scope) {
26 exports.emit('actor added', aid, scope);
27});
28
29actors.on('actor removed', function (aid, scope) {
30 exports.emit('actor removed', aid, scope);
31});
32
33discovery.on('discovery started', function (aid) {
34 exports.emit('discovery started', aid);
35});
36
37discovery.on('discovery stopped', function (aid) {
38 exports.emit('discovery stopped', aid);
39});
40
41discovery.on('discovery', function (aid) {
42 exports.emit('discovery', aid);
43});
44
45_.forEach(adapters, function (adapter) {
46 adapter.on('req sent', function (req) {
47 exports.emit('req sent', req);
48 });
49
50 adapter.on('req received', function (req) {
51 exports.emit('req received', req);
52 });
53
54 adapter.on('res sent', function (res) {
55 exports.emit('res sent', res);
56 });
57
58 adapter.on('res received', function (res) {
59 exports.emit('res received', res);
60 });
61});
62
63/**
64 * Returns actors in the given scope
65 * @param {string} [scope]
66 * @returns {Array} actors
67 */
68exports.actors = function (scope) {
69 return actors.all(scope);
70};
71
72/**
73 * Returns current discoveries with the last research date for each of them
74 * @returns {Object} discoveries
75 */
76exports.discoveries = function () {
77 return discovery.discoveries();
78};
79
80/*
81 * Ping management
82 */
83
84application.addActor(properties.ID + '_ping', function (req) {
85 logger.makeLog('trace', 'hub-51', 'ping', {req: req});
86 req.reply();
87});
88
89/**
90 *
91 * @param cid {String} target container id
92 * @param cb {Function} callback
93 */
94exports.pingContainer = function (cid, cb) {
95 application.send(properties.ID, cid + '_ping', 'PING', properties.containerPingTimeout, function (err) {
96 cb(err);
97 });
98};