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 | function storeIncomingMessage(message) {
|
32 |
|
33 | var time = process.hrtime();
|
34 | var ix = time[0] * 1e9 + time[1];
|
35 | ix = new Date().getTime() + '-' + ix;
|
36 | var glo = {
|
37 | global: this.resilientMode.documentName,
|
38 | subscripts: ['message', ix, 'content'],
|
39 | data: JSON.stringify(message)
|
40 | };
|
41 | this.db.set(glo, function(error, result) {});
|
42 |
|
43 | glo.subscripts = ['message', ix, 'token'];
|
44 | glo.data = message.token;
|
45 | this.db.set(glo, function(error, result) {});
|
46 |
|
47 | glo.subscripts = ['pending', message.token, ix];
|
48 | glo.data = '';
|
49 | this.db.set(glo, function(error, result) {});
|
50 |
|
51 | return ix;
|
52 | }
|
53 |
|
54 | function nextValue(glo, callback) {
|
55 |
|
56 | this.db.order(glo, function(error, result) {
|
57 | console.log('nextValue - result = ' + JSON.stringify(result));
|
58 | if (!error && callback) callback(result);
|
59 | });
|
60 | }
|
61 |
|
62 | function requeueMessages(token, ix, handleMessage) {
|
63 | var ewdQueueDocument = this.resilientMode.documentName;
|
64 |
|
65 |
|
66 |
|
67 | var pendingGlo = {
|
68 | global: ewdQueueDocument,
|
69 | subscripts: ['pending', token, ""]
|
70 | };
|
71 | var q = this;
|
72 |
|
73 |
|
74 |
|
75 | var callback = function(node) {
|
76 |
|
77 | var dbIndex = node.result;
|
78 | if (dbIndex !== '') {
|
79 |
|
80 | if (dbIndex !== ix) {
|
81 | var glo = {
|
82 | global: ewdQueueDocument,
|
83 | subscripts: ['message', dbIndex, 'content']
|
84 | };
|
85 | q.db.get(glo, function(error, result) {
|
86 | if (result.defined) {
|
87 | var message = JSON.parse(result.data);
|
88 | if (message.type !== 'ewd-register' && message.type !== 'ewd-reregister') {
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | glo.subscripts = ['message', dbIndex, 'workerStatus'];
|
95 | q.db.get(glo, function(error, result) {
|
96 | if (result.defined && result.data === 'started') {
|
97 | message.resubmitted = true;
|
98 | }
|
99 |
|
100 | handleMessage(message);
|
101 |
|
102 | nextValue.call(q, node, callback);
|
103 | });
|
104 | }
|
105 | }
|
106 | });
|
107 |
|
108 | q.db.kill(node, function(error, result) {});
|
109 | }
|
110 | else {
|
111 | nextValue.call(q, node, callback);
|
112 | }
|
113 | }
|
114 |
|
115 | };
|
116 |
|
117 | console.log('Checking to see if any messages need re-queueing');
|
118 | nextValue.call(this, pendingGlo, callback);
|
119 | }
|
120 |
|
121 | function storeResponse(resultObj, token, ix, count, handleMessage) {
|
122 | if (resultObj.type === 'ewd-reregister') {
|
123 |
|
124 | requeueMessages.call(this, token, ix, handleMessage);
|
125 | }
|
126 |
|
127 | saveResponse.call(this, ix, count, JSON.stringify(resultObj));
|
128 | if (resultObj.finished) {
|
129 |
|
130 | removePendingIndex.call(this, token, ix);
|
131 | }
|
132 | }
|
133 |
|
134 | function saveResponse(ix, count, data) {
|
135 |
|
136 | var glo = {
|
137 | global: this.resilientMode.documentName,
|
138 | subscripts: ['message', ix, 'response', count],
|
139 | data: data
|
140 | };
|
141 | this.db.set(glo, function(error, result) {});
|
142 | }
|
143 |
|
144 | function removePendingIndex(token, ix) {
|
145 | var glo = {
|
146 | global: this.resilientMode.documentName,
|
147 | subscripts: ['pending', token, ix]
|
148 | };
|
149 | var q = this;
|
150 | setTimeout(function() {
|
151 |
|
152 | q.db.kill(glo, function(error, result) {});
|
153 | }, 1000);
|
154 | }
|
155 |
|
156 | function storeWorkerStatusUpdate(messageObj, status) {
|
157 | var queueStore = new this.documentStore.DocumentNode(this.userDefined.config.resilientMode.documentName, ["message", messageObj.dbIndex, "workerStatus"]);
|
158 | queueStore.value = status;
|
159 | }
|
160 |
|
161 | function cleardownQueueBackup() {
|
162 | var queue = new this.documentStore.DocumentNode(this.userDefined.config.resilientMode.documentName);
|
163 | var messages = queue.$('message');
|
164 | var pending = queue.$('pending');
|
165 |
|
166 | var now = new Date().getTime();
|
167 | var diff = this.userDefined.config.resilientMode.keepPeriod;
|
168 | diff = diff * 1000;
|
169 | var cutOff = now - diff;
|
170 |
|
171 | console.log(process.pid + ': Clearing down queue backup document, up to ' + (diff /1000) + ' seconds ago');
|
172 |
|
173 | messages.forEachChild(function(dbIndex, messageObj) {
|
174 | var timestamp = parseInt(dbIndex.split('-')[0]);
|
175 | if (timestamp < cutOff) {
|
176 | var token = messageObj.$('token').value;
|
177 | if (!pending.$(token).$(timestamp).exists) {
|
178 | messageObj.delete();
|
179 | console.log(process.pid + ': ' + dbIndex + ' deleted');
|
180 | }
|
181 | }
|
182 | else {
|
183 | return true;
|
184 | }
|
185 | });
|
186 |
|
187 | console.log(process.pid + ': Queue backup document cleared down');
|
188 | }
|
189 |
|
190 | function garbageCollector(delay) {
|
191 |
|
192 | var q = this;
|
193 | delay = delay*1000 || 300000;
|
194 | var garbageCollector;
|
195 |
|
196 | this.on('stop', function() {
|
197 | clearInterval(garbageCollector);
|
198 | console.log('Queue Backup Garbage Collector has stopped');
|
199 | });
|
200 |
|
201 | garbageCollector = setInterval(function() {
|
202 | cleardownQueueBackup.call(q);
|
203 | }, delay);
|
204 |
|
205 | console.log('Queue Backup Garbage Collector has started in worker ' + process.pid);
|
206 | }
|
207 |
|
208 | module.exports = {
|
209 | storeResponse: storeResponse,
|
210 | storeIncomingMessage: storeIncomingMessage,
|
211 | storeWorkerStatusUpdate: storeWorkerStatusUpdate,
|
212 | garbageCollector: garbageCollector
|
213 | }; |
\ | No newline at end of file |