time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

worker.js (2271B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var logger = require( 'debug' );
     24 var proc = require( './process.js' );
     25 var db = require( './children.js' );
     26 var closeProcess = require( './close.js' );
     27 var exec = require( './exec.js' );
     28 var spawn = require( './spawn.js' );
     29 
     30 
     31 // VARIABLES //
     32 
     33 var debug = logger( 'parallel:worker' );
     34 
     35 
     36 // FUNCTIONS //
     37 
     38 /**
     39 * Callback invoked upon receiving a message from a parent process.
     40 *
     41 * @private
     42 * @param {string} message - message from parent
     43 * @returns {void}
     44 */
     45 function onMessage( message ) {
     46 	var child;
     47 	if ( message === 'close' ) {
     48 		debug( 'Received a message to close. Closing...' );
     49 		return closeProcess();
     50 	}
     51 	debug( 'Received a message to run a script: %s. pid: %s.', message, proc.pid );
     52 	if ( proc.env.WORKER_ORDERED ) {
     53 		debug( 'Environment configuration specifies to preserve script output.' );
     54 		child = exec( proc.env.WORKER_CMD, message, done );
     55 	} else {
     56 		debug( 'Environment configuration allows interleaved script output.' );
     57 		child = spawn( proc.env.WORKER_CMD, message, done );
     58 	}
     59 	db[ child.pid ] = true;
     60 }
     61 
     62 /**
     63 * Callback invoked once a child process finishes.
     64 *
     65 * @private
     66 * @param {(Error|null)} error - error object
     67 * @param {number} pid - child process id
     68 * @param {string} script - script filepath (i.e., identifier)
     69 * @returns {void}
     70 */
     71 function done( error, pid, script ) {
     72 	delete db[ pid ];
     73 	if ( error ) {
     74 		closeProcess();
     75 		return proc.emit( 'error', error );
     76 	}
     77 	// Inform the parent process that the script has finished:
     78 	proc.send( script );
     79 }
     80 
     81 
     82 // MAIN //
     83 
     84 /**
     85 * Main script.
     86 *
     87 * @private
     88 */
     89 function main() {
     90 	proc.on( 'message', onMessage );
     91 }
     92 
     93 
     94 // EXPORTS //
     95 
     96 module.exports = main;