New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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

  • 0.1.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
14
decreased by-50%
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){ })

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])

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

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])

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.on("request", function(message, data, response){ })

Handling request from parent

message message from request.

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

  • message Message name.
  • data Data payload
  • response Callback function. Invoke to send response to parent.

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 file path.

  • child.get() get ChildProcess object.

  • child.start(data) start child process with data as argument (optional).

  • child.stop(data) stop child process with data as argument (optional).

  • 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.

Examples

master.js

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

/* Handle child message. */
child.on("message", function (message, data) {
    /* Go message from child. */
    console.log("[Child::Message]: Got 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 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("------------------------------");
        /* 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?");
    }
});

/* 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 stop event. */
partner.on("message", function (message, data, master) {
    if (master) {
        console.log("[Message]: Got from itself: " + (JSON.stringify({
            message: message,
            data: data
        })));
    } else {
        console.log("[Message]: Got from parent: " + (JSON.stringify({
            message: message,
            data: data
        })));
    }
});

/* Start entry point. */
partner.start("Master::Start");

TODO

License

MIT

Keywords

FAQs

Package last updated on 31 Jul 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