all files / prolific.monitor/ monitor.bin.js

100% Statements 45/45
75% Branches 3/4
92.86% Functions 13/14
100% Lines 45/45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                                                                                           
#!/usr/bin/env node
 
/*
    ___ usage ___ en_US ___
    usage: node prolific.tcp.bin.js
 
        -i, --inherit   <number>        file handles to inherit
        -I, --ipc                       enable Node.js IPC forwarding
            --configuration <string>    base configuration JSON or environment variable
            --help                      display this message
 
    ___ $ ___ en_US ___
 
        log is required:
            the `--log` address and port is a required argument
 
        port is not an integer:
            the `--log` port must be an integer
 
    ___ . ___
*/
 
var pumper = require('prolific.pumper')
var children = require('child_process')
var inherit = require('prolific.inherit')
var ipc = require('prolific.ipc')
var url = require('url')
 
require('arguable')(module, require('cadence')(function (async, program) {
    var configure = require('./configure')
    var killer = require('./killer')
 
    program.helpIf(program.command.param.help)
 
    var configuration = configure(program.env, program.command.param.configuration)
 
    var inheritance = inherit(program)
    configuration.fd = inheritance.fd
 
    var isProgram = require('./programmatic')
    var argv = program.argv.slice(), terminal = false
    var loop = async(function () {
        if (isProgram(program, terminal, argv)) {
            process.env.PROLIFIC_CONFIGURATION = JSON.stringify(configuration)
            var nullProcessor = { process: function () {} }, nextProcessor = nullProcessor
            var processors = configuration.processors.map(function (configuration) {
                var Processor = require(configuration.moduleName)
                return nextProcessor = new Processor(configuration.parameters, nextProcessor)
            })
            processors.reverse()
            var initialized = []
            async([function () {
                async.forEach(function (processor) {
                    processor.close(async())
                })(initialized)
            }], function () {
                async.forEach(function (processor) {
                    async(function () {
                        processor.open(async())
                    }, function () {
                        initialized.push(processor)
                    })
                })(processors)
            }, function () {
                var child = children.spawn(argv.shift(), argv, { stdio: inheritance.stdio })
                // Let child shtudown to shut us down.
                program.on('SIGINT', killer(child, 'SIGINT'))
                program.on('SIGTERM', killer(child, 'SIGTERM'))
                var io = { async: child.stdio[inheritance.fd], sync: child.stderr }
                ipc(program.command.param.ipc, process, child)
                processors.push(nullProcessor)
                pumper(processors[0], child, io, program.stderr, async())
            }, function (code) {
                return [ loop.break, code ]
            })
        } else {
            var command = argv.shift()
            var parser = command[0] == '@'
                       ? require(command[0].substring(1))
                       : require('prolific.' + command + '/' + command + '.argv')
            async(function () {
                parser(argv, async())
            }, function (processor) {
                configuration.processors.push(processor)
                argv = processor.argv
                terminal = processor.terminal
            })
        }
    })()
}))