Created
April 25, 2016 17:16
-
-
Save mweagle/4b6a656c94772a4e06e605353c06fb57 to your computer and use it in GitHub Desktop.
This file contains 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 createForwarder = function(path) { | |
var forwardToGolangProcess = function(event, context, metricName) | |
{ | |
if (!golangProcess) { | |
ensureGoLangBinary(function() { | |
golangProcess = child_process.spawn(SPARTA_BINARY_PATH, ['execute', '--signal', process.pid], {}); | |
golangProcess.stdout.on('data', function(buf) { | |
buf.toString('utf-8').split('\n').forEach(function (eachLine) { | |
sparta_utils.log(eachLine); | |
}); | |
}); | |
golangProcess.stderr.on('data', function(buf) { | |
buf.toString('utf-8').split('\n').forEach(function (eachLine) { | |
sparta_utils.log(eachLine); | |
}); | |
}); | |
var terminationHandler = function(eventName) { | |
return function(value) { | |
var onPosted = function() { | |
console.error(util.format('Sparta %s: %s\n', eventName.toUpperCase(), JSON.stringify(value))); | |
failCount += 1; | |
if (failCount > MAXIMUM_RESPAWN_COUNT) { | |
process.exit(1); | |
} | |
golangProcess = null; | |
forwardToGolangProcess(null, null, METRIC_NAMES.TERMINATED); | |
}; | |
postMetricCounter(METRIC_NAMES.TERMINATED, onPosted); | |
}; | |
}; | |
golangProcess.on('error', terminationHandler('error')); | |
golangProcess.on('exit', terminationHandler('exit')); | |
process.on('exit', function() { | |
if (golangProcess) { | |
golangProcess.kill(); | |
} | |
}); | |
var golangProcessReadyHandler = function() { | |
process.removeListener('SIGUSR2', golangProcessReadyHandler); | |
forwardToGolangProcess(event, context, METRIC_NAMES.CREATED); | |
}; | |
process.on('SIGUSR2', golangProcessReadyHandler); | |
}); | |
} | |
else if (event && context) | |
{ | |
postMetricCounter(metricName || METRIC_NAMES.REUSED); | |
makeRequest(path, event, context); | |
} | |
}; | |
return forwardToGolangProcess; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment