Created
January 17, 2021 18:41
-
-
Save gx0r/7d1cfc1fa8e35ae107603c55b3232c11 to your computer and use it in GitHub Desktop.
Talking process monitorr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var chalk = require('chalk'); | |
var lh = 'localhost'; | |
var EventEmitter = require('events').EventEmitter; | |
//var exec = require('child_process').exec; | |
var execSync = require('child_process').execSync; | |
var warn = console.log | |
var ee = new EventEmitter(); | |
ee.on('down', function(serviceName, had_error) { | |
if (serviceLast[serviceName] != 'down') { | |
execSync('say ' + serviceName + ' is down!'); | |
var time = new Date().toJSON(); | |
if (had_error) | |
warn(chalk.red(time + ' ' + serviceName + ' down, had error')); | |
else | |
warn(chalk.red(time + ' ' + serviceName + ' down')); | |
serviceLast[serviceName] = 'down'; | |
} | |
}); | |
ee.on('up', function(serviceName) { | |
if (serviceLast[serviceName] != 'up') { | |
execSync('say ' + serviceName + ' is up'); | |
var time = new Date().toJSON(); | |
warn(chalk.green(time + ' ' + serviceName + ' up')); | |
serviceLast[serviceName] = 'up'; | |
} | |
}); | |
var serviceLast = {}; | |
function testLocalService(name, port, count, RETRY_COUNT, RETRY_DELAY) { | |
count = count || 0; | |
var had_error = had_error || false; | |
RETRY_COUNT = RETRY_COUNT || 3; | |
RETRY_DELAY = RETRY_DELAY || 300; | |
var socket = net.createConnection(port, 'localhost'); | |
socket.setKeepAlive(true); | |
var retry = function(count) { | |
setTimeout(function() { | |
// console.log('retry ' + name + ' count: ' + count); | |
count++; | |
if (count > RETRY_COUNT) { | |
ee.emit('down', name, had_error); | |
count = 0; | |
} | |
setTimeout(function() { | |
testLocalService(name, port, count, RETRY_COUNT, | |
RETRY_DELAY) | |
}, RETRY_DELAY); | |
}, 0) | |
}; | |
socket.on('close', function(had_error) { | |
retry(count); | |
}); | |
socket.on('connect', function() { | |
// console.log('up ' + name); | |
count = 0; | |
ee.emit('up', name); | |
}); | |
socket.on('error', function(err) { | |
// warn(name + ' ' + err); | |
}); | |
socket.on('timeout', function() { | |
socket.destroy(); | |
retry(); | |
}); | |
} | |
//testLocalService('search-indexer', 7532); | |
testLocalService('active MQ', 61616); | |
testLocalService('oracle', 1521); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment