0.0.0 • Published 5 years ago

poolie v0.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

Poolie

Usage

const childProcess = require('child_process')

async function create(pool) {
  // spawn a long-running process
  const cmd = childProcess.spawn('./some-cmd')

  // if it exits, release back into the pool
  cmd.on('close', code => {
    pool.release(cmd)
  })

  // let the pool manage this process
  return cmd
}

// called when a timeout occurs
// or the pool is closed. passes
// the process in so you can
// try to destroy it
async function destroy(cmd) {
  // psuedo-code below
  await cmd.signal('SIGTERM')
  return await cmd.wait()
}

const pool = new Pool({
  // capacity affects how many outstanding
  // resources can be acquired. when the pool
  // is full, pool.acquire() will wait until
  // we've called pool.release() or a resource
  // has been destroyed
  capacity: 8,
  destroy: destroy,
  create: create,
  // timeout isn't implemented yet, but should be
  timeout: 2000
})

// do something with this acquired resource
const cmd = await pool.acquire()

// wait until the pool has been drained (empty)
await pool.wait()

// close the pool, calling destroy
await pool.close()
0.0.0

5 years ago