-
Notifications
You must be signed in to change notification settings - Fork 1
/
azurequeue.js
executable file
·173 lines (155 loc) · 5.54 KB
/
azurequeue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Created by Aureliano on 21/03/2016.
*/
'use strict';
const DEFUALT_VISIBILITY_TIMEOUT = 2;
const azureQueue = require('azure-queue-node');
const underscore = require('underscore');
const async = require('async');
const assert = require('assert');
const util = require('util');
/**
* wrapper for the azure queue for nodejs fatus job processor
*/
class AzureQueue{
/**
* default constructor
*/
constructor (){
this.queueClient = azureQueue.getDefaultClient();
}
/**
* insert new objects in queue
* @param queueName
* @param objects
* @param onComplete
*/
insertInQueue(queueName,objects,onComplete){
assert.equal(typeof queueName,'string','queueName must be an string');
assert.equal(typeof onComplete,'function','onComplete must be a function');
this.queueClient.putMessage(queueName,objects,{},onComplete);
}
/**
* create a new queue by name
* @param queueName
* @param onCreate
*/
createQueue(queueName,onCreate){
assert.equal(typeof queueName,'string','queueName must be an string');
assert.equal(typeof onCreate,'function','onCreate must be a function');
this.queueClient.createQueue(queueName,onCreate);
}
/**
* get the top message from a queue (reserving it)
* @param queueName
* @param option
* @param onGet
*/
getMessage(queueName,option,onGet){
assert.equal(typeof queueName,'string','queueName must be an string');
assert.equal(typeof onGet,'function','onGet must be a function');
var timeOutOption = {visibilityTimeout : DEFUALT_VISIBILITY_TIMEOUT}
option = underscore.extend(timeOutOption,option);
this.queueClient.getMessages(
queueName,
option,
onGet
);
}
/**
* clear all elements in queue
* @param queueName
* @param onOk
*/
clearQueue(queueName,onOk){
assert.equal(typeof queueName,'string','queueName must be an string');
assert.equal(typeof onOk,'function','onOk must be a function');
this.queueClient.clearMessages(queueName,{},onOk);
}
/**
* counts all elements in queue
* @param queueName
* @param onCount
*/
countQueue(queueName,onCount){
assert.equal(typeof queueName,'string','queueName must be an string');
assert.equal(typeof onCount,'function','onCount must be a function');
this.queueClient.countMessages(queueName,{},onCount);
}
/**
* delete the queue object
* @param queueName
* @param onDone
*/
deleteQueue(queueName,onDone){
assert.equal(typeof queueName,'string','queueName must be an string');
assert.equal(typeof onDone,'function','onDone must be a function');
this.queueClient.deleteQueue(queueName,onDone);
}
/**
* delete a single message
* @param queueName
* @param messageId
* @param popReceipt
* @param options
* @param onDelete
*/
deleteMessage(queueName,messageId,popReceipt,options,onDelete){
assert.equal(typeof queueName,'string','queueName must be an string');
assert.equal(typeof messageId,'string','messageId must be a string');
assert.equal(typeof popReceipt,'string','popReceipt must be a function');
assert.equal(typeof onDelete,'function','onDelete must be a function');
this.queueClient.deleteMessage(queueName,messageId,popReceipt,options,onDelete);
}
/**
* update a message in queue
* @param queueName
* @param messageId
* @param popReceipt
* @param visibilityTimeout
* @param msg
* @param options
* @param onUpdate
*/
updateMessage(queueName,messageId,popReceipt,visibilityTimeout,msg,options,onUpdate){
assert.equal(typeof queueName,'string','queueName must be an string');
assert.equal(typeof messageId,'string','messageId must be a string');
assert.equal(typeof popReceipt,'string','popReceipt must be a function');
assert.equal(typeof msg,'object','msg must be a function');
assert.equal(typeof onUpdate,'function','onUpdate must be a function');
//console.log('========== %s ======= %s ====== %s ========',messageId,popReceipt,util.inspect(msg));
this.queueClient.updateMessage(queueName,messageId,popReceipt,visibilityTimeout||DEFUALT_VISIBILITY_TIMEOUT,msg,options,onUpdate);
}
/**
* peek the top of the queue
* @param queueName
* @param options
* @param onGet
*/
peekMsg(queueName,options,onGet){
assert.equal(typeof queueName,'string','queueName must be an string');
this.queueClient.peekMessages(queueName,options,onGet);
}
/**
* peek all the queue
* @param queueName
* @param onGet
*/
peekQueue(queueName,onGet){
assert.equal(typeof queueName,'string','queueName must be an string');
assert.equal(typeof onGet,'function','onGet must be a function');
var thisObj = this;
async.waterfall([
function _getCount(wfcallback){
thisObj.countQueue(queueName,wfcallback);
},
function _putMessage(res,wfcallback){
thisObj.peekMsg(queueName,{maxMessages:res},wfcallback);
},
],
onGet
)
}
}
/** Exports */
module.exports = AzureQueue;