silofarm v1.0.1
SiloFarm
SiloFarm is a Node library to add, remove and view running worker threads, an experimental feature introduced by Node v10.5.0 onwards.
Here, each worker thread is a silo in the farm, containing grains of code.
IMPORTANT
Since this is an experimental feature, it is not meant to be used on enterprise applications. I shall update once Node releases a stable version.
CONTENTS
INSTALLATION
$ npm install silofarm
HOW TO USE
A new farm instance can be create by calling new Farm(logging [optional], limit [optional])
method. Logging means if you want SiloFarm to log all events occuring, where default is false. Limit is the maximum number of concurrent worker threads running to be allowed, default is 10.
In order to remove the limit, set it to Infinity
.
var { Farm } = require('silofarm')
let farm = new Farm(true, 5)
In order to create a silo, create a separate file running the code, and import parentPort and workerData to pass in values from the main thread, and send messages to the thread.
var { parentPort, workerData } = require('silofarm')
workerData.list.forEach(element => {
element.split(' ').forEach((elem, i) => {
if (elem === 'Vatsal' && elem !== undefined) {
parentPort.postMessage('Found instance at ' + i + ' instance')
}
})
})
In the main program, call the farm.addSilo(path, data [optional], events [optional], name [optional])
function with path as the absolute or relative path to the file.
farm.addSilo('path/to/file.js', { list: ['Vatsal Kandoi', 'Some other name', 'Third name'] }, {
error: (err) => {
console.log(err)
},
message: (data) => {
console.log(data)
}
}, 'checkNameInstance')
DOCUMENTATION
CREATING A NEW FARM INSTANCE
In order to create a new farm instance, responsible for creating the silos (workers)
let { Farm } = require('silofarm');
let farm = new Farm(logging [optional], limit [optional]);
Variables to be passed in the constructor
OPTION | TYPE | DEFAULT | STATUS |
---|---|---|---|
logging | boolean | false | OPTIONAL |
limit | number | 10 | OPTIONAL |
OPTION | MEANING |
---|---|
logging | Enabling console logs for all events |
limit | Number of maximum threads running simultaneously (excluding the main thread) |
ADDING A SILO
This method is called to add a new silo (worker) to the farm.
farm.addSilo(path,
data [optional],
events [optional],
name [optional],
options [optional],
callback [optional]
)
Variables to be passed in the function.
OPTION | TYPE | DEFAULT | STATUS |
---|---|---|---|
path | string | Assumes to be inline js | COMPULSORY |
data | JSON | {} | OPTIONAL |
events | { error: function(err) {}, exit: function(code) {}, online: function() {}, message: function(message) {} } | { } | OPTIONAL |
name | string | Generates random name | OPTIONAL |
options | { input, output, error, env } | { } | OPTIONAL |
callback | function | null | OPTIONAL |
Variable meanings.
OPTION | MEANING |
---|---|
path | Path to file to be run in the worker process |
data | data to be used by the code running in the worker process. Referenced as workerData.variable_name_defined_in_data_object |
events | Registering functions to be called on events emitted by the worker process like error, exit, online, message |
name | Name of the silo (worker thread) |
options | Defines input, output, and error references to worker threads |
callback | Function to be called after execution |
IMPORTANT
If path doesn't exits, SiloFarm assumes the string to be a JavaScript code, and is run in the silo.
More on options
OPTION | MEANING |
---|---|
input | Setting to true enables worker.stdin (Writable Stream) to be used as process.stdin inside worker |
output | Setting to true means process.stdout wont be piped to main thread's process.stdout but to worker.stdout (Readable stream) |
error | Setting to true means process.stderr wont be piped to main thread's process.stderr but worker.stderr (Readable stream) |
env | Setting this to true will enable you to modify env variables from inside the worker process. |
FORCE REMOVING A SILO
If you want to force stop a silo, run farm.destroySilo()
to force quit the worker thread.
farm.destroySilo(name, callback)
OPTIONS | TYPE | DEFAULT | STATUS |
---|---|---|---|
name | string | none | COMPULSORY |
callback | function | null | OPTIONAL |
CHECKING SILO BY STATUS
In order to check silos by status, farm.checkSilo()
is to be used.
This function returns or logs the silo of the particular status passed as a variable. status
can be active
or inactive
farm.checkSilo(status, log, callback)
OPTION | TYPE | DEFAULT | STATUS |
---|---|---|---|
status | string | active | OPTIONAL |
log | boolean | false | OPTIONAL |
callback | function | null | OPTIONAL |
CHECKING PARTICULAR SILO
In order to check running silos, farm.getSilo()
is to be used.
This function returns or logs the silo of the particular name passed as a variable, or returns /logs all silos created, including inactive ones.
farm.getSilo(name, log, callback)
OPTION | TYPE | DEFAULT | STATUS |
---|---|---|---|
name | string | all silos returned or logged | OPTIONAL |
log | boolean | false | OPTIONAL |
callback | function | null | OPTIONAL |
CHECKING PENDING SILOS
This function logs or returns a list of all pending silos to be created
farm.checkPendingSilos(log, callback)
OPTION | TYPE | DEFAULT | STATUS |
---|---|---|---|
log | boolean | returns list of pending silos | OPTIONAL |
callback | function | null | OPTIONAL |
CLEARING PENDING SILOS
This function removes all pending silo creation of a given name, or all by default.
farm.removePendingSilo(name)
OPTION | TYPE | DEFAULT | STATUS |
---|---|---|---|
name | string | all pending silos removed | OPTIONAL |