Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

partner

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

partner

Child process management. Support to create, manage, interact between child process.

  • 0.5.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
456
increased by7.55%
Maintainers
1
Weekly downloads
 
Created
Source

partner

Wrapping require("child_process") module. Supporting to manage child process status such as start, stop. Handle message, exit status, request/response between parent and child.

Installation

npm install partner

Usage

var partner = require("partner");

partner.on("start", function(data, master, done){ })

Handling start event.

start start event.

function(data, master) is a callback function.

  • data Start argument.
  • master false it is child process otherwise callback is invoked from partner.start([data], [function(error, data){ }])
  • done(error, data) callback notify to master.

partner.on("stop", function(data, master, done)){ })

Handling stop event.

stop stop event.

function(data, master) is a callback function.

  • data Stop argument.
  • master false it is child process otherwise callback is invoked from partner.stop([data], [function(error, data){ }])
  • done(error, data) callback notify to master.

partner.on("message", function(message, data, master){ })

Handling message.

message message event.

function(message, data, master) is a callback function.

  • message Message name.
  • data Data payload
  • master false it is child process otherwise callback is invoked from partner.send(message, data, [sendHandle])

partner.handle(function(message, data, response){ })

Handling request from parent

function(message, data, response) is a callback function.

  • message Message name.
  • data Data payload
  • response Callback function(error, data). Invoke to send response to parent.

