node-crew

0.0.3 • Public • Published

Crew

Manage workers on a queue.

Installation

    $ npm install node-crew
  • crew.Pool([options])

    • maxProcs Number Max concurrent workers
    • dieOnError Boolean (true) Exit main process when a worker has an error
    • dieOnEmpty Boolean (true) Exit main process when there on no workers in the queue or active
    • cache Boolean (true) Save the process id, unique id, path, and error or exit signal for each worker
  • crew.Worker([options])

    • path String Path to the child script
    • data Object|Array|String|Number|Boolean Data to send to the process
    • exit Function (instance, child, code, signal) "exit" Event handler, invoked when the worker process is exited
    • error Function (error, worker, child) "error" Event handler, invoked when the worker process has an error
    • message Function (message, worker, child) "message" Event Handler, invoked when worker process sends data to the main process
    • args Array Passed straight to node's ChildProcess node docs
    • options Object Passed to node's ChildProcess node docs

Pool

    var pool = new crew.Pool();
  • Pool#addWorker([options])

    Options can be any valid arguments for the Worker constructor or a Worker instance. Worker is added to the queue. Returns the Worker instance.
  • Pool#drain()

    Block workers from being started until the pool is empty
  • Pool#getCache()

    Returns cache for all completed workers.

Worker

  • Worker#setPool(pool)

    Attach the worker to a Pool instance. Pool#addWorker will do the same thing.
  • Worker#kill

    Kill a workers child process, the worker instance will emit "terminated" when the child process is killed.
        worker.kill()
          .on('terminated', function () {
           // child process is exited
        })
  • Worker#data(data)

    • data Object|Array|String|Number|Boolean Set the data that should be initially sent to the workers child process.
  • Worker#send(data)

    • data Object|Array|String|Number|Boolean Send data to the workers child process. If the workers child process is started the data will be sent, otherwise the data will be sent when the child process starts.
        var worker = new crew.Worker({
            path: './child',
            data: { name: 'Nick' }
        });
     
        // events
        worker.on('exit', function (worker, process, code, signal) {
            // exit handler
        })
        .on('error', function (err, worker, process) {
            // error handler
        })
        .on('message', function (message, worker, instance) {
            // message handler
        });
     
        pool.addWorker(worker);

Package Sidebar

Install

npm i node-crew

Weekly Downloads

2

Version

0.0.3

License

none

Last publish

Collaborators

  • nindaff