Apply fGC to all global contexts for default values

This commit is contained in:
Nick O'Leary
2018-07-02 15:21:13 +01:00
parent 14882bda78
commit 038d821a7c
4 changed files with 239 additions and 111 deletions

View File

@@ -19,21 +19,23 @@ var log = require("../../log");
var memory = require("./memory");
var settings;
// A map of scope id to context instance
var contexts = {};
var globalContext = null;
var externalContexts = {};
var hasExternalContext = false;
// A map of store name to instance
var stores = {};
// Whether there context storage has been configured or left as default
var hasConfiguredStore = false;
var defaultStore = "_";
function init(_settings) {
settings = _settings;
externalContexts = {};
// init memory plugin
stores = {};
var seed = settings.functionGlobalContext || {};
externalContexts["_"] = memory();
externalContexts["_"].setGlobalContext(seed);
globalContext = createContext("global",seed);
contexts['global'] = globalContext;
contexts['global'] = createContext("global",seed);
}
function load() {
@@ -43,6 +45,8 @@ function load() {
var defaultIsAlias = false;
var promises = [];
if (plugins) {
var hasDefault = plugins.hasOwnProperty('default');
var defaultName;
for (var pluginName in plugins) {
if (plugins.hasOwnProperty(pluginName)) {
// "_" is a reserved name - do not allow it to be overridden
@@ -59,6 +63,9 @@ function load() {
defaultIsAlias = true;
continue;
}
if (!hasDefault && !defaultName) {
defaultName = pluginName;
}
var plugin;
if (plugins[pluginName].hasOwnProperty("module")) {
// Get the provided config and copy in the 'approved' top-level settings (eg userDir)
@@ -79,7 +86,7 @@ function load() {
}
try {
// Create a new instance of the plugin by calling its module function
externalContexts[pluginName] = plugin(config);
stores[pluginName] = plugin(config);
} catch(err) {
return reject(new Error(log._("context.error-loading-module",{module:pluginName,message:err.toString()})));
}
@@ -91,23 +98,39 @@ function load() {
}
// Open all of the configured contexts
for (var plugin in externalContexts) {
if (externalContexts.hasOwnProperty(plugin)) {
promises.push(externalContexts[plugin].open());
for (var plugin in stores) {
if (stores.hasOwnProperty(plugin)) {
promises.push(stores[plugin].open());
}
}
// If 'default' is an alias, point it at the right module - we have already
// checked that it exists
if (defaultIsAlias) {
externalContexts["default"] = externalContexts[plugins["default"]];
}
// There is a 'default' listed in the configuration
if (hasDefault) {
// If 'default' is an alias, point it at the right module - we have already
// checked that it exists. If it isn't an alias, then it will
// already be set to a configured store
if (defaultIsAlias) {
stores["default"] = stores[plugins["default"]];
}
stores["_"] = stores["default"];
} else if (defaultName) {
// No 'default' listed, so pick first in list as the default
stores["default"] = stores[defaultName];
stores["_"] = stores["default"];
} // else there were no stores list the config object - fall through
// to below where we default to a memory store
}
if (promises.length === 0) {
promises.push(externalContexts["_"].open())
// No stores have been configured. Setup the default as an instance
// of memory storage
stores["_"] = memory();
stores["default"] = stores["_"];
promises.push(stores["_"].open())
} else {
hasExternalContext = true;
// if there's configured storage then the lifecycle is slightly different
// - specifically, we don't delete node context on redeploy
hasConfiguredStore = true;
}
return resolve(Promise.all(promises));
});
@@ -122,12 +145,12 @@ function copySettings(config, settings){
}
function getContextStorage(storage) {
if (externalContexts.hasOwnProperty(storage)) {
if (stores.hasOwnProperty(storage)) {
// A known context
return externalContexts[storage];
} else if (externalContexts.hasOwnProperty("default")) {
return stores[storage];
} else if (stores.hasOwnProperty("default")) {
// Not known, but we have a default to fall back to
return externalContexts["default"];
return stores["default"];
} else {
// Not known and no default configured
var contextError = new Error(log._("context.error-use-undefined-storage", {storage:storage}));
@@ -136,14 +159,19 @@ function getContextStorage(storage) {
}
}
function createContext(id,seed) {
// Seed is only set for global context - sourced from functionGlobalContext
var scope = id;
var obj = seed || {};
var seedKeys;
if (seed) {
seedKeys = Object.keys(seed);
}
obj.get = function(key, storage, callback) {
var context;
if (!storage && !callback) {
context = externalContexts["_"];
context = stores["_"];
} else {
if (typeof storage === 'function') {
callback = storage;
@@ -154,12 +182,34 @@ function createContext(id,seed) {
}
context = getContextStorage(storage);
}
return context.get(scope, key, callback);
if (seed) {
// Get the value from the underlying store. If it is undefined,
// check the seed for a default value.
if (callback) {
context.get(scope,key,function(err, v) {
if (v === undefined) {
callback(err, seed[key]);
} else {
callback(err, v);
}
})
} else {
// No callback, attempt to do this synchronously
var storeValue = context.get(scope,key);
if (storeValue === undefined) {
return seed[key];
} else {
return storeValue;
}
}
} else {
return context.get(scope, key, callback);
}
};
obj.set = function(key, value, storage, callback) {
var context;
if (!storage && !callback) {
context = externalContexts["_"];
context = stores["_"];
} else {
if (typeof storage === 'function') {
callback = storage;
@@ -175,7 +225,7 @@ function createContext(id,seed) {
obj.keys = function(storage, callback) {
var context;
if (!storage && !callback) {
context = externalContexts["_"];
context = stores["_"];
} else {
if (typeof storage === 'function') {
callback = storage;
@@ -186,7 +236,18 @@ function createContext(id,seed) {
}
context = getContextStorage(storage);
}
return context.keys(scope, callback);
if (seed) {
if (callback) {
context.keys(scope, function(err,keys) {
callback(err,Array.from(new Set(seedKeys.concat(keys)).keys()));
});
} else {
var keys = context.keys(scope);
return Array.from(new Set(seedKeys.concat(keys)).keys())
}
} else {
return context.keys(scope, callback);
}
};
return obj;
}
@@ -203,21 +264,20 @@ function getContext(localId,flowId) {
if (flowId) {
newContext.flow = getContext(flowId);
}
if (globalContext) {
newContext.global = globalContext;
}
newContext.global = contexts['global'];
contexts[contextId] = newContext;
return newContext;
}
function deleteContext(id,flowId) {
if(!hasExternalContext){
if(!hasConfiguredStore){
// only delete context if there's no configured storage.
var contextId = id;
if (flowId) {
contextId = id+":"+flowId;
}
delete contexts[contextId];
return externalContexts["_"].delete(contextId);
return stores["_"].delete(contextId);
}else{
return Promise.resolve();
}
@@ -225,9 +285,9 @@ function deleteContext(id,flowId) {
function clean(flowConfig) {
var promises = [];
for(var plugin in externalContexts){
if(externalContexts.hasOwnProperty(plugin)){
promises.push(externalContexts[plugin].clean(Object.keys(flowConfig.allNodes)));
for(var plugin in stores){
if(stores.hasOwnProperty(plugin)){
promises.push(stores[plugin].clean(Object.keys(flowConfig.allNodes)));
}
}
for (var id in contexts) {
@@ -243,9 +303,9 @@ function clean(flowConfig) {
function close() {
var promises = [];
for(var plugin in externalContexts){
if(externalContexts.hasOwnProperty(plugin)){
promises.push(externalContexts[plugin].close());
for(var plugin in stores){
if(stores.hasOwnProperty(plugin)){
promises.push(stores[plugin].close());
}
}
return Promise.all(promises);

View File

@@ -109,10 +109,6 @@ Memory.prototype.clean = function(activeNodes){
return Promise.resolve();
}
Memory.prototype.setGlobalContext= function(seed){
this.data["global"] = seed;
};
module.exports = function(config){
return new Memory(config);
};
};