From 31ee1be81e261ef890860dd236446ea9ec042a99 Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Thu, 19 Jul 2018 07:40:52 +0900 Subject: [PATCH 1/4] add logging of context store --- red/runtime/index.js | 1 + red/runtime/locales/en-US/runtime.json | 4 ++- red/runtime/nodes/context/index.js | 25 +++++++++++++- red/runtime/nodes/context/localfilesystem.js | 18 ++++++++++ red/runtime/nodes/context/memory.js | 6 ++++ red/runtime/nodes/index.js | 3 +- test/red/runtime/nodes/context/index_spec.js | 34 +++++++++++++++++++ .../nodes/context/localfilesystem_spec.js | 10 +++++- test/red/runtime/nodes/context/memory_spec.js | 7 ++++ 9 files changed, 104 insertions(+), 4 deletions(-) diff --git a/red/runtime/index.js b/red/runtime/index.js index e8af69712..21197f8bc 100644 --- a/red/runtime/index.js +++ b/red/runtime/index.js @@ -163,6 +163,7 @@ function start() { if (settings.httpStatic) { log.info(log._("runtime.paths.httpStatic",{path:path.resolve(settings.httpStatic)})); } + redNodes.logContextStores(); redNodes.loadFlows().then(redNodes.startFlows).catch(function(err) {}); started = true; }).catch(function(err) { diff --git a/red/runtime/locales/en-US/runtime.json b/red/runtime/locales/en-US/runtime.json index c0fac42dc..20a863d23 100644 --- a/red/runtime/locales/en-US/runtime.json +++ b/red/runtime/locales/en-US/runtime.json @@ -163,7 +163,9 @@ "error-module-not-defined": "'module' is not defined in '__storage__' of settings.contextStorage", "error-invalid-module-name": "Invalid context store name: '__name__'", "error-invalid-default-module": "Invalid storage '__storage__' is specified as a default storage", - "error-use-undefined-storage": "Undefined storage '__storage__' is specified" + "error-use-undefined-storage": "Undefined storage '__storage__' is specified", + "unknown-store": "Unknown store '__name__' specified. Use default store instead.", + "log-store-init": "Context store : '__name__' [__info__]" } } diff --git a/red/runtime/nodes/context/index.js b/red/runtime/nodes/context/index.js index a12943863..9e159f6bb 100644 --- a/red/runtime/nodes/context/index.js +++ b/red/runtime/nodes/context/index.js @@ -31,6 +31,27 @@ var defaultStore; // Whether there context storage has been configured or left as default var hasConfiguredStore = false; +// Unknown Stores +var unknownStores = {}; + +function logUnknownStore(name) { + var count = unknownStores[name] || 0; + if (count == 0) { + log.warn(log._("context.unknown-store", {name: name})); + count++; + unknownStores[name] = count; + } +} + +function logStores() { + for(var name in stores) { + if (name !== '_') { // ignore default store + var plugin = stores[name]; + log.info(log._("context.log-store-init", + {name:name, info:plugin.info() })); + } + } +} function init(_settings) { settings = _settings; @@ -158,6 +179,7 @@ function getContextStorage(storage) { return stores[storage]; } else if (stores.hasOwnProperty("_")) { // Not known, but we have a default to fall back to + logUnknownStore(storage); return stores["_"]; } else { // Not known and no default configured @@ -402,5 +424,6 @@ module.exports = { get: getContext, delete: deleteContext, clean: clean, - close: close + close: close, + logStores: logStores }; diff --git a/red/runtime/nodes/context/localfilesystem.js b/red/runtime/nodes/context/localfilesystem.js index 551289a95..0de8365e7 100644 --- a/red/runtime/nodes/context/localfilesystem.js +++ b/red/runtime/nodes/context/localfilesystem.js @@ -267,6 +267,24 @@ LocalFileSystem.prototype.clean = function(activeNodes){ }); } +LocalFileSystem.prototype.info = function() { + var self = this; + var conf = self.config; + var info = "module=localfilesystem"; + if (conf) { + if (conf.hasOwnProperty("base")) { + info += ",base='"+conf.base+"'"; + } + if (conf.hasOwnProperty("dir")) { + info += ",dir='"+conf.dir+"'"; + } + if (conf.hasOwnProperty("cache")) { + info += ",cache"; + } + } + return info; +} + module.exports = function(config){ return new LocalFileSystem(config); }; diff --git a/red/runtime/nodes/context/memory.js b/red/runtime/nodes/context/memory.js index 3e333724e..adc02b3ea 100644 --- a/red/runtime/nodes/context/memory.js +++ b/red/runtime/nodes/context/memory.js @@ -124,6 +124,12 @@ Memory.prototype._export = function() { return this.data; } +Memory.prototype.info = function() { + var self = this; + var conf = self.config; + var info = "module=memory"; + return info; +} module.exports = function(config){ return new Memory(config); diff --git a/red/runtime/nodes/index.js b/red/runtime/nodes/index.js index 4e89acc2d..eeb958678 100644 --- a/red/runtime/nodes/index.js +++ b/red/runtime/nodes/index.js @@ -223,5 +223,6 @@ module.exports = { // Contexts loadContextsPlugin: context.load, closeContextsPlugin: context.close, - listContextStores: context.listStores + listContextStores: context.listStores, + logContextStores: context.logStores }; diff --git a/test/red/runtime/nodes/context/index_spec.js b/test/red/runtime/nodes/context/index_spec.js index 5ec3ff5d7..21292d711 100644 --- a/test/red/runtime/nodes/context/index_spec.js +++ b/test/red/runtime/nodes/context/index_spec.js @@ -18,6 +18,7 @@ var should = require("should"); var sinon = require('sinon'); var path = require("path"); var Context = require("../../../../../red/runtime/nodes/context/index"); +var Log = require("../../../../../red/runtime/log"); describe('context', function() { describe('local memory',function() { @@ -792,4 +793,37 @@ describe('context', function() { }); }); }); + + describe('log', function() { + it('should log context store info', function(done) { + Context.init({ + contextStorage: { + memory: { + module: "memory", + congig: { + } + } + } + }); + Context.load().then(function() { + var loginfo = undefined; + var logmsg = undefined; + sinon.stub(Log, 'log', function(msg) { + loginfo = msg; + }); + sinon.stub(Log, '_', function(msg, info) { + logmsg = JSON.stringify(info); + return logmsg; + }); + Context.logStores(); + should.deepEqual(loginfo, { + level: Log.INFO, + msg: logmsg + }); + Log.log.restore(); + Log._.restore(); + done(); + }); + }); + }); }); diff --git a/test/red/runtime/nodes/context/localfilesystem_spec.js b/test/red/runtime/nodes/context/localfilesystem_spec.js index c462c389f..14cbe69c3 100644 --- a/test/red/runtime/nodes/context/localfilesystem_spec.js +++ b/test/red/runtime/nodes/context/localfilesystem_spec.js @@ -376,7 +376,7 @@ describe('localfilesystem',function() { }); }); - describe('if cache is enabled',function() { + describe('#if cache is enabled',function() { afterEach(function() { return context.clean([]).then(function(){ return context.close().then(function(){ @@ -496,4 +496,12 @@ describe('localfilesystem',function() { }); }); }); + + describe('#info', function() { + it('should return info', function() { + var context = LocalFileSystem({dir: "/tmp", base: "xyz", cache: true}); + var info = context.info(); + info.should.be.equal("module=localfilesystem,base='xyz',dir='/tmp',cache"); + }); + }); }); diff --git a/test/red/runtime/nodes/context/memory_spec.js b/test/red/runtime/nodes/context/memory_spec.js index 00582d986..56b3923a7 100644 --- a/test/red/runtime/nodes/context/memory_spec.js +++ b/test/red/runtime/nodes/context/memory_spec.js @@ -214,4 +214,11 @@ describe('memory',function() { }); }); + describe('#info',function() { + it('should return info', function() { + var info = context.info(); + info.should.be.equal("module=memory"); + }); + }); + }); From 761248157018c2d92009deab615ae204ebbe2cec Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Thu, 19 Jul 2018 14:12:01 +0900 Subject: [PATCH 2/4] ignore default store from logging --- red/runtime/nodes/context/index.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/red/runtime/nodes/context/index.js b/red/runtime/nodes/context/index.js index 9e159f6bb..467aa7b74 100644 --- a/red/runtime/nodes/context/index.js +++ b/red/runtime/nodes/context/index.js @@ -35,11 +35,13 @@ var hasConfiguredStore = false; var unknownStores = {}; function logUnknownStore(name) { - var count = unknownStores[name] || 0; - if (count == 0) { - log.warn(log._("context.unknown-store", {name: name})); - count++; - unknownStores[name] = count; + if (name) { + var count = unknownStores[name] || 0; + if (count == 0) { + log.warn(log._("context.unknown-store", {name: name})); + count++; + unknownStores[name] = count; + } } } From 65e67b6c3e1a656efe3bfd189c164b00b270235c Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Thu, 19 Jul 2018 14:18:27 +0900 Subject: [PATCH 3/4] add a space to align position of ":" --- red/runtime/locales/en-US/runtime.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/red/runtime/locales/en-US/runtime.json b/red/runtime/locales/en-US/runtime.json index 20a863d23..36a3ef314 100644 --- a/red/runtime/locales/en-US/runtime.json +++ b/red/runtime/locales/en-US/runtime.json @@ -165,7 +165,7 @@ "error-invalid-default-module": "Invalid storage '__storage__' is specified as a default storage", "error-use-undefined-storage": "Undefined storage '__storage__' is specified", "unknown-store": "Unknown store '__name__' specified. Use default store instead.", - "log-store-init": "Context store : '__name__' [__info__]" + "log-store-init": "Context store : '__name__' [__info__]" } } From a29527ec96884e8b36f30ef893714128f73fe0b5 Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Fri, 20 Jul 2018 23:26:47 +0900 Subject: [PATCH 4/4] use implicit logging of context store --- red/runtime/index.js | 8 ++--- red/runtime/nodes/context/index.js | 15 ++++----- red/runtime/nodes/context/localfilesystem.js | 18 ---------- red/runtime/nodes/context/memory.js | 7 ---- red/runtime/nodes/index.js | 3 +- test/red/runtime/nodes/context/index_spec.js | 33 ------------------- .../nodes/context/localfilesystem_spec.js | 7 ---- test/red/runtime/nodes/context/memory_spec.js | 7 ---- 8 files changed, 11 insertions(+), 87 deletions(-) diff --git a/red/runtime/index.js b/red/runtime/index.js index 21197f8bc..19e3dc17e 100644 --- a/red/runtime/index.js +++ b/red/runtime/index.js @@ -90,7 +90,6 @@ function start() { }) .then(function() { return storage.init(runtime)}) .then(function() { return settings.load(storage)}) - .then(function() { return redNodes.loadContextsPlugin()}) .then(function() { if (log.metric()) { @@ -163,9 +162,10 @@ function start() { if (settings.httpStatic) { log.info(log._("runtime.paths.httpStatic",{path:path.resolve(settings.httpStatic)})); } - redNodes.logContextStores(); - redNodes.loadFlows().then(redNodes.startFlows).catch(function(err) {}); - started = true; + redNodes.loadContextsPlugin().then(function () { + redNodes.loadFlows().then(redNodes.startFlows).catch(function(err) {}); + started = true; + }); }).catch(function(err) { console.log(err); }); diff --git a/red/runtime/nodes/context/index.js b/red/runtime/nodes/context/index.js index 467aa7b74..cb61ca187 100644 --- a/red/runtime/nodes/context/index.js +++ b/red/runtime/nodes/context/index.js @@ -45,13 +45,10 @@ function logUnknownStore(name) { } } -function logStores() { - for(var name in stores) { - if (name !== '_') { // ignore default store - var plugin = stores[name]; - log.info(log._("context.log-store-init", - {name:name, info:plugin.info() })); - } +function logStore(name, module) { + if (name !== '_') { // ignore default store + log.info(log._("context.log-store-init", + {name:name, info:"module="+module})); } } @@ -116,6 +113,7 @@ function load() { try { // Create a new instance of the plugin by calling its module function stores[pluginName] = plugin(config); + logStore(pluginName, plugins[pluginName].module); } catch(err) { return reject(new Error(log._("context.error-loading-module",{module:pluginName,message:err.toString()}))); } @@ -426,6 +424,5 @@ module.exports = { get: getContext, delete: deleteContext, clean: clean, - close: close, - logStores: logStores + close: close }; diff --git a/red/runtime/nodes/context/localfilesystem.js b/red/runtime/nodes/context/localfilesystem.js index 0de8365e7..551289a95 100644 --- a/red/runtime/nodes/context/localfilesystem.js +++ b/red/runtime/nodes/context/localfilesystem.js @@ -267,24 +267,6 @@ LocalFileSystem.prototype.clean = function(activeNodes){ }); } -LocalFileSystem.prototype.info = function() { - var self = this; - var conf = self.config; - var info = "module=localfilesystem"; - if (conf) { - if (conf.hasOwnProperty("base")) { - info += ",base='"+conf.base+"'"; - } - if (conf.hasOwnProperty("dir")) { - info += ",dir='"+conf.dir+"'"; - } - if (conf.hasOwnProperty("cache")) { - info += ",cache"; - } - } - return info; -} - module.exports = function(config){ return new LocalFileSystem(config); }; diff --git a/red/runtime/nodes/context/memory.js b/red/runtime/nodes/context/memory.js index adc02b3ea..1f76a1520 100644 --- a/red/runtime/nodes/context/memory.js +++ b/red/runtime/nodes/context/memory.js @@ -124,13 +124,6 @@ Memory.prototype._export = function() { return this.data; } -Memory.prototype.info = function() { - var self = this; - var conf = self.config; - var info = "module=memory"; - return info; -} - module.exports = function(config){ return new Memory(config); }; diff --git a/red/runtime/nodes/index.js b/red/runtime/nodes/index.js index eeb958678..4e89acc2d 100644 --- a/red/runtime/nodes/index.js +++ b/red/runtime/nodes/index.js @@ -223,6 +223,5 @@ module.exports = { // Contexts loadContextsPlugin: context.load, closeContextsPlugin: context.close, - listContextStores: context.listStores, - logContextStores: context.logStores + listContextStores: context.listStores }; diff --git a/test/red/runtime/nodes/context/index_spec.js b/test/red/runtime/nodes/context/index_spec.js index 21292d711..3500896c9 100644 --- a/test/red/runtime/nodes/context/index_spec.js +++ b/test/red/runtime/nodes/context/index_spec.js @@ -18,7 +18,6 @@ var should = require("should"); var sinon = require('sinon'); var path = require("path"); var Context = require("../../../../../red/runtime/nodes/context/index"); -var Log = require("../../../../../red/runtime/log"); describe('context', function() { describe('local memory',function() { @@ -794,36 +793,4 @@ describe('context', function() { }); }); - describe('log', function() { - it('should log context store info', function(done) { - Context.init({ - contextStorage: { - memory: { - module: "memory", - congig: { - } - } - } - }); - Context.load().then(function() { - var loginfo = undefined; - var logmsg = undefined; - sinon.stub(Log, 'log', function(msg) { - loginfo = msg; - }); - sinon.stub(Log, '_', function(msg, info) { - logmsg = JSON.stringify(info); - return logmsg; - }); - Context.logStores(); - should.deepEqual(loginfo, { - level: Log.INFO, - msg: logmsg - }); - Log.log.restore(); - Log._.restore(); - done(); - }); - }); - }); }); diff --git a/test/red/runtime/nodes/context/localfilesystem_spec.js b/test/red/runtime/nodes/context/localfilesystem_spec.js index 14cbe69c3..84a33982e 100644 --- a/test/red/runtime/nodes/context/localfilesystem_spec.js +++ b/test/red/runtime/nodes/context/localfilesystem_spec.js @@ -497,11 +497,4 @@ describe('localfilesystem',function() { }); }); - describe('#info', function() { - it('should return info', function() { - var context = LocalFileSystem({dir: "/tmp", base: "xyz", cache: true}); - var info = context.info(); - info.should.be.equal("module=localfilesystem,base='xyz',dir='/tmp',cache"); - }); - }); }); diff --git a/test/red/runtime/nodes/context/memory_spec.js b/test/red/runtime/nodes/context/memory_spec.js index 56b3923a7..00582d986 100644 --- a/test/red/runtime/nodes/context/memory_spec.js +++ b/test/red/runtime/nodes/context/memory_spec.js @@ -214,11 +214,4 @@ describe('memory',function() { }); }); - describe('#info',function() { - it('should return info', function() { - var info = context.info(); - info.should.be.equal("module=memory"); - }); - }); - });