You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -19,6 +19,11 @@ In a clustered server environment, you'd occasionally need to process a task aft
19
19
20
20
## Requirements
21
21
* Requires Redis version 2.6.0 or later (dtimer uses lua)
22
+
* The redis module NUST be promisified at module level in advance, even legacy callback style is used.
23
+
```js
24
+
varPromise=require('bluebird');
25
+
var redis =Promise.promisifyAll(require('redis'));
26
+
```
22
27
23
28
## API
24
29
@@ -30,81 +35,197 @@ In a clustered server environment, you'd occasionally need to process a task aft
30
35
* {object} option Options.
31
36
* {string} ns Namespace to define a unique event notification domain. Defaults to 'dt'.
32
37
* {number} maxEvents The maximum number of events this instance wants to received at a time. Defaults to 8. You may change this value later with the setter, `maxEvents`
38
+
* {number} confTimeout Confirmation wait timeout in seconds. Defaults to 10 [sec].
39
+
40
+
> The redis module MUST be promsified before instantiating clients for `pub` and `sub`. See the example below.
33
41
34
42
### Instance method
35
-
* join(cb) - Start listening to events.
36
-
* leave(cb)) - Stop listening to events.
37
-
* post(ev, delay, cb) - Post an event.
43
+
#### join() - Start listening to events.
44
+
```
45
+
join([cb]) => {Promise}
46
+
* cb {function} Optional callback.
47
+
* returns Promise if cb is not supplied.
48
+
```
49
+
50
+
#### leave() - Stop listening to events.
51
+
```
52
+
leave([cb]) => {Promise}
53
+
* cb {function} Optional callback.
54
+
* returns Promise if cb is not supplied.
55
+
```
56
+
57
+
#### post() - Post an event.
58
+
```
59
+
post(ev, delay [, cb]) => {Promise}
38
60
* {object} ev - Event data.
61
+
* {string} [ev.id] - User provided event ID. If not present, dtimer will automatically
62
+
assign an ID using uuid.
63
+
* {number} [ev.maxRetries] - Maximum number of retries that occur if confirm() is not
64
+
made within confTimeout [sec]. If not present, it defaults to 0 (no retry).
39
65
* {number} delay - Delay value in milliseconds.
40
-
* {function} cb - Callback made when the post operation is complete. The callback function takes following args:
41
-
* {Error} err - Error object. Null is set on sucess.
42
-
* {number} evId - Event ID assigned to the posted event. The ID is used to cancel the event.
43
-
* cancel(evId, cb) - Cancel an event by its event ID.
44
-
* {number} evId - The event ID obtained via the callback of post() method.
45
-
* upcoming([option], cb) - Retrieve upcoming events. This method is provided for diagnostic purpose only and the use of this method in production is highly discouraged unless the number of events retrieved is reasonably small. Cost of this operation is O(N), where N is the number events that would be retrieved.
66
+
* {function} [cb] - Callback made when the post operation is complete.
67
+
* returns Promise if cb is not supplied.
68
+
* Resolved value: evId {string} - Event ID assigned to the posted event. If ev object
69
+
already had id property, this evId is identical to ev.id always.
70
+
```
71
+
72
+
> The `ev` object may have user-defined properties as its own properties, however, the following properties are reserved and used by dtimer; 'id', 'maxRetries' and '_numRetries'. If your appkication needs to use these names (for application specific use), then consider putting all user-defined event object inside the `ev` like this:
73
+
74
+
```js
75
+
{
76
+
id:'25723fdd-4434-4cbd-b579-4693e221ec73',
77
+
maxRetries:3,
78
+
// _numRetries: 0 // added by dtimer before the event was fired
79
+
data: { /*...*/ } // user-defined event object
80
+
}
81
+
```
82
+
83
+
84
+
#### peek() - Peek an event scheduled.
85
+
```
86
+
peek(evId [, cb]) => {Promise}
87
+
* {string} evId - The event ID to be canceled.
88
+
* {function} [cb] - Callback made when the operation is complete.
89
+
* returns Promise if cb is not supplied.
90
+
* Resolved value: results {array} An array of results.
91
+
* results[0] {number} Time to expire in milliseconds, or null if the event does
92
+
not exit.
93
+
* results[1] {object} Event object, or null if the event does not exit.
94
+
```
95
+
96
+
#### cancel() - Cancel an event by its event ID.
97
+
```
98
+
cancel(evId [, cb]) => {Promise}
99
+
* {string} evId - The event ID to be canceled.
100
+
* {function} [cb] - Callback made when the operation is complete.
101
+
* returns Promise if cb is not supplied.
102
+
* Resolved value {number} 0: the event ID not found. 1: the event has been canceled.
103
+
```
104
+
105
+
#### confirm() - Confirm that specified event has been processed.
106
+
```
107
+
confirm(evId [, cb])
108
+
* {string} evId - The event ID to be canceled.
109
+
* {function} [cb] - Callback made when the operation is complete.
110
+
* returns Promise if cb is not supplied.
111
+
* Resolved value {number} 0: the event ID not found. 1: the event has been confirmed.
112
+
```
113
+
114
+
#### changeDelay() - Change delay of specified event.
115
+
```
116
+
changeDelay(evId, delay, [, cb]) => {Promise}
117
+
* {string} evId - The event ID for which the delay will be changed.
118
+
* {number} delay - New delay (in milliseconds relative to the current time).
119
+
* {function} [cb] - Callback made when the operation is complete.
120
+
* returns Promise if cb is not supplied.
121
+
* Resolved value {number} 0: the event ID not found. 1: the delay has been updated.
122
+
```
123
+
124
+
#### upcoming() - Retrieve upcoming events.
125
+
This method is provided for diagnostic purpose only and the use of this method in production is highly discouraged unless the number of events retrieved is reasonably small. Cost of this operation is O(N), where N is the number events that would be retrieved.
126
+
```
127
+
upcoming([option] [, cb]) => {Promise}
46
128
* {object} option - Options
47
-
* {number} offset Offset expiration time in msec from which events are retrieved . This defaults to the current (redis-server) time (-1).
48
-
* {number} duration Time length [msec] from offset time for which events are trieved. This defaults to '+inf' (-1).
49
-
* {number} limit Maximum number of events to be retrieved. This defaults to `no limit` (-1)
50
-
* {function} cb - Callback made when upcoming operation is complete. The callback function takes following args:
129
+
* {number} offset Offset expiration time in msec from which events are retrieved.
130
+
This defaults to the current (redis-server) time (-1).
131
+
* {number} duration Time length [msec] from offset time for which events are
132
+
retrieved. This defaults to '+inf' (-1).
133
+
* {number} limit Maximum number of events to be retrieved. This defaults to
134
+
`no limit` (-1).
135
+
* {function} [cb] - Callback made when upcoming operation is complete.
136
+
The callback function takes following args:
51
137
* {Error} err - Error object. Null is set on success.
52
138
* {object} events - List of objects that met the given criteria.
53
-
Example of retrieved events by upcoming():
139
+
* returns Promise if cb is not supplied.
140
+
```
141
+
142
+
##### Example of retrieved events by upcoming():
54
143
55
144
```
56
145
{
57
-
"2": {
146
+
"25723fdd-4434-4cbd-b579-4693e221ec73": {
58
147
"expireAt": 1410502530320,
59
148
"event": {
60
149
"msg": "hello"
61
150
}
62
151
},
63
-
"3": {
152
+
"24bbef35-8014-4107-803c-5ff4b858a5ad": {
64
153
"expireAt": 1410502531321,
65
154
"event": {
66
155
"msg": "hello"
67
156
}
68
157
}
69
158
}
70
-
```
159
+
```
71
160
72
161
73
162
### Instance member (getter/setter)
74
163
* {number} maxEvents (getter&setter) - The max number of events this node can grab at a time. The attempt to set it to 0 or negative value result in setting it to the original value supplied in the option field, or the default (8).
75
164
76
165
77
166
### Event types
78
-
* Event: 'event' - Emitted when an event is received.
79
-
* Event: 'error' - Emitted when an error occurred.
167
+
168
+
#### Event: 'event' - Emitted when an event is received.
169
+
The handler will be called with the following argument:
170
+
171
+
```
172
+
* ev {object} Event object.
173
+
* ev.id {string} Event ID.
174
+
* ev.maxRetries {number} Max retries specified when this event was posted.
175
+
* ev._numRetries {number} Number of retries made before this event occured.
176
+
```
177
+
178
+
#### Event: 'error' - Emitted when an error occurred.
179
+
The handler will be called with the following argument:
180
+
181
+
```
182
+
* err {Error} Error object.
183
+
```
80
184
81
185
## Example
82
186
83
187
```js
84
188
var DTimer =require('dtimer').DTimer;
85
-
var redis =require('redis');
189
+
varPromise=require('bluebird');
190
+
var redis =Promise.promisifyAll(require('redis')); // module level promisification
86
191
var pub =redis.createClient();
87
192
var sub =redis.createClient();
88
193
var dt =newDTimer('ch1', pub, sub)
89
194
dt.on('event', function (ev) {
90
-
// do something with ev})
195
+
// do something with ev
196
+
197
+
// If the posted event has `maxRetries` property set to a number greater than 0,
198
+
// you must call confirm() with its event ID. If not confirmed, the event will
199
+
// fire in `confTimeout` (default 10 sec) again, until the number of retries
200
+
// becomes `maxRetries`.
201
+
dt.confirm(ev.id, function (err) {
202
+
// confirmed.
203
+
});
204
+
})
91
205
dt.on('error', function (err) {
92
-
// handle error})
206
+
// handle error
207
+
})
93
208
dt.join(function (err) {
94
209
if (err) {
95
210
// join failed
96
-
return; }
97
-
// join successfully})
98
-
dt.post({msg:'hello'}, 200, function (err, evId) {
211
+
return;
212
+
}
213
+
// join successfully
214
+
})
215
+
dt.post({id:'myId', maxRetries:3, msg:'hello'}, 200, function (err) {
99
216
if (err) {
100
217
// failed to post event
101
-
return; }
102
-
// posted event successfully
218
+
return;
219
+
}
220
+
// posted the event successfully
221
+
103
222
// If you need to cancel this event, then do:
104
-
//dt.cancel(evId, function (err) {...});})
223
+
//dt.cancel('myId', function (err) {...});
224
+
})
105
225
```
226
+
106
227
## Tips
107
228
108
229
* You do not have to join to post. By calling `join()`, you are declaring yourself as a listener to consume due events.
109
230
* Under the hood, more than one event may be retrieved. You may change the maximum number of events (using the setter, DTimer#maxEvents) to be retrieved at once to optimize memory usage and/or overall performance.
110
-
* To control incoming event traffic, use DTimer#leave() and then DTimer#join() again. Note that you may still receive some events after calling leave() if there are remaing events inside DTimer that have already been received from Redis.
231
+
* To control incoming event traffic, use DTimer#leave() and then DTimer#join() again. Note that you may still receive some events after calling leave() if there are remaining events inside DTimer that have already been received from Redis.
0 commit comments