Skip to content

๐Ÿ”Œ Instance Control - A NPM module to wrap around node's child process spawn functions.

License

Notifications You must be signed in to change notification settings

tkodev/lib-instance-control

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

18 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Instance Control

  • ๐Ÿ” Instance Control - A NPM module to wrap around node's child process spawn functions.
    • Just a wrapper to start and stop process instances.
    • Not for public production! Please fork for personal use! - This module is for @htkoca's private development.

Usage Summary

  • Prep: Load the module
  • Step 1: Instance creation and termination
  • Step 2: General instance functions

Prep

Let's install the module

  • in terminal: npm install instance-control

Step 1

Instance creation and termination

// dependencies
const InstanceCtrl = require('instance-control')

// create new controller
let instanceCtrl = new InstanceCtrl();

// start new child instances with name string, returns node native <ChildProcess>
let subProcess = instanceCtrl.startInstance('nodeApp', `node example-process.js`)
let subProcess2 = instanceCtrl.startInstance('nodeApp', `node example-process.js`)

// let's get a console log when process exited
subProcess.on('exit', (code, signal) => {
	console.log(`subProcess exited with code: '${code}', signal: '${signal}'`);
});

// get all instances under 'nodeApp' child name, returns: array of <ChildProcess>'s
let instances = instanceCtrl.getInstances('nodeApp');

// kill all instances under 'nodeApp' child name, returns: true if any kill signal sent
let killStatus = instanceCtrl.killInstances('nodeApp');

Step 2

General instance functions

// start another instance to test global functions
instanceCtrl.startInstance('nodeApp', `node example-process.js`);

// get all instances, returns: object with child name as property, process arrays as value
let allInstances = instanceCtrl.getAll();

// kill all instances, returns: object with child name as property, process arrays as value
let killStatusAll = instanceCtrl.killAll();

Step 3

Single instance functions - promise based

// start only one process - each command kills previous app
(async function() {
  let subProcess = await instanceCtrl.singleInstance('nodeApp', `node example-process.js`);
	let subProcess2 = await instanceCtrl.singleInstance('nodeApp', `node example-process.js`);
	let subProcess3 = await instanceCtrl.singleInstance('nodeApp', `node example-process.js`);
	console.log(subProcess3);
})();

About

๐Ÿ”Œ Instance Control - A NPM module to wrap around node's child process spawn functions.

Resources

License

Stars

Watchers

Forks

Packages

No packages published