cloudcms-server 3.2.304 → 3.2.306

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.
@@ -1231,7 +1231,7 @@ module.exports = function(app, dust)
1231
1231
  "config": {}
1232
1232
  };
1233
1233
 
1234
- var divId = formId || "form" + new Date().getTime();
1234
+ var divId = formId || "form" + Date.now();
1235
1235
 
1236
1236
  chunk.write("<div id='" + divId + "'></div>");
1237
1237
  chunk.write("<script src='/_lib/formhelper/formhelper.js'></script>");
package/duster/index.js CHANGED
@@ -218,9 +218,9 @@ exports.execute = function(req, store, filePath, model, callback)
218
218
  var executeTemplate = function(template, templatePath, context, callback)
219
219
  {
220
220
  // execute template
221
- var t1 = new Date().getTime();
221
+ var t1 = Date.now();
222
222
  dust.render(template, context, function(err, out) {
223
- var t2 = new Date().getTime();
223
+ var t2 = Date.now();
224
224
 
225
225
  if (err)
226
226
  {
@@ -160,7 +160,7 @@ var handleInvalidate = function(req, res)
160
160
  trigger("invalidate");
161
161
 
162
162
  // new timestamp
163
- process.env.CLOUDCMS_APPSERVER_TIMESTAMP = new Date().getTime();
163
+ process.env.CLOUDCMS_APPSERVER_TIMESTAMP = Date.now();
164
164
 
165
165
  // // update all socket clients
166
166
  // process.IO.sockets.emit("timestamp", {
package/index.js CHANGED
@@ -39,6 +39,9 @@ process.defaultKeepAliveMs = (3 * 60 * 1000);
39
39
  // default http timeout (2 minutes)
40
40
  process.defaultHttpTimeoutMs = 2 * 60 * 1000;
41
41
 
42
+ // default exclusive lock timeout (2 minutes)
43
+ process.defaultExclusiveLockTimeoutMs = 2 * 60 * 1000;
44
+
42
45
  if (process.env.DEFAULT_HTTP_TIMEOUT_MS)
43
46
  {
44
47
  try
@@ -63,7 +66,7 @@ var HttpKeepAliveAgent = require('agentkeepalive');
63
66
  var HttpsKeepAliveAgent = require('agentkeepalive').HttpsAgent;
64
67
  http.globalAgent = new HttpKeepAliveAgent({
65
68
  keepAlive: true,
66
- keepAliveMsecs: 30000,
69
+ keepAliveMsecs: process.defaultKeepAliveMs,
67
70
  maxSockets: 24000,
68
71
  maxFreeSockets: 256,
69
72
  timeout: process.defaultHttpTimeoutMs,
@@ -71,7 +74,7 @@ http.globalAgent = new HttpKeepAliveAgent({
71
74
  });
72
75
  https.globalAgent = new HttpsKeepAliveAgent({
73
76
  keepAlive: true,
74
- keepAliveMsecs: 30000,
77
+ keepAliveMsecs: process.defaultKeepAliveMs,
75
78
  maxSockets: 24000,
76
79
  maxFreeSockets: 256,
77
80
  timeout: process.defaultHttpTimeoutMs,
@@ -695,11 +695,11 @@ exports = module.exports = function()
695
695
  return function(req, res, next) {
696
696
 
697
697
  // record filter start time
698
- var _auth_filter_start_ms = new Date().getTime();
698
+ var _auth_filter_start_ms = Date.now();
699
699
 
700
700
  fn(req, res, function(result, authenticator) {
701
701
 
702
- var _auth_filter_end_ms = new Date().getTime() - _auth_filter_start_ms;
702
+ var _auth_filter_end_ms = Date.now() - _auth_filter_start_ms;
703
703
 
704
704
  util.setHeader(res, "x-cloudcms-auth-filter-ms", _auth_filter_end_ms);
705
705
 
@@ -736,8 +736,11 @@ exports = module.exports = function()
736
736
  };
737
737
  }
738
738
 
739
- if (result.err && result.err.message) {
740
- req.log("Auth strategy: " + strategyId + " - filter error: " + result.err.message);
739
+ if (!result.skip)
740
+ {
741
+ if (result.err && result.err.message) {
742
+ req.log("Auth strategy: " + strategyId + " - filter error: " + result.err.message);
743
+ }
741
744
  }
742
745
 
743
746
  var providerId = null;
@@ -333,7 +333,7 @@ exports = module.exports = function()
333
333
  var reap = function() {
334
334
 
335
335
  // reap anything before a calculated time in the past
336
- var beforeMs = new Date().getTime() - maxAgeMs;
336
+ var beforeMs = Date.now() - maxAgeMs;
337
337
 
338
338
  // run expirations
339
339
  expire(beforeMs, function(err, updatedMembershipChannelIds, expiredUserIdsByChannelId) {
@@ -81,7 +81,7 @@ class AbstractAsyncProvider extends AbstractProvider
81
81
 
82
82
  channel.users[user.id] = {
83
83
  "user": user,
84
- "time": new Date().getTime()
84
+ "time": Date.now()
85
85
  };
86
86
 
87
87
  self.writeChannel(channelId, channel, function (err) {
@@ -53,11 +53,16 @@ exports = module.exports = function()
53
53
  provider.init(function(err) {
54
54
 
55
55
  // global caches
56
- process.deploymentDescriptorCache = createNamespacedCache.call(r, "descriptors");
56
+ process.deploymentDescriptorCache = createNamespacedCache.call(r, "descriptors", {
57
+ "seconds": 5 * 60 // 5 minutes (5 minutes * (60 seconds / minute) = 300 seconds)
58
+ });
57
59
  process.driverConfigCache = createNamespacedCache.call(r, "driverconfigs", {
58
60
  "seconds": 5 * 60 // 5 minutes (5 minutes * (60 seconds / minute) = 300 seconds)
59
61
  });
60
62
  process.subKeyMapCache = createNamespacedCache.call(r, "keyMap");
63
+ process.runtimeCache = createNamespacedCache.call(r, "runtimes", {
64
+ "seconds": 5 * 60 // 5 minutes (5 minutes * (60 seconds / minute) = 300 seconds)
65
+ });
61
66
 
62
67
  // subscribe to node invalidation broadcast events
63
68
  process.broadcast.subscribe("node_invalidation", function (message, channel, invalidationDone) {
@@ -26,7 +26,7 @@ exports = module.exports = function(cacheConfig)
26
26
 
27
27
  if (seconds > -1)
28
28
  {
29
- expirationTimeMap[key] = new Date().getTime() + (seconds * 1000);
29
+ expirationTimeMap[key] = Date.now() + (seconds * 1000);
30
30
  }
31
31
 
32
32
  callback();
@@ -39,7 +39,7 @@ exports = module.exports = function(cacheConfig)
39
39
  var expirationTime = expirationTimeMap[key];
40
40
  if (expirationTime)
41
41
  {
42
- var now = new Date().getTime();
42
+ var now = Date.now();
43
43
  if (now > expirationTime)
44
44
  {
45
45
  delete valueMap[key];
@@ -729,7 +729,7 @@ exports = module.exports = function()
729
729
 
730
730
  var cacheKey = cacheSettingsKey(application.ref(), "application", "application");
731
731
 
732
- var nowMs = new Date().getTime();
732
+ var nowMs = Date.now();
733
733
 
734
734
  var timestamp = CACHED_APP_SETTINGS_TIMESTAMPS[cacheKey];
735
735
  if (!timestamp || (nowMs - timestamp > CACHED_APP_SETTINGS_TTL))
@@ -376,7 +376,7 @@ module.exports = function(configStore)
376
376
 
377
377
  _watchLog("Detected changes on disk - reloading...");
378
378
 
379
- var t1 = new Date().getTime();
379
+ var t1 = Date.now();
380
380
 
381
381
  // reload context
382
382
  loadContext(function (err, context) {
@@ -390,7 +390,7 @@ module.exports = function(configStore)
390
390
  compileContextToRegistry(context);
391
391
  registry.reloadContext(context);
392
392
 
393
- var t2 = new Date().getTime();
393
+ var t2 = Date.now();
394
394
  _watchLog("Reloaded context in: " + (t2 - t1) + " ms");
395
395
  }
396
396
  catch (e)
@@ -3,6 +3,8 @@ var http = require('http');
3
3
  var util = require("../../util/util");
4
4
  var async = require("async");
5
5
 
6
+ var Loaders = require("../../util/loaders");
7
+
6
8
  var Gitana = require("gitana");
7
9
 
8
10
  ////////////////////////////////////////////////////////////////////////////
@@ -38,6 +40,27 @@ exports = module.exports = function()
38
40
  var r = {};
39
41
 
40
42
  var doConnect = r.doConnect = function(req, gitanaConfig, callback)
43
+ {
44
+ var key = JSON.stringify(gitanaConfig);
45
+
46
+ var loader = function(req, gitanaConfig)
47
+ {
48
+ return function(cb)
49
+ {
50
+ _doConnect(req, gitanaConfig, function(err) {
51
+ cb.call(this, err);
52
+ });
53
+ }
54
+ }(req, gitanaConfig);
55
+
56
+ var exclusiveLoader = Loaders.exclusive(loader, key, process.defaultExclusiveLockTimeoutMs);
57
+
58
+ exclusiveLoader(function(err) {
59
+ callback.call(this, err);
60
+ });
61
+ };
62
+
63
+ var _doConnect = function(req, gitanaConfig, callback)
41
64
  {
42
65
  // either connect anew or re-use an existing connection to Cloud CMS for this application
43
66
  Gitana.connect(gitanaConfig, function(err) {
@@ -45,7 +68,7 @@ exports = module.exports = function()
45
68
  if (err)
46
69
  {
47
70
  // log as much as we can
48
- if(process.env.NODE_ENV === "production")
71
+ if (process.env.NODE_ENV === "production")
49
72
  {
50
73
  console.warn("Error connecting driver (domainHost=" + req.domainHost + ", virtualHost: " + req.virtualHost + ", err: " + JSON.stringify(err));
51
74
  }
@@ -90,7 +113,7 @@ exports = module.exports = function()
90
113
  var rootStore = req.stores.root;
91
114
 
92
115
  var originalFilename = "gitana.json";
93
- var backupFilename = "gitana.json.backup-" + new Date().getTime();
116
+ var backupFilename = "gitana.json.backup-" + Date.now();
94
117
 
95
118
  console.log("Backing up: gitana.json to: " + backupFilename);
96
119
  rootStore.writeFile(backupFilename, JSON.stringify(gitanaConfig, null, " "), function(err) {
@@ -16,7 +16,7 @@ exports = module.exports = function()
16
16
  */
17
17
  r.localeInterceptor = function()
18
18
  {
19
- return function(req, res, next)
19
+ return function locale(req, res, next)
20
20
  {
21
21
  var acceptLanguage = "en-us"
22
22
 
@@ -110,7 +110,7 @@ exports = module.exports = function()
110
110
 
111
111
  var cacheInfo = JSON.parse(cacheInfoText);
112
112
  var expireTime = cacheInfo.expireTime;
113
- if (new Date().getTime() > expireTime)
113
+ if (Date.now() > expireTime)
114
114
  {
115
115
  handleBadStream();
116
116
  }
@@ -196,7 +196,7 @@ exports = module.exports = function()
196
196
 
197
197
  // write a cache info file as well
198
198
  var cacheInfo = {
199
- "expireTime": new Date().getTime() + cacheTTL
199
+ "expireTime": Date.now() + cacheTTL
200
200
  };
201
201
  contentStore.writeFile(filePath + ".cache", JSON.stringify(cacheInfo), function() {
202
202
  _end.call(res, data, encoding);
@@ -4,6 +4,8 @@ var http = require('http');
4
4
  var util = require("../../util/util");
5
5
  var Gitana = require("gitana");
6
6
 
7
+ var Loaders = require("../../util/loaders");
8
+
7
9
  /**
8
10
  * Runtime middleware.
9
11
  *
@@ -56,7 +58,7 @@ exports = module.exports = function()
56
58
  var data = {};
57
59
 
58
60
  // generate a cache buster (in case we're in production mode)
59
- data.cb = new Date().getTime();
61
+ data.cb = Date.now();
60
62
  if (json.cb) {
61
63
  data.cb = json.cb;
62
64
  }
@@ -162,63 +164,87 @@ exports = module.exports = function()
162
164
  */
163
165
  r.interceptor = function()
164
166
  {
165
- return util.createInterceptor("runtime", function(req, res, next, stores, cache, configuration) {
166
-
167
+ var loadRuntime = function(req, res, callback)
168
+ {
167
169
  var store = req.stores.content;
170
+ var key = store.id;
168
171
 
169
- store.existsFile("runtime.json", function(exists) {
170
-
171
- if (exists)
172
+ // define the loader (loads runtime)
173
+ var loader = function(store)
174
+ {
175
+ return function(cb)
172
176
  {
173
- store.readFile("runtime.json", function(err, data) {
177
+ store.existsFile("runtime.json", function(exists) {
174
178
 
175
- if (err) {
176
- req.log("Error loading runtime.json");
177
- return next();
178
- }
179
+ if (exists)
180
+ {
181
+ store.readFile("runtime.json", function (err, data) {
182
+
183
+ if (err) {
184
+ return cb(err);
185
+ }
179
186
 
180
- req.runtime = JSON.parse(data);
181
- next();
187
+ return cb(null, JSON.parse(data));
188
+ });
189
+ }
190
+ else
191
+ {
192
+ // write initial file
193
+
194
+ var runtime = {};
195
+
196
+ // cache buster (cb)
197
+ runtime.cb = Date.now();
198
+ if (process.env.CLOUDCMS_RUNTIME_CB) {
199
+ runtime.cb = process.env.CLOUDCMS_RUNTIME_CB;
200
+ }
201
+
202
+ // release id
203
+ runtime.releaseId = null;
204
+ if (process.env.CLOUDCMS_RUNTIME_RELEASE_ID) {
205
+ runtime.releaseId = process.env.CLOUDCMS_RUNTIME_RELEASE_ID;
206
+ }
207
+
208
+ // branch id
209
+ runtime.branchId = null;
210
+ if (process.env.CLOUDCMS_RUNTIME_BRANCH_ID) {
211
+ runtime.branchId = process.env.CLOUDCMS_RUNTIME_BRANCH_ID;
212
+ }
213
+
214
+ // don't bother writing to disk if we don't have any state to set
215
+ if (!runtime.releaseId && !runtime.branchId) {
216
+ return cb(null, runtime);
217
+ }
218
+
219
+ // create runtime file
220
+ store.writeFile("runtime.json", JSON.stringify(runtime, null, 2), function (err) {
221
+ return cb(null, runtime);
222
+ });
223
+ }
182
224
  });
183
225
  }
184
- else
185
- {
186
- // write initial file
226
+ }(store);
187
227
 
188
- var data = {};
228
+ // wrap loader with caching + an exclusive lock
229
+ var cachedLoader = Loaders.cached(loader, process.runtimeCache, key);
230
+ var exclusiveLoader = Loaders.exclusive(cachedLoader, key, process.defaultExclusiveLockTimeoutMs);
189
231
 
190
- // cache buster (cb)
191
- data.cb = new Date().getTime();
192
- if (process.env.CLOUDCMS_RUNTIME_CB) {
193
- data.cb = process.env.CLOUDCMS_RUNTIME_CB;
194
- }
195
-
196
- // release id
197
- data.releaseId = null;
198
- if (process.env.CLOUDCMS_RUNTIME_RELEASE_ID) {
199
- data.releaseId = process.env.CLOUDCMS_RUNTIME_RELEASE_ID;
200
- }
201
-
202
- // branch id
203
- data.branchId = null;
204
- if (process.env.CLOUDCMS_RUNTIME_BRANCH_ID) {
205
- data.branchId = process.env.CLOUDCMS_RUNTIME_BRANCH_ID;
206
- }
207
-
208
- // don't bother writing if we don't have anything to persist
209
- if (!data.releaseId && !data.branchId)
210
- {
211
- req.runtime = data;
212
- next();
213
- }
214
-
215
- // create runtime file
216
- store.writeFile("runtime.json", JSON.stringify(data, null, " "), function(err) {
217
- req.runtime = data;
218
- next();
219
- });
232
+ exclusiveLoader(callback);
233
+ };
234
+
235
+ return util.createInterceptor("runtime", function(req, res, next, stores, cache, configuration) {
236
+
237
+ loadRuntime(req, res, function(err, runtime) {
238
+
239
+ if (err) {
240
+ req.log("Error loading runtime");
241
+ req.log(err);
242
+ return next();
220
243
  }
221
- })
244
+
245
+ req.runtime = runtime;
246
+ next();
247
+ });
222
248
  });
223
249
  };
224
250
 
@@ -16,12 +16,12 @@ exports = module.exports = function(engine, engineType, engineId, engineConfigur
16
16
  var debugStart = function(text) {
17
17
  if (DEBUG_LOG) {
18
18
  console.log("[" + engineId + "] " + text);
19
- t1 = new Date().getTime();
19
+ t1 = Date.now();
20
20
  }
21
21
  };
22
22
  var debugFinish = function(text) {
23
23
  if (DEBUG_LOG) {
24
- t2 = new Date().getTime();
24
+ t2 = Date.now();
25
25
  console.log("[" + engineId + "] " + text + ": " + (t2-t1) + " ms");
26
26
  }
27
27
  };
@@ -144,13 +144,13 @@ exports = module.exports = function()
144
144
  else if (stores)
145
145
  {
146
146
  CACHED_STORES_BY_HOST[host] = stores;
147
- CACHED_STORES_EXPIRATION_MS_BY_HOST[host] = new Date().getTime() + TTL_MS;
147
+ CACHED_STORES_EXPIRATION_MS_BY_HOST[host] = Date.now() + TTL_MS;
148
148
  }
149
149
 
150
150
  var val = CACHED_STORES_BY_HOST[host];
151
151
 
152
152
  var expTime = CACHED_STORES_EXPIRATION_MS_BY_HOST[host];
153
- if (expTime && new Date().getTime() > expTime)
153
+ if (expTime && Date.now() > expTime)
154
154
  {
155
155
  delete CACHED_STORES_BY_HOST[host];
156
156
  delete CACHED_STORES_EXPIRATION_MS_BY_HOST[host];
@@ -399,14 +399,13 @@ exports = module.exports = function()
399
399
  process.driverConfigCache.write(req.virtualHost, {
400
400
  "config": gitanaConfig
401
401
  }, function (err) {
402
- completionFunction(null, gitanaConfig);
402
+ completionFunction(err, gitanaConfig);
403
403
  });
404
404
  }
405
405
  else
406
406
  {
407
407
  // mark with sentinel
408
- process.driverConfigCache.write(req.virtualHost, SENTINEL_NOT_FOUND_VALUE, 5, function (err)
409
- {
408
+ process.driverConfigCache.write(req.virtualHost, SENTINEL_NOT_FOUND_VALUE, 5, function (err) {
410
409
  completionFunction();
411
410
  });
412
411
  }
@@ -8,23 +8,26 @@ var util = require("../../util/util");
8
8
  */
9
9
  exports = module.exports = function()
10
10
  {
11
+ var SENTINEL_NOT_FOUND_VALUE = "null";
12
+
11
13
  var r = {};
12
14
 
13
- r.interceptor = function() {
14
- return util.createInterceptor("virtualHost", function (req, res, next, stores, cache, configuration) {
15
+ r.interceptor = function()
16
+ {
17
+ return util.createInterceptor("virtualFiles", function (req, res, next, stores, cache, configuration) {
15
18
 
16
19
  var completionFunction = function (err, descriptor) {
17
20
 
18
21
  if (err) {
22
+ console.log("ERR: ", err);
23
+
19
24
  // something went wrong
20
- next();
21
- return;
25
+ return next();
22
26
  }
23
27
 
24
28
  if (!descriptor) {
25
29
  // nothing found
26
- next();
27
- return;
30
+ return next();
28
31
  }
29
32
 
30
33
  // yes, virtual host deployed, store a few interesting things on the request
@@ -53,48 +56,54 @@ exports = module.exports = function()
53
56
  // NOTE: null is a valid sentinel value (meaning none)
54
57
  process.deploymentDescriptorCache.read(req.virtualHost, function(err, descriptor) {
55
58
 
59
+ // check for null sentinel
60
+ if (descriptor === SENTINEL_NOT_FOUND_VALUE) {
61
+ return completionFunction();
62
+ }
63
+
56
64
  if (typeof(descriptor) !== "undefined" || descriptor === null) {
57
65
  // all done
58
- completionFunction(null, descriptor);
59
- return;
66
+ return completionFunction(null, descriptor);
60
67
  }
61
- else
62
- {
63
- // nothing in cache, load from disk
64
- // check if there is a descriptor on disk
65
- rootStore.existsFile("descriptor.json", function (exists) {
66
68
 
67
- if (exists) {
69
+ // nothing in cache, load from disk
70
+ // check if there is a descriptor on disk
71
+ rootStore.existsFile("descriptor.json", function (exists) {
68
72
 
69
- // load the descriptor
70
- rootStore.readFile("descriptor.json", function (err, descriptor) {
73
+ if (exists) {
71
74
 
72
- if (err) {
73
- // no file descriptor, virtual files not deployed
74
- next();
75
- return;
76
- }
75
+ // load the descriptor
76
+ rootStore.readFile("descriptor.json", function (err, descriptor) {
77
77
 
78
- // yes, there is a descriptor, so we have virtual files
78
+ if (err) {
79
+ // no file descriptor, virtual files not deployed
80
+ next();
81
+ return;
82
+ }
79
83
 
80
- // convert descriptor to JSON
81
- descriptor = JSON.parse(descriptor);
84
+ // yes, there is a descriptor, so we have virtual files
82
85
 
83
- // CACHE: write
84
- process.deploymentDescriptorCache.write(req.virtualHost, descriptor, function() {
86
+ // convert descriptor to JSON
87
+ descriptor = JSON.parse(descriptor);
85
88
 
86
- // all done
87
- completionFunction(null, descriptor);
89
+ // CACHE: write
90
+ process.deploymentDescriptorCache.write(req.virtualHost, descriptor, function() {
88
91
 
89
- });
92
+ // all done
93
+ completionFunction(null, descriptor);
90
94
 
91
95
  });
92
- }
93
- else {
96
+
97
+ });
98
+ }
99
+ else
100
+ {
101
+ // write null sentinel and return nothing
102
+ process.deploymentDescriptorCache.write(req.virtualHost, SENTINEL_NOT_FOUND_VALUE, function() {
94
103
  completionFunction();
95
- }
96
- });
97
- }
104
+ });
105
+ }
106
+ });
98
107
  });
99
108
 
100
109
  });
@@ -272,7 +272,7 @@ exports = module.exports = function()
272
272
 
273
273
  ensureInvalidate(function() {
274
274
 
275
- var now = new Date().getTime();
275
+ var now = Date.now();
276
276
 
277
277
  req.cache.read(WCM_PAGES, function (err, cachedPages) {
278
278
  req.cache.read(WCM_PAGES_CACHE_TIME, function(err, cachedPagesTime) {
@@ -356,7 +356,7 @@ exports = module.exports = function()
356
356
  {
357
357
  req.log("Falling back to using cached pages, will retain for " + WCM_PAGES_CACHE_RETRY_TIME_MS + " ms before trying again");
358
358
  req.cache.write(WCM_PAGES_CACHE_TIME, {
359
- "ms": (new Date().getTime() + WCM_PAGES_CACHE_RETRY_TIME_MS)
359
+ "ms": (Date.now() + WCM_PAGES_CACHE_RETRY_TIME_MS)
360
360
  });
361
361
 
362
362
  return finished(null, cachedPages);
@@ -386,7 +386,7 @@ exports = module.exports = function()
386
386
  // build out pages
387
387
  var loadedPages = {};
388
388
 
389
- var queryTimeMs = new Date().getTime();
389
+ var queryTimeMs = Date.now();
390
390
 
391
391
  branch.trap(function (err) {
392
392
 
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  },
7
7
  "name": "cloudcms-server",
8
8
  "description": "Cloud CMS Application Server Module",
9
- "version": "3.2.304",
9
+ "version": "3.2.306",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "git://github.com/gitana/cloudcms-server.git"
@@ -20,7 +20,7 @@
20
20
  "alpaca": "^1.5.27",
21
21
  "archiver": "^1.3.0",
22
22
  "async": "^3.2.3",
23
- "async-lock": "^1.3.2",
23
+ "async-lock": "^1.4.1",
24
24
  "aws-sdk": "^2.1208.0",
25
25
  "axios": "^1.5.0",
26
26
  "basic-auth": "^2.0.1",
package/server/index.js CHANGED
@@ -760,6 +760,47 @@ var startServer = function(config, startServerFinishedFn)
760
760
  {
761
761
  var app = express();
762
762
  app.disable('x-powered-by');
763
+
764
+ // customize the app use() method to provide sensible logging
765
+ app._use = app.use;
766
+ app.use = function(f)
767
+ {
768
+ if (typeof(f) !== "function")
769
+ {
770
+ return app._use.apply(app, arguments);
771
+ }
772
+
773
+ return app._use(function(f) {
774
+ return function(req, res, next)
775
+ {
776
+ var functionName = f.name;
777
+ if (!functionName) {
778
+ functionName = "unknown";
779
+ }
780
+
781
+ var id = req.id;
782
+
783
+ var startTime = process.hrtime();
784
+
785
+ f(req, res, function() {
786
+ var totalTime = process.hrtime(startTime);
787
+ var totalTimeMs = (totalTime[1] / 1000000).toFixed(2);
788
+ if (totalTimeMs > 100)
789
+ {
790
+ if (id)
791
+ {
792
+ console.log("[" + id + "](" + functionName + ") time: " + totalTimeMs);
793
+ }
794
+
795
+ //console.trace();
796
+ //process.exit(-1);
797
+ }
798
+
799
+ next();
800
+ });
801
+ }
802
+ }(f));
803
+ };
763
804
 
764
805
  initSession(function(err, initializedSession) {
765
806
 
@@ -873,7 +914,7 @@ var startServer = function(config, startServerFinishedFn)
873
914
 
874
915
  return message;
875
916
  });
876
-
917
+
877
918
  /*
878
919
  // debug headers being set
879
920
  app.use(function(req, res, next) {
@@ -887,7 +928,7 @@ var startServer = function(config, startServerFinishedFn)
887
928
  */
888
929
 
889
930
  // increment and assign request id
890
- app.use(function (req, res, next) {
931
+ app.use(function increment_and_assign_id(req, res, next) {
891
932
  requestCounter++;
892
933
  req.id = requestCounter;
893
934
  next();
@@ -897,7 +938,7 @@ var startServer = function(config, startServerFinishedFn)
897
938
  runFunctions(config.initFunctions, [app], function (err) {
898
939
 
899
940
  // retain originalUrl and originalPath since these can get modified along the way
900
- app.use(function (req, res, next) {
941
+ app.use(function retain_original_url_path(req, res, next) {
901
942
  req.originalUrl = req.url;
902
943
  req.originalPath = req.path;
903
944
  next();
@@ -907,7 +948,7 @@ var startServer = function(config, startServerFinishedFn)
907
948
  app.use(requestParam);
908
949
 
909
950
  // add req.log function
910
- app.use(function (req, res, next) {
951
+ app.use(function bind_req_log(req, res, next) {
911
952
 
912
953
  req._log = req.log = function (text/*, warn*/) {
913
954
 
@@ -1171,7 +1212,9 @@ var createHttpServer = function(app, done)
1171
1212
  try { socket.end(); } catch (e) { }
1172
1213
  try { socket.destroy(); } catch (e) { }
1173
1214
  });
1174
-
1215
+
1216
+ var c = 0;
1217
+
1175
1218
  // socket
1176
1219
  httpServer.on("connection", function (socket) {
1177
1220
  socket.setNoDelay(true);
@@ -1275,7 +1318,7 @@ var configureServer = function(config, app, httpServer, configureServerFinishedF
1275
1318
  }
1276
1319
 
1277
1320
  // SET INITIAL VALUE FOR SERVER TIMESTAMP
1278
- process.env.CLOUDCMS_APPSERVER_TIMESTAMP = new Date().getTime();
1321
+ process.env.CLOUDCMS_APPSERVER_TIMESTAMP = Date.now();
1279
1322
 
1280
1323
  // DUST
1281
1324
  runFunctions(config.dustFunctions, [app, duster.getDust()], function (err) {
@@ -0,0 +1,99 @@
1
+ var exports = module.exports;
2
+
3
+ var AsyncLock = require("async-lock");
4
+
5
+ var SENTINEL_NOT_FOUND_VALUE = "null";
6
+
7
+ /**
8
+ * Applies caching to a loader.
9
+ *
10
+ * A loader function is invoked like:
11
+ *
12
+ * loader(function(err, value) {
13
+ *
14
+ * });
15
+ *
16
+ * Its job is to load something from a remote place and then fire the callback.
17
+ *
18
+ * This method wraps caching around the loader for the given key. It returns a new loader
19
+ * that checks a given cache (key) for a value ahead of invoking the actual underlying loader.
20
+ *
21
+ * @param loader
22
+ * @param cache
23
+ * @param key
24
+ */
25
+ exports.cached = function(loader, cache, key)
26
+ {
27
+ return function(callback)
28
+ {
29
+ cache.read(key, function(err, value) {
30
+
31
+ if (value === SENTINEL_NOT_FOUND_VALUE) {
32
+ return callback();
33
+ }
34
+
35
+ if (value) {
36
+ return callback(null, value);
37
+ }
38
+
39
+ loader(function(err, value) {
40
+
41
+ if (err) {
42
+ return callback(err);
43
+ }
44
+
45
+ if (!value) {
46
+ return cache.write(key, SENTINEL_NOT_FOUND_VALUE, function () {
47
+ callback();
48
+ });
49
+ }
50
+
51
+ // write to cache
52
+ return cache.write(key, value, function () {
53
+ callback.call(this, null, value);
54
+ });
55
+ });
56
+ });
57
+ };
58
+ };
59
+
60
+ var lock = new AsyncLock();
61
+
62
+ /**
63
+ * Applies caching to a loader.
64
+ *
65
+ * A loader function is invoked like:
66
+ *
67
+ * loader(function(err, value) {
68
+ *
69
+ * });
70
+ *
71
+ * Its job is to load something from a remote place and then fire the callback.
72
+ *
73
+ * This method wraps an exclusive mutex lock around the given loader. This makes it so that only one
74
+ * invocation of this loader may run per key within the event loop.
75
+ *
76
+ * @param loader
77
+ * @param key
78
+ */
79
+ exports.exclusive = function(loader, key, timeout)
80
+ {
81
+ return function(callback)
82
+ {
83
+ var opts = {};
84
+ // up to 50000 tasks in the queue
85
+ opts.maxPending = 50000;
86
+ if (timeout) {
87
+ opts.timeout = timeout;
88
+ }
89
+
90
+ lock.acquire(key, function(releaseFn) {
91
+ loader(function(err, value) {
92
+ setTimeout(function() {
93
+ releaseFn();
94
+ }, 0);
95
+ callback.call(this, err, value);
96
+ });
97
+ }, opts);
98
+ };
99
+ };
package/util/util.js CHANGED
@@ -700,11 +700,8 @@ var retryGitanaRequest = exports.retryGitanaRequest = function(logMethod, gitana
700
700
  config.headers["Authorization"] = headers2["Authorization"];
701
701
 
702
702
  // make the request
703
- console.log("C: " + JSON.stringify(config, null, 2));
704
703
  request(config, function(err, response, body) {
705
- console.log("E: " + err);
706
- console.log("B: " + body);
707
-
704
+
708
705
  if (response)
709
706
  {
710
707
  // ok case (just callback)
@@ -850,43 +847,42 @@ var pluck = exports.pluck = function(list, propertyPath)
850
847
  //
851
848
  /////////////////////////////////////////////////////////////////////////////////////////////
852
849
 
853
- // global pool of interceptor timings
854
- process.timings = {};
855
- var incrementTimings = function(req, family, id, time)
856
- {
857
- var increment = function(map, family, id, time)
858
- {
859
- var key = family + ":" + id;
860
-
861
- // interceptor timings
862
- if (!map[key]) {
863
- map[key] = {
864
- count: 0,
865
- total: 0,
866
- avg: 0
867
- };
868
- }
869
-
870
- map[key].count++;
871
- map[key].total += time;
872
- map[key].avg = map[key].total / map[key].count;
873
- map[key].avg = map[key].avg.toFixed(2);
874
- };
875
-
876
- // increment global timings
877
- increment(process.timings, family, id, time);
878
-
879
- // increment request timings
880
- if (!req.timings) {
881
- req.timings = {};
882
- }
883
- increment(req.timings, family, id, time);
884
- };
885
-
886
- var getGlobalTimings = exports.getGlobalTimings = function()
887
- {
888
- return process.timings;
889
- };
850
+ // // global pool of interceptor timings
851
+ // var incrementTimings = function(req, family, id, time)
852
+ // {
853
+ // var increment = function(map, family, id, time)
854
+ // {
855
+ // var key = family + ":" + id;
856
+ //
857
+ // // interceptor timings
858
+ // if (!map[key]) {
859
+ // map[key] = {
860
+ // count: 0,
861
+ // total: 0,
862
+ // avg: 0
863
+ // };
864
+ // }
865
+ //
866
+ // map[key].count++;
867
+ // map[key].total += time;
868
+ // map[key].avg = map[key].total / map[key].count;
869
+ // map[key].avg = map[key].avg.toFixed(2);
870
+ // };
871
+ //
872
+ // // increment global timings
873
+ // increment(process.timings, family, id, time);
874
+ //
875
+ // // increment request timings
876
+ // if (!req.timings) {
877
+ // req.timings = {};
878
+ // }
879
+ // increment(req.timings, family, id, time);
880
+ // };
881
+ //
882
+ // var getGlobalTimings = exports.getGlobalTimings = function()
883
+ // {
884
+ // return process.timings;
885
+ // };
890
886
 
891
887
 
892
888
  /////////////////////////////////////////////////////////////////////////////////////////////
@@ -895,7 +891,6 @@ var getGlobalTimings = exports.getGlobalTimings = function()
895
891
  //
896
892
  /////////////////////////////////////////////////////////////////////////////////////////////
897
893
 
898
-
899
894
  var createHandler = exports.createHandler = function(id, configName, fn)
900
895
  {
901
896
  if (typeof(configName) === "function") {
@@ -903,30 +898,30 @@ var createHandler = exports.createHandler = function(id, configName, fn)
903
898
  configName = id;
904
899
  }
905
900
 
906
- return function(req, res, next) {
901
+ var func = function(req, res, next) {
907
902
 
908
- var startAt = process.hrtime();
903
+ //var startAt = process.hrtime();
909
904
 
910
905
  // override next so that we can capture completion of interceptor
911
906
  var _next = next;
912
907
  next = function(err) {
913
- if (startAt)
914
- {
915
- var diff = process.hrtime(startAt);
916
- var time = diff[0] * 1e3 + diff[1] * 1e-6;
917
- incrementTimings(req, "handler", id, time);
918
- startAt = null;
919
- }
908
+ // if (startAt)
909
+ // {
910
+ // var diff = process.hrtime(startAt);
911
+ // var time = diff[0] * 1e3 + diff[1] * 1e-6;
912
+ // incrementTimings(req, "handler", id, time);
913
+ // startAt = null;
914
+ // }
920
915
  return _next.call(_next, err);
921
916
  };
922
917
  onHeaders(res, function() {
923
- if (startAt)
924
- {
925
- var diff = process.hrtime(startAt);
926
- var time = diff[0] * 1e3 + diff[1] * 1e-6;
927
- incrementTimings(req, "handler", id, time);
928
- startAt = null;
929
- }
918
+ // if (startAt)
919
+ // {
920
+ // var diff = process.hrtime(startAt);
921
+ // var time = diff[0] * 1e3 + diff[1] * 1e-6;
922
+ // incrementTimings(req, "handler", id, time);
923
+ // startAt = null;
924
+ // }
930
925
  });
931
926
 
932
927
  req.configuration(configName, function(err, handlerConfiguration) {
@@ -943,6 +938,10 @@ var createHandler = exports.createHandler = function(id, configName, fn)
943
938
  fn(req, res, next, req.stores, req.cache, handlerConfiguration);
944
939
  });
945
940
  };
941
+
942
+ Object.defineProperty(func, "name", { value: id });
943
+
944
+ return func;
946
945
  };
947
946
 
948
947
  var createInterceptor = exports.createInterceptor = function(id, configName, fn)
@@ -952,30 +951,30 @@ var createInterceptor = exports.createInterceptor = function(id, configName, fn)
952
951
  configName = id;
953
952
  }
954
953
 
955
- return function(req, res, next) {
954
+ var func = function(req, res, next) {
956
955
 
957
- var startAt = process.hrtime();
956
+ //var startAt = process.hrtime();
958
957
 
959
958
  // override next so that we can capture completion of interceptor
960
959
  var _next = next;
961
960
  next = function(err) {
962
- if (startAt > -1)
963
- {
964
- var diff = process.hrtime(startAt);
965
- var time = diff[0] * 1e3 + diff[1] * 1e-6;
966
- incrementTimings(req, "interceptor", id, time);
967
- startAt = -1;
968
- }
961
+ // if (startAt > -1)
962
+ // {
963
+ // var diff = process.hrtime(startAt);
964
+ // var time = diff[0] * 1e3 + diff[1] * 1e-6;
965
+ // incrementTimings(req, "interceptor", id, time);
966
+ // startAt = -1;
967
+ // }
969
968
  return _next.call(_next, err);
970
969
  };
971
970
  onHeaders(res, function() {
972
- if (startAt > -1)
973
- {
974
- var diff = process.hrtime(startAt);
975
- var time = diff[0] * 1e3 + diff[1] * 1e-6;
976
- incrementTimings(req, "interceptor", id, time);
977
- startAt = -1;
978
- }
971
+ // if (startAt > -1)
972
+ // {
973
+ // var diff = process.hrtime(startAt);
974
+ // var time = diff[0] * 1e3 + diff[1] * 1e-6;
975
+ // incrementTimings(req, "interceptor", id, time);
976
+ // startAt = -1;
977
+ // }
979
978
  });
980
979
 
981
980
  req.configuration(configName, function(err, interceptorConfiguration) {
@@ -992,6 +991,10 @@ var createInterceptor = exports.createInterceptor = function(id, configName, fn)
992
991
  fn(req, res, next, req.stores, req.cache, interceptorConfiguration);
993
992
  });
994
993
  };
994
+
995
+ Object.defineProperty(func, "name", { value: id });
996
+
997
+ return func;
995
998
  };
996
999
 
997
1000
  var replaceAll = exports.replaceAll = function(text, find, replace)