partner.request(message, data, function(error, message, data)){ }`)

Request parent handle.

  • message Message name.
  • data Data payload
  • function(error, message, data) is a callback function.

partner.with(modulePath, [strategy])

Asking parent dispatches request/message from other children.

  • module Module file path to handle request.
  • strategy Strategy for select partner to handle. There are: "first", "last", "next", "random", index-number

var friend = partner.with("modulePath", [strategy])

  • friend.request(message, data, function(error, message, data)){ }) Request handle
  • friend.send(message, data, [sendHandle]) Send message.
  • friend.register(function(message, data) { ... }) Register handle message.
  • friend.unRegister(function(message, data) { ... }) Unregister handle message.

partner.select(modulePath, [strategy]);

Select a child by file path and request handle with strategy.

  • module Module file path to handle request.
  • strategy Strategy for select partner to handle. There are: "first", "last", "next", "random", index-number

var child = partner.select("modulePath", [strategy])

  • child.request(message, data, function(error, message, data)){ }) Request handle
  • child.send(message, data, [sendHandle]) Send message.

partner.send(message, data, [sendHandle])

Sending message. If itself is child process then send back to parent, otherwise raise "message" event for itself.

  • message Message in string format.
  • data Data payload.

partner.broadcast(message, data, [sendHandle])

Sending message for all children process.

  • message Message in string format.
  • data Data payload.

partner.stop([data])

Sending current process. If itself is child process then do nothing, otherwise raise "stop" event for itself.

  • data Optional data payload.

partner.start([data])

Start current process. If itself is child process then do nothing, otherwise raise "start" event for itself.

  • data Optional data payload.

partner.list(): Array

List all children process.

partner.create(modulePath): object

Create child process with JavaScript file path. Return new partner object with methods:

var child = partner.create("./child.js");

  • child.setOptions(options) set options for child process like: child_process.fork(modulePath, [args], [options]).

  • child.getOptions() get options.

  • child.setArguments(args) set arguments for child process like: child_process.fork(modulePath, [args], [options]).

  • child.getArguments() get arguments.

  • child.getFile() get absolute file path.

  • child.get() get ChildProcess object.

  • child.start([data], [callback]) start child process with argument data. Callback if partner invoke done([error], [data])

  • child.stop([data], [callback]) stop child process with argument data. Callback if partner invoke done([error], [data])

  • child.send(message, data, [sendHandle]) send message to child process with data and sendHandler

  • child.request(message, data, function(error, message, data){ }) request child process message and waiting for response in callback.

  • child.broadcast(message, data, [sendHandle]) send message to all children process except itself with data and sendHandler.

  • child.exit() force child process must stop.

  • child.on("message", function(message, data){} ) handle message event is sent back from child.

  • child.on("exit", function(data){} ) handle exit event if child is kill or die. Data is start data.

  • child.handle(function(message, data, response){ }) handle request from child.

Examples

master.js


var partner = require("./index");
var child = partner.create("master.js");

/* Handle child message. */

child.on("message", function (message, data) {

	/* Go message from child. */
	console.log("------------------------------");
	console.log("[Child::Message]: Got message from child: " + (JSON.stringify({
		message: message,
		data: data
	})));
});


/* Handle child exit. */

child.on("exit", function (data) {

	/* Go message from child. */
	console.log("[Child::Exit]: Child is killed. Start again with arguments: " + (JSON.stringify(data)));

	/* Start again. */
	this.start(data);
	console.log("------------------------------");
});


/* Handle child request. */

child.handle(function (message, data, response) {

	/* Go request from child. */
	console.log("[Master::Request]: Got request from child: " + (JSON.stringify({
		message: message,
		data: data
	})));
	switch (message) {
		case "Multiply":
			response(null, data.first * data.second);
		default:
			response("Master don't handle it!");
	}
});


/* Handle start event. */

partner.on("start", function (data, master) {
	if (master) {
		console.log("[Start]: I'm master: " + (JSON.stringify(data)));

		/* Start child if itself is master. */
		child.start("Child::Start");

		/* Send message to child. */
		child.send("Hello", "How are you?");
		console.log("------------------------------");

		/* Request message to child process. */
		child.request("Sum", {
			first: 1,
			second: 2
		}, function (error, message, data) {
			console.log("------------------------------");
			if (error != null) {
				console.error("[" + message + "::Master::Response]: Got error: " + error);
			}
			console.log("[" + message + "::Master::Response]: " + (JSON.stringify({
				message: message,
				data: data
			})));
		});

		/* Request message to child process with no handle. */
		child.request("NoHandler", "Data", function (error, message, data) {
			console.log("------------------------------");
			if (error != null) {
				console.error("[" + message + "::Master::Response]: Got error: " + error);
			}
			console.log("[" + message + "::Master::Response]: " + (JSON.stringify({
				message: message,
				data: data
			})));
		});

		/* Stop entry point after 6 second. */
		setTimeout(function () {
			console.log("------------------------------");
			partner.stop("Master::Stop");
		}, 3000);

		/* Kill child after 2 second. */
		setTimeout(function () {
			console.log("------------------------------");
			console.log("            child.get().kill()");
			child.get().kill();
		}, 2000);
	} else {
		console.log("[Start]: I'm child: " + (JSON.stringify(data)));

		/* Send to itself . */
		console.log("------------------------------");
		partner.send("???", "Who am I?");

		/* Ask master request. */
		partner.request("Multiply", {
			first: 10,
			second: 10
		}, function (error, message, data) {
			console.log("------------------------------");
			if (error != null) {
				console.error("[" + message + "::Child::Response]: Got error: " + error);
			}
			console.log("[" + message + "::Child::Response]: " + (JSON.stringify({
				message: message,
				data: data
			})));
		});

		/* Request message to child process with no handle. */
		partner.request("Reject", "Data", function (error, message, data) {
			console.log("------------------------------");
			if (error != null) {
				console.error("[" + message + "::Child::Response]: Got error: " + error);
			}
			console.log("[" + message + "::Child::Response]: " + (JSON.stringify({
				message: message,
				data: data
			})));
		});
	}
});


/* Handle stop event. */

partner.on("stop", function (data, master) {
	if (master) {

		/* Stop child if itself is master. */
		child.stop("Child::Stop");
		console.log("[Stop]: I'm master: " + (JSON.stringify(data)));

		/* Stop child after 1 second. */
		setTimeout(function () {
			console.log("------------------------------");
			console.log("            child.stop('Child::Stop')");
			child.stop("Child::Stop");
		}, 2000);

		/* Force kill child after 3 second. */
		setTimeout(function () {
			console.log("------------------------------");
			console.log("            child.exit()");
			child.exit();
		}, 3000);
	} else {
		console.log("[Stop]: I'm child: " + (JSON.stringify(data)));
	}
});


/* Handle message event. */

partner.on("message", function (message, data, master) {
	if (master) {
		console.log("[Message]: Got message from itself: " + (JSON.stringify({
			message: message,
			data: data
		})));
	} else {
		console.log("[Message]: Got message from parent: " + (JSON.stringify({
			message: message,
			data: data
		})));
	}
});


/* Handle default request */

partner.handle(function (message, data, response) {

	/* Go request from child. */
	console.log("[Child::Request]: Got request from parent: " + (JSON.stringify({
		message: message,
		data: data
	})));
	switch (message) {
		case "Sum":
			response(null, data.first + data.second);
		default:
			response("I don't handle it!");
	}
});


/* Start entry point. */

partner.start("Master::Start");

TODO

License

MIT

Keywords

FAQs

Package last updated on 26 Nov 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc