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 idString the is of the time to acknowledge as delivered successfully
callbackNodeback <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 idString A unique Id of the time
bodyObject Configuration options for the timer instance
Properties
Name Type Argument Default Description timeoutNumber the time in milliseconds from now the timer should execute. This must be in the range: 0 < timeout < 2^31 - 1.
dataString The data to be assicated with the timer, when it is executed
createdNumber <optional>
Date.now() timestamp when the timer is created. if not set, will default to now
callbackObject Options for the outbound transport for the timer when it executes
Properties
Name Type Description transportString The transport type ( http, etc )
transport.methodString The method the transport should use when executing the timer
transport.uriString The target uri for the transport when the timer executes
callbackNodeback - 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 idString the is of the time to acknowledge as delivered successfully
errorError The error object to send with event objects
callbackNodeback <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 callbackNodeback 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 idString the is of the time to acknowledge as delivered successfully
callbackNodeback <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 idString A unique Id of the time
bodyObject Configuration options for the timer instance
Properties
Name Type Description timeoutNumber Duration in milisecods to delay execution of the timer
dataString The data to be assicated with the timer, when it is executed
callbackObject Options for the outbound transport for the timer when it executes
Properties
Name Type Description transportString The transport type ( http, etc )
transport.methodString The method the transport should use when executing the timer
transport.uriString The target uri for the transport when the timer executes
callbackNodeback - 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 keyString The name of the nats queue to create
callbackNodeback Node style callback to execute when the function has finished execution
- Source:
Type Definitions
-
Nodeback()
-
Node style callback
- Source:
Properties:
Name Type Argument Description errError <optional>
<nullable>
An error instance. If not null, the results should not be trusted
resultObject The results of the function execution