Manage Timers on a node
new (require("skyring/lib/timer"))( [options] [, onReady])
Parameters:
Name | Type | Argument | Default | Description | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
<optional> |
Properties
|
||||||||||||||||||||||||||||||||||||
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
- module:os
- module:crypto
- module:path
- module:levelup
- module:encoding-down
- module:debug
- module:skyring/lib/transports
- module:skyring/lib/nats
- module:skyring/lib/json
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
- Source:
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