partner
v0.6.1
Published
Child process management. Support to create, manage, interact between child process.
Downloads
1,883
Maintainers
Readme
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, done)
callback function.
data
Start argument.master
false it is child process otherwise callback is invoked frompartner.start([data], [function(error){ }])
done(error)
callback notify to master.
partner.on("stop", function(data, master, done)){ })
Handling stop event.
stop
stop event.
function(data, master, done)
callback function.
data
Stop argument.master
false it is child process otherwise callback is invoked frompartner.stop([data], [function(error){ }])
done(error)
callback notify to master.
partner.on("message", function(message, data, master){ })
Handling message.
message
message event.
function(message, data, master)
callback function.
message
Message name.data
Data payloadmaster
false it is child process otherwise callback is invoked frompartner.send(message, data, [sendHandle])
partner.handle(function(message, data, response){ }
)
Handling request from parent
function(message, data, response)
callback function.
message
Message name.data
Data payloadresponse
Callback function(error, data). Invoke to send response to parent.
partner.request(message, data, function(error, data, message){ }
)
Request parent handle.
message
Message name.data
Data payloadfunction(error, data, message)
callback function.
partner.with(modulePath|alias
, [strategy])
Asking parent dispatches request/message from other children by module path or alias.
module|alias
Module file path or alias 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, data, message)){ }
) Request handlefriend.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|alias
, [strategy]);
Select a child by file path or alias and request handle with strategy.
module|alias
Module file path or alias 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, data, message)){ }
) Request handlechild.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], [function(error){}])
Sending current process. If itself is child process then do nothing, otherwise raise "stop" event for itself.
data
Optional data payload.function(error)
callback function.
partner.start([data], [function(error){}])
Start current process. If itself is child process then do nothing, otherwise raise "start" event for itself.
data
Optional data payload.function(error)
callback function.
partner.list(): Array
List all children process.
partner.create(modulePath
, [alias]): object
Create child process with JavaScript file path, alias. Return new partner object with methods:
var child = partner.create("file://./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 sendHandlerchild.request(message, data, function(error, data, message){ })
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(errors, 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
TODO
- Support socket.io to connect other process cross network.
License
MIT