cloudcms-server 0.9.261 → 0.9.264

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,28 +14,33 @@ class AbstractAsyncProvider extends AbstractProvider
14
14
  {
15
15
  super(config);
16
16
  }
17
-
17
+
18
+ _lock(lockKey, workFunction)
19
+ {
20
+ process.locks.lock(lockKey, workFunction);
21
+ }
22
+
18
23
  readOrCreateChannel(channelId, callback) {
19
24
 
20
25
  var self = this;
21
26
 
22
27
  self.readChannel(channelId, function(err, channel) {
23
-
28
+
24
29
  if (err) {
25
30
  return callback(err);
26
31
  }
27
-
32
+
28
33
  if (channel) {
29
34
  return callback(null, channel);
30
35
  }
31
36
 
32
37
  var channel = {};
33
38
  self.writeChannel(channelId, channel, function(err) {
34
-
39
+
35
40
  if (err) {
36
41
  return callback(err);
37
42
  }
38
-
43
+
39
44
  callback(null, channel);
40
45
  });
41
46
  });
@@ -46,26 +51,52 @@ class AbstractAsyncProvider extends AbstractProvider
46
51
  register(channelId, user, callback)
47
52
  {
48
53
  var self = this;
49
-
54
+
55
+ //console.log("a1");
56
+ self._lock("channels", function(err, releaseLockFn) {
57
+ // console.log("a2: ", err);
58
+ // console.log("a3: ", releaseLockFn);
59
+ self.doRegister(channelId, user, function(err) {
60
+ // console.log("a4 ", err);
61
+ callback(err);
62
+ // console.log("a5: ", releaseLockFn);
63
+ return releaseLockFn();
64
+ });
65
+ });
66
+ }
67
+
68
+ /**
69
+ * Workhorse method. The "channels" lock is taken out ahead of this being called.
70
+ *
71
+ * @param channelId
72
+ * @param user
73
+ * @param callback
74
+ * @private
75
+ */
76
+ doRegister(channelId, user, callback)
77
+ {
78
+ var self = this;
79
+
50
80
  self.readOrCreateChannel(channelId, function (err, channel) {
51
-
81
+
52
82
  if (err) {
53
83
  return callback(err);
54
84
  }
55
-
85
+
56
86
  if (!channel.users) {
57
87
  channel.users = {};
58
88
  }
59
-
89
+
60
90
  channel.users[user.id] = {
61
91
  "user": user,
62
92
  "time": new Date().getTime()
63
93
  };
64
-
94
+
65
95
  self.writeChannel(channelId, channel, function (err) {
66
96
  callback(err);
67
97
  });
68
98
  });
99
+
69
100
  }
70
101
 
71
102
  discover(channelId, callback)
@@ -101,123 +132,136 @@ class AbstractAsyncProvider extends AbstractProvider
101
132
  });
102
133
  }
103
134
 
