nsolid-manager
Version:
A manager that sets up and runs N|Solid for you
140 lines (114 loc) • 3.7 kB
JavaScript
const hub = require('./hub')
const getStorage = require('./storage')
const buildResponse = require('./build-response')
const EventEmitter = require('events').EventEmitter
const graphCache = require('./graph-cache')
const async = require('async')
const camelize = require('camelize')
const commands = [
'info',
'system_info',
'system_stats',
'process_stats'
]
module.exports = poll
function poll (options, cache, broadcast) {
var ee = new EventEmitter()
var pollInterval = options.interval
var duration = options.duration
var storagePath = options.storage
var timer = null
var stopped = false
var hubErrors = []
function run () {
async.mapSeries(commands, function performBroadcast (command, next) {
(broadcast || hub.broadcast)(command, options, (errors, r) => {
if (errors && errors.length) {
errors.forEach((o) => {
var key = o.hub + o.error.message
if (hubErrors.indexOf(key) === -1) {
console.error('POLLING ERROR:', o.error.message)
hubErrors.push(key)
}
})
}
next(null, r)
})
}, function (err, commandResults) {
if (err) {
return setTimeout(run, pollInterval)
}
var results = {}
commands.forEach(function (name, i) {
results[name] = clean(commandResults[i])
})
ee.emit('input', results)
cache.collectPollingResult(function collect (pollingCache) {
const hosts = pollingCache.hosts
results.info.forEach(function collectInfo (info) {
const tags = info.reply.tags || []
const p = results.process_stats.find(p => p.meta.id === info.meta.id)
if (!p) return
p.meta.tags = tags
})
results.system_info.forEach(function collectSystemInfo (system) {
const host = system.reply
const hostname = host.hostname
if (!hosts[hostname]) {
hosts[hostname] = host
}
})
results.system_stats.forEach(function collectSystemStats (system) {
const hostname = system.meta.hostname
const host = system.reply
if (!hosts[hostname]) return
if (!hosts[hostname].stats) {
hosts[hostname].stats = host
const totalmem = hosts[hostname].totalmem
host.memoryPercentageUsed = ((totalmem - host.freemem) / totalmem) * 100
}
})
buildResponse(pollingCache, results.process_stats, duration)
})
ee.emit('output', cache.toJSON())
// for testing
if (!stopped && pollInterval !== false) {
timer = setTimeout(run, pollInterval)
}
})
}
let graphDB = null
// kick it off
if (pollInterval !== 'never') {
getStorage(storagePath, 'graphs', options, function gotStorage (err, db) {
if (err) ee.emit('error', err)
graphDB = db
graphCache.init(graphDB, duration, run)
})
}
ee.once('stop', onStop => {
stopped = true
clearTimeout(timer)
if (!graphDB) return ee.emit('stopped')
graphDB.close(err => {
if (err) ee.emit('error', err)
ee.emit('stopped')
})
})
return ee
}
function clean (a) {
// Until we can land a fix for https://github.com/nodesource/nsolid-epm-cli/issues/2
if (!a) {
return []
}
return a.filter(function (o) {
// TODO: it's likely better to show the errors on the client if something
// bad is happening. In all of the cases I've seen so far it's just
// an inability to resolve an ipv6 address (derp)
return !!o.reply
}).map(function (o) {
o.reply = camelize(o.reply)
return o
})
}