Skip to content

Management wrapper for unreliable resources, e.g., another process.

Notifications You must be signed in to change notification settings

unlib-js/unreliable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unreliable

Management wrapper for unreliable resources, e.g., another process.

Example

import { ChildProcess, spawn } from 'child_process'
import Unreliable from '@unlib-js/unreliable'
import stopOnExit from '@unlib-js/unreliable/build/stopOnExit'
import { once } from 'events'


class Proc extends Unreliable<ChildProcess> {
  static meta: UnreliableMeta = {
    states: {
      init: 'init',
      starting: 'starting',
      startFailed: 'start-failed',
      running: 'running',
      stopping: 'stopping',
      stopped: 'stopped'
    },
    stateConf: {
      startable: new Set([ 'init', 'start-failed', 'stopped' ]),
      stoppable: new Set([ 'running', 'stopping' ]),
      abortOnDeath: [ 'starting', 'running' ],
      abortOnStartFailure: [ 'running', 'stopped' ]
    },
    eventHandlers: {},
    deathEvents: [ 'exit' ]
  }

  public async start() {
    await super.start()
    stopOnExit(this)
  }

  protected async _createAndCheck() {
    const proc = spawn('foo', [ 'bar' ])
    try {
      await once(proc, 'spawn')
    } catch (err) {
      proc.kill()
      throw err
    }
    return proc
  }

  protected _stop() {
    this._uObj!.kill()
  }
}


async function foo() {
  const proc = new Proc
  await proc.start()
  // Do something with `proc`
  proc.stop()
  await proc.waitForState('stopped')
}

For example of unreliable daemon, see src/test.ts.