104
- expire(beforeMs, callback)
135
+ checkRegistered(channelId, userId, callback)
105
136
  {
106
137
  var self = this;
107
138
 
108
- self.listChannelIds(function(err, channelIds) {
139
+ self.readChannel(channelId, function(err, channel) {
109
140
 
110
141
  if (err) {
111
142
  return callback(err);
112
143
  }
113
144
 
145
+ if (!channel) {
146
+ return callback(null, false);
147
+ }
148
+
149
+ if (!channel.users) {
150
+ return callback(null, false);
151
+ }
152
+
153
+ if (channel.users[userId]) {
154
+ return callback(null, true);
155
+ }
156
+
157
+ callback(null, false);
158
+
159
+ });
160
+ }
161
+
162
+ expire(beforeMs, callback)
163
+ {
164
+ var self = this;
165
+
166
+ self._lock("channels", function(err, releaseLockFn) {
167
+ self._expire(beforeMs, function(err) {
168
+ callback(err);
169
+ return releaseLockFn();
170
+ });
171
+ });
172
+ }
173
+
174
+ /**
175
+ * @override
176
+ */
177
+ _expire(beforeMs, callback)
178
+ {
179
+ var self = this;
180
+
181
+ self.listChannelIds(function(err, channelIds) {
182
+
183
+ if (err) {
184
+ return callback(err);
185
+ }
186
+
114
187
  if (!channelIds || channelIds.length === 0) {
115
188
  return callback(null, [], {});
116
189
  }
117
-
190
+
118
191
  // a list of channel IDs whose memberships were updated
119
192
  var updatedMembershipChannelIds = [];
120
193
  var expiredUserIdsByChannelId = {};
121
-
194
+
122
195
  var fns = [];
123
-
196
+
124
197
  for (var i = 0; i < channelIds.length; i++)
125
198
  {
126
199
  var channelId = channelIds[i];
127
-
200
+
128
201
  var fn = function (channelId, updatedMembershipChannelIds, expiredUserIdsByChannelId, beforeMs) {
129
202
  return function (done) {
130
-
203
+
131
204
  self.readChannel(channelId, function(err, channel) {
132
-
205
+
133
206
  if (err) {
134
207
  return done(err);
135
208
  }
136
-
209
+
137
210
  if (!channel) {
138
211
  return done();
139
212
  }
213
+
214
+ if (!channel.users || Object.keys(channel.users).length === 0)
215
+ {
216
+ return done();
217
+ }
140
218
 
141
- var updatedMembership = false;
142
-
143
- if (channel.users)
219
+ // populate all of the user IDs that need to be removed
220
+ var userIdsToRemove = [];
221
+ for (var userId in channel.users)
144
222
  {
145
- // populate all of the user IDs that need to be removed
146
- var userIdsToRemove = [];
147
- for (var userId in channel.users)
223
+ var entry = channel.users[userId];
224
+ if (entry.time < beforeMs)
148
225
  {
149
- var entry = channel.users[userId];
150
- if (entry.time < beforeMs)
151
- {
152
- updatedMembership = true;
153
- userIdsToRemove.push(userId);
154
-
155
- var expiredUserIds = expiredUserIdsByChannelId[channelId]
156
- if (!expiredUserIds) {
157
- expiredUserIds = expiredUserIdsByChannelId[channelId] = [];
158
- }
159
-
160
- expiredUserIds.push(userId);
226
+ updatedMembershipChannelIds.push(channelId);
227
+ userIdsToRemove.push(userId);
228
+
229
+ var expiredUserIds = expiredUserIdsByChannelId[channelId]
230
+ if (!expiredUserIds) {
231
+ expiredUserIds = expiredUserIdsByChannelId[channelId] = [];
161
232
  }
162
- }
163
-
164
- // remove the user IDs
165
- for (var i = 0; i < userIdsToRemove.length; i++)
166
- {
167
- delete channel.users[userIdsToRemove[i]];
233
+
234
+ expiredUserIds.push(userId);
168
235
  }
169
236
  }
170
-
171
- if (updatedMembership)
237
+
238
+ // remove the user IDs
239
+ for (var i = 0; i < userIdsToRemove.length; i++)
172
240
  {
173
- updatedMembershipChannelIds.push(channelId);
241
+ delete channel.users[userIdsToRemove[i]];
174
242
  }
175
-
176
- done();
243
+
244
+ self.writeChannel(channelId, channel, function() {
245
+ done();
246
+ });
247
+
177
248
  });
178
249
  };
179
250
  }(channelId, updatedMembershipChannelIds, expiredUserIdsByChannelId, beforeMs);
180
251
  fns.push(fn);
181
252
  }
182
-
253
+
183
254
  async.parallel(fns, function(err) {
184
-
255
+
185
256
  if (err) {
186
257
  return callback(err);
187
258
  }
188
-
259
+
189
260
  callback(null, updatedMembershipChannelIds, expiredUserIdsByChannelId);
190
261
  });
191
262
  });
192
- }
193
-
194
- checkRegistered(channelId, userId, callback)
195
- {
196
- var self = this;
197
-
198
- self.readChannel(channelId, function(err, channel) {
199
-
200
- if (err) {
201
- return callback(err);
202
- }
203
-
204
- if (!channel) {
205
- return callback(null, false);
206
- }
207
-
208
- if (!channel.users) {
209
- return callback(null, false);
210
- }
211
-
212
- if (channel.users[userId]) {
213
- return callback(null, true);
214
- }
215
-
216
- callback(null, false);
217
-
218
- });
219
- }
220
-
263
+ };
264
+
221
265
  acquireLock(channelId, user, callback)
222
266
  {
223
267
  var self = this;
@@ -303,14 +347,16 @@ class AbstractAsyncProvider extends AbstractProvider
303
347
  callback(null, lock);
304
348
  });
305
349
  }
306
-
307
-
350
+
351
+
352
+
353
+
308
354
  //////////////////////////////////////////////////////////////////////////////////////////////////////////
309
355
  //
310
356
  // ABSTRACT METHODS
311
357
  //
312
358
  //////////////////////////////////////////////////////////////////////////////////////////////////////////
313
-
359
+
314
360
  // ABSTRACT
315
361
  readChannel(channelId, callback)
316
362
  {
@@ -328,7 +374,7 @@ class AbstractAsyncProvider extends AbstractProvider
328
374
  {
329
375
  throw new Error("listChannelIds() method is not implemented");
330
376
  }
331
-
377
+
332
378
  // ABSTRACT
333
379
  readLock(lockId, callback)
334
380
  {
@@ -2,7 +2,7 @@
2
2
  * Abstract class for an Awareness Provider.
3
3
  *
4
4
  * This class provides an interface or base functions that any implementation class must implement in order to
5
- * work with the Awareness Service. Any methods that
5
+ * work with the Awareness Service.
6
6
  *
7
7
  */
8
8
  class AbstractProvider
@@ -24,20 +24,6 @@ class MemoryProvider extends AbstractAsyncProvider
24
24
  callback();
25
25
  }
26
26
 
27
- readOrCreateChannel(channelId, callback)
28
- {
29
- var self = this;
30
-
31
- var channel = self.channelMap[channelId];
32
- if (channel) {
33
- return callback(null, channel);
34
- }
35
-
36
- channel = self.channelMap[channelId] = {};
37
-
38
- callback(null, channel);
39
- }
40
-
41
27
  readChannel(channelId, callback)
42
28
  {
43
29
  var self = this;