Skip to content

Commit

Permalink
Merge pull request #27 from pcnate/fork2spawn
Browse files Browse the repository at this point in the history
Fork2spawn
  • Loading branch information
pcnate authored Dec 3, 2018
2 parents 34d4733 + 9bdd068 commit c5b6d95
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
38 changes: 24 additions & 14 deletions app/proxy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fork = require( 'child_process' ).fork;
const spawn = require( 'child_process' ).spawn;
const path = require('path');

var proxyProcess = null;
Expand All @@ -11,7 +11,7 @@ var redbirdPID = 0;
var handleLogs = () => {};

function registerLogHandler( handler ) {
if( typeof handler !== 'function' ) {
if ( typeof handler !== 'function' ) {
return;
}
handleLogs = handler;
Expand All @@ -33,12 +33,12 @@ function countRequest( host, url ) {
let matchedRoutes = allRoutes.filter( m => url.indexOf( m ) > -1 );

// sort by longest so best result comes first if there is more than one
if( matchedRoutes.length > 1 ) {
if ( matchedRoutes.length > 1 ) {
matchedRoutes.sort( ( a, b) => b.length - a.length );
}

// validadate enough results
if( matchedRoutes.length < 1 ) {
if ( matchedRoutes.length < 1 ) {
return;
}

Expand Down Expand Up @@ -80,18 +80,18 @@ function findResponder( message ) {
let success = message.success;

matches = responderRegistry.filter( m => m.identifier === identifier );
if( matches.length > 0 ) {
if ( matches.length > 0 ) {

matches = matches[0];

if( success ) {
if ( success ) {
if ( typeof matches.success === 'function' ) {
matches.success();
responderRegistry = responderRegistry.filter( m => m.identifier !== identifier );
}
}

if( !success ) {
if ( !success ) {
if ( typeof matches.error === 'function' ) {
matches.error();
responderRegistry = responderRegistry.filter( m => m.identifier !== identifier );
Expand All @@ -107,30 +107,40 @@ function findResponder( message ) {
*/
function launchProxy() {
return new Promise( async ( resolve, reject ) => {
proxyProcess = await fork( path.join( __dirname, 'proxyWorker.js' ), { silent: false });
proxyProcess = await spawn( 'node', [ path.join( __dirname, 'proxyWorker.js' ) ], {
windowsHide: true,
stdio: [ 'inherit', 'inherit', 'inherit', 'ipc' ],
});


if ( typeof proxyProcess.stderr !== 'undefined' && proxyProcess.stderr !== null ) {
proxyProcess.stderr.on( 'data', ( error ) => {
console.error( 'spawn error', error.toString() );
});
}

if (typeof proxyProcess.stdout !== 'undefined' && proxyProcess.stdout !== null ) {
if ( typeof proxyProcess.stdout !== 'undefined' && proxyProcess.stdout !== null ) {
proxyProcess.stdout.on('data', data => {})
}

// watch for messages
proxyProcess.on( 'message', message => {
if( typeof message === 'object' ) {
if ( typeof message === 'object' ) {

if( typeof message.type !== 'undefined' && message.type === 'processReady' ) {
if ( typeof message.type !== 'undefined' && message.type === 'processReady' ) {
redbirdPID = message.PID;
resolve();
return;
}

if( message.output ) {
if ( message.output ) {
console.log( 'worker msg', message.output );
}

if( typeof message.type !== 'undefined' && message.type === 'registration_done' ) {
if ( typeof message.type !== 'undefined' && message.type === 'registration_done' ) {
findResponder( message );
}
if( typeof message.type !== 'undefined' && message.type === 'unregistration_done' ) {
if ( typeof message.type !== 'undefined' && message.type === 'unregistration_done' ) {
findResponder( message );
}

Expand Down
2 changes: 2 additions & 0 deletions app/proxyWorker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

// listen for new messages
process.on('message', async message => {

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "red-ampp",
"version": "1.1.1",
"version": "1.1.2",
"description": "developer tool for running micro services behind a redbird proxy",
"main": "server.js",
"scripts": {
Expand Down

0 comments on commit c5b6d95

Please sign in to comment.