Module: skyring/lib/timer

Manage Timers on a node


new (require("skyring/lib/timer"))( [options] [, onReady])

Parameters:
Name Type Argument Default Description
options Object <optional>
Properties
Name Type Argument Description
nats Object <optional>

Nats connection information

Properties
Name Type Argument Description
servers Array.<String> <optional>

A list of nats host:port to connect to

storage Object <optional>

Storage config options for level db

Properties
Name Type Argument Default Description
backend Array.<String> <optional>
memdown

a requireable module name, or absolute path to a leveldb compatible backend. memdown and leveldown are built in leveldown and memdown are installed by default

path String

A directory path to a leveldb instance. One will be created if it doesn't already exist. If the backend is memdown, this is optional and randomly generated per timer instance

onReady function <optional>
()=>{}

A callback function to call after initial recovery has completed

options.transports Array.<String> | Array.<function()> <optional>

an array of custom transport functions, or requireable paths that resolve to functions. All transport function must be named functions If not specified, configuration values will be used

Source:

Requires

Methods


cancelled(id [, callback])

Clears the respective timer from storage and publishes a cancelled event via nats

Parameters:
Name Type Argument Description
id String

the is of the time to acknowledge as delivered successfully

callback Nodeback <optional>

Callback to execute when the acknowledge is complete

Source:
Example
timers.cancel('2e2f6dad-9678-4caf-bc41-8e62ca07d551')

create(id, body, callback, callback)

Sets a new time instance. If The timer has lapsed, it will be executed immediately

Parameters:
Name Type Description
id String

A unique Id of the time

body Object

Configuration options for the timer instance

Properties
Name Type Argument Default Description
timeout Number

the time in milliseconds from now the timer should execute. This must be in the range: 0 < timeout < 2^31 - 1.

data String

The data to be assicated with the timer, when it is executed

created Number <optional>
Date.now()

timestamp when the timer is created. if not set, will default to now

callback Object

Options for the outbound transport for the timer when it executes

Properties
Name Type Description
transport String

The transport type ( http, etc )

transport.method String

The method the transport should use when executing the timer

transport.uri String

The target uri for the transport when the timer executes

callback Nodeback
Source:
Example
const crypto = require('crypto')
id = crypto.createHash('sha1')
           .update(crypto.randomBytes(10))
           .digest('hex')

const options = {
  timeout: 4000
, data: "this is a payload"
, callback: {
    transport: 'http'
  , method: 'put'
  , uri: 'http://api.domain.com/callback'
  }
}

timers.create(id, options, (err) => {
  if (err) throw err
})

failure(id, error [, callback])

Clears the respective timer from storage and publishes a failure event via nats

Parameters:
Name Type Argument Description
id String

the is of the time to acknowledge as delivered successfully

error Error

The error object to send with event objects

callback Nodeback <optional>

Callback to execute when the acknowledge is complete

Source:
Example
const error = Error('Remote server unavailable')
error.code = 'ENOREMOTE'
timers.failure('2e2f6dad-9678-4caf-bc41-8e62ca07d551', error)

shutdown(callback)

Triggers timers to be purged from this node canceling all locally pending timers, and distributing them in the ring. It is assumed this node is no longer a ring member

Parameters:
Name Type Description
callback Nodeback

Node style callback to execute when the function is complete

Source:

success(id [, callback])

Clears the respective timer from storage and publishes a success event via nats

Parameters:
Name Type Argument Description
id String

the is of the time to acknowledge as delivered successfully

callback Nodeback <optional>

Callback to execute when the acknowledge is complete

Source:
Example
timers.success('2e2f6dad-9678-4caf-bc41-8e62ca07d551')

update(id, body, callback, callback)

Updates a timer inplace

Parameters:
Name Type Description
id String

A unique Id of the time

body Object

Configuration options for the timer instance

Properties
Name Type Description
timeout Number

Duration in milisecods to delay execution of the timer

data String

The data to be assicated with the timer, when it is executed

callback Object

Options for the outbound transport for the timer when it executes

Properties
Name Type Description
transport String

The transport type ( http, etc )

transport.method String

The method the transport should use when executing the timer

transport.uri String

The target uri for the transport when the timer executes

callback Nodeback
Source:
Example
timers.update('0dc5a555-d0f6-49a0-b336-5befb0437288', {
  timeout: 4000
, data: "this is a payload"
, callback: {
    transport: 'http'
  , method: 'put'
  , uri: 'http://api.domain.com/callback'
  }
})

watch(key, callback)

Starts an internal nats queue

Parameters:
Name Type Description
key String

The name of the nats queue to create

callback Nodeback

Node style callback to execute when the function has finished execution

Source:

Type Definitions


Nodeback()

Node style callback

Properties:
Name Type Argument Description
err Error <optional>
<nullable>

An error instance. If not null, the results should not be trusted

result Object

The results of the function execution

Source: