/gate-executor

Execute functions that return via callback in order, but pause if a function is marked as a gate.

Primary LanguageHTMLMIT LicenseMIT

gate-executor

npm version Build Status Coverage Status Dependency Status Gitter chat

Execute functions that return via callback in order, but pause if a function is marked as a gate.

Annotated Source

A work execution queue that provides tracing and gating. Work functions can have optional callbacks. Timeouts are triggered when execution does not complete within a specified time.

Gating places execution into a serial mode, where all gated work functions must complete in order before other work functions in the queue are called. The gate can be ignored.

Used by Seneca micro-service communication to execute tasks in order. If you haven't heard about Seneca, check out the getting started guide.

Support

If you're using this module, feel free to contact me on twitter if you have any questions! :) @rjrodger

Usage

The gate executor provides functionality to pause gated tasks and to quit tasks that exceed a given timeout. The executor can be created with a few options:

var e0 = executor({
  trace: true,              // Error logging trace, default false
  timeout: 150,             // Timout for tasks, default 33333
  error: function() {...},  // A function to wrap all errors in, default noop
  stubs: {		    // Stubs to substitute default node functions
    now: {...},
    setTimeout: {...},
    clearTimeout: {...}
  }
})

When calling the executor with a task, use the following pattern:

e0.execute({
  id: 'a',              // Optional identifier for trace
  fn: function() {...}  // Function to be preformed
  cb: function(err, out) {...}
})

Worker definition

The worker definition object has the following properties:

  • id: an identifier string for the worker.
  • desc: a description string for the worker.
  • fn: the worker function itself; it should accept one argument, a completion callback, which must be called (this in turn then calls the task callback, if any).
  • cb: optional callback function, of the form: function(err,result) { ... }.
  • gate: this worker is a gate; all subsequent workers will wait for this one to complete.
  • ungate: this worker will ignore any gates that are active, and so will be executed regardless.
  • timeout: override the executor timeout for this individual work

Testing

npm test