1.0.1 • Published 5 years ago

silofarm v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
5 years ago

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

OPTIONTYPEDEFAULTSTATUS
loggingbooleanfalseOPTIONAL
limitnumber10OPTIONAL
OPTIONMEANING
loggingEnabling console logs for all events
limitNumber 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.

OPTIONTYPEDEFAULTSTATUS
pathstringAssumes to be inline jsCOMPULSORY
dataJSON{}OPTIONAL
events{ error: function(err) {}, exit: function(code) {}, online: function() {}, message: function(message) {} }{ }OPTIONAL
namestringGenerates random nameOPTIONAL
options{ input, output, error, env }{ }OPTIONAL
callbackfunctionnullOPTIONAL

Variable meanings.

OPTIONMEANING
pathPath to file to be run in the worker process
datadata to be used by the code running in the worker process. Referenced as workerData.variable_name_defined_in_data_object
eventsRegistering functions to be called on events emitted by the worker process like error, exit, online, message
nameName of the silo (worker thread)
optionsDefines input, output, and error references to worker threads
callbackFunction 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

OPTIONMEANING
inputSetting to true enables worker.stdin (Writable Stream) to be used as process.stdin inside worker
outputSetting to true means process.stdout wont be piped to main thread's process.stdout but to worker.stdout (Readable stream)
errorSetting to true means process.stderr wont be piped to main thread's process.stderr but worker.stderr (Readable stream)
envSetting 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)
OPTIONSTYPEDEFAULTSTATUS
namestringnoneCOMPULSORY
callbackfunctionnullOPTIONAL

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)
OPTIONTYPEDEFAULTSTATUS
statusstringactiveOPTIONAL
logbooleanfalseOPTIONAL
callbackfunctionnullOPTIONAL

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)
OPTIONTYPEDEFAULTSTATUS
namestringall silos returned or loggedOPTIONAL
logbooleanfalseOPTIONAL
callbackfunctionnullOPTIONAL

CHECKING PENDING SILOS

This function logs or returns a list of all pending silos to be created

farm.checkPendingSilos(log, callback)
OPTIONTYPEDEFAULTSTATUS
logbooleanreturns list of pending silosOPTIONAL
callbackfunctionnullOPTIONAL

CLEARING PENDING SILOS

This function removes all pending silo creation of a given name, or all by default.

farm.removePendingSilo(name)
OPTIONTYPEDEFAULTSTATUS
namestringall pending silos removedOPTIONAL
1.0.1

5 years ago

1.0.0

5 years ago