Skip to content

Commit

Permalink
Merge 6f3db64 into f645513
Browse files Browse the repository at this point in the history
  • Loading branch information
Rautherdir committed Feb 7, 2019
2 parents f645513 + 6f3db64 commit 9d954de
Show file tree
Hide file tree
Showing 38 changed files with 1,059 additions and 69 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/**
build/**
examples/**
examples/**
test/json/**
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

# Editor config
.vscode
22 changes: 15 additions & 7 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ folders. This also starts the application life-cycle so the app can stop gracefu
<a name="spawnpoint"></a>

## spawnpoint
Agnostic JS framework that empowers devs to focus on quickly building apps, rather than focusing on applicationconfig, health-checks, application structure, or architecture to build a 12 factor app in Docker.Spawnpoint can be configured to manage the entire application life-cycle or standalone as a utility library.
Agnostic JS framework that empowers devs to focus on quickly building apps, rather than focusing on application
config, health-checks, application structure, or architecture to build a 12 factor app in Docker.

Spawnpoint can be configured to manage the entire application life-cycle or standalone as a utility library.

**Kind**: global class

Expand Down Expand Up @@ -60,7 +63,8 @@ Creates new instance of spawnpoint
<a name="spawnpoint+recursiveList"></a>

### spawnpoint.recursiveList(dir, [exts]) ⇒ <code>Array</code>
Recursively list files in a directory by an optional file extension.NOTE: This is an event blocking sync method.
Recursively list files in a directory by an optional file extension.
NOTE: This is an event blocking sync method.

**Kind**: instance method of [<code>spawnpoint</code>](#spawnpoint)
**Returns**: <code>Array</code> - Absolute/full path of filenames found.
Expand All @@ -86,7 +90,8 @@ Utility: Create random string.
<a name="spawnpoint+sample"></a>

### spawnpoint.sample(items) ⇒ <code>\*</code>
Utility: get random element from `collection`.This is a copy of the lodash _.sample method.
Utility: get random element from `collection`.
This is a copy of the lodash _.sample method.

**Kind**: instance method of [<code>spawnpoint</code>](#spawnpoint)
**Returns**: <code>\*</code> - Returns the random element.
Expand Down Expand Up @@ -153,7 +158,8 @@ Helper method that requires a file and hoists the current spawnpoint application
<a name="spawnpoint+code"></a>

### spawnpoint.code(code, [data]) ⇒ <code>Object</code>
Builds a Spawnpoint code object. Codes are used to create a link between a human readable messageand a computer readable string. Example: `file.not_found` -> "The requested file was not found."
Builds a Spawnpoint code object. Codes are used to create a link between a human readable message
and a computer readable string. Example: `file.not_found` -> "The requested file was not found."

**Kind**: instance method of [<code>spawnpoint</code>](#spawnpoint)
**Returns**: <code>Object</code> - Code Object with a `message` with the computer readable message and the `code` matching the input code.
Expand Down Expand Up @@ -192,7 +198,7 @@ Spawnpoint code that wraps a Javascript `Error`, as a soft error.
<a name="spawnpoint+registerLimit"></a>

### spawnpoint.registerLimit(code, threshold, options, callback) ⇒ <code>this</code>
Error Monitoring, when enabled. This allows you to track how often an error occurs and issue a callback once that threadhold is met.
Error Monitoring, when enabled. This allows you to track how often an error occurs and issue a callback once that threshold is met.

**Kind**: instance method of [<code>spawnpoint</code>](#spawnpoint)

Expand Down Expand Up @@ -265,7 +271,8 @@ Registers multiple custom Errors to a specific errorCode, using the `registerErr
<a name="spawnpoint+maskErrorToCode"></a>

### spawnpoint.maskErrorToCode(error) ⇒ <code>errorCode</code> \| <code>false</code>
Checks for Spawnpoint wrapped code, errorCode, or failCode when a potential error map is found (and previously registered). This method is useful as middleware to your applicationerror handling so that you don't have to have the server reply with a generic error.
Checks for Spawnpoint wrapped code, errorCode, or failCode when a potential error map is found (and previously registered). This method is useful as middleware to your application
error handling so that you don't have to have the server reply with a generic error.

**Kind**: instance method of [<code>spawnpoint</code>](#spawnpoint)
**Returns**: <code>errorCode</code> \| <code>false</code> - Returns Spawnpoint mapped code, errorCode, or failCode or false when no mapped error was found.
Expand All @@ -277,6 +284,7 @@ Checks for Spawnpoint wrapped code, errorCode, or failCode when a potential erro
<a name="callback"></a>

## callback ⇒ <code>this</code>
Initializes framework to read the `configFile`, init config, Spawnpoint plugins, errorCodes and autoloadfolders. This also starts the application life-cycle so the app can stop gracefully.
Initializes framework to read the `configFile`, init config, Spawnpoint plugins, errorCodes and autoload
folders. This also starts the application life-cycle so the app can stop gracefully.

**Kind**: global typedef
79 changes: 47 additions & 32 deletions lib/spawnpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,12 @@ class spawnpoint extends EventEmitter{
threshold: threshold,
error: 'errorCode', // or failCode
index: null, // 'object.to.path' of unique index to track by
reset: true, // reset balance counter on callback
reset: 1, // reset balance counter to this on a subsequent callback. Give it a negative number to disable this.
time: null
});

opts.uuid = _.uniqueId();

if(!this.limitMaps[opts.error]){
this.limitMaps[opts.error] = {};
}
Expand Down Expand Up @@ -453,7 +455,7 @@ class spawnpoint extends EventEmitter{
config: this.config.log,
type: helpers.tag('WARN', chalk.yellow),
line: chalk.yellow(util.format.apply(this, arguments))
});
}, 'error');
return this;
}

Expand Down Expand Up @@ -634,7 +636,7 @@ class spawnpoint extends EventEmitter{
this.config.getRoundRobin = function(path){
if(!rrKeys[path]){
const items = self.config.get(path);
rrKeys[path] = self.roundRobbin(items);
rrKeys[path] = self.roundRobin(items);
}
return rrKeys[path].next();
};
Expand Down Expand Up @@ -946,7 +948,7 @@ class spawnpoint extends EventEmitter{
}

/**
* Internal: Starts the Spawnpoint errorCode & failCode tracking. Disabled by default unless `config.trackERrors` is enabled due to a larger
* Internal: Starts the Spawnpoint errorCode & failCode tracking. Disabled by default unless `config.trackErrors` is enabled due to a larger
* memory footprint required.
* This is step 7 of 8 to startup Spawnpoint
* @return {this}
Expand All @@ -965,37 +967,50 @@ class spawnpoint extends EventEmitter{
return; // no issue being tracked
}

var limit = self.limitMaps[type][error.code];
var limits = self.limitMaps[type][error.code];


const defaultIssues = {
occurrences: 0, // long count, track
balance: 0, // time-based balance
dateFirst: Math.floor(Date.now() / 1000),
dateLast: null,
datesTriggered: [],
triggered: false // track if we've triggered the current balance
};

// new issue
if(!issues[type][error.code]){
issues[type][error.code] = {
occurrences: 0, // long count, track
balance: 0, // time-based balance
dateFirst: Math.floor(Date.now() / 1000),
dateLast: null,
datesTriggered: [],
triggered: false // track if we've triggered the current balance
};
}
issues[type][error.code].occurrences++;
issues[type][error.code].balance++;
issues[type][error.code].dateLast = Math.floor(Date.now() / 1000);

if(limit.time){
setTimeout(function(){
issues[type][error.code].balance--;
if(issues[type][error.code].balance <= 0){
issues[type][error.code].balance = 0;
issues[type][error.code].triggered = false;
}
}, limit.time);
}
if(!issues[type][error.code].triggered && issues[type][error.code].balance > limit.threshold){
issues[type][error.code].triggered = true;
limit.callback(_.clone(issues[type][error.code]));
issues[type][error.code].datesTriggered.push(issues[type][error.code].dateLast); // add after callback, to avoid double dates
issues[type][error.code] = {};
issues[type][error.code]['Global'] = _.pick(defaultIssues, ['occurrences', 'dateFirst', 'dateLast']);
}

issues[type][error.code]['Global'].occurrences++;
issues[type][error.code]['Global'].dateLast = Math.floor(Date.now() / 1000);

limits.forEach((limit) => {
// new issue
if(!issues[type][error.code][limit.uuid]){
issues[type][error.code][limit.uuid] = _.pick(defaultIssues, ['balance', 'triggered', 'datesTriggered']);
}
issues[type][error.code][limit.uuid].balance++;
if(limit.time){
setTimeout(function(){
issues[type][error.code][limit.uuid].balance--;
if(issues[type][error.code][limit.uuid].balance <= 0){
issues[type][error.code][limit.uuid].balance = 0;
issues[type][error.code][limit.uuid].triggered = false;
}
}, limit.time);
}
if(!issues[type][error.code][limit.uuid].triggered && issues[type][error.code][limit.uuid].balance >= limit.threshold){
issues[type][error.code][limit.uuid].triggered = true;
limit.callback(_.merge(_.clone(issues[type][error.code][limit.uuid]), _.clone(issues[type][error.code]['Global'])));
issues[type][error.code][limit.uuid].datesTriggered.push(issues[type][error.code]['Global'].dateLast); // add after callback, to avoid double dates
}else if(issues[type][error.code][limit.uuid].triggered && limit.reset >= 0){
issues[type][error.code][limit.uuid].triggered = false;
issues[type][error.code][limit.uuid].balance = limit.reset;
}
});
}
self.on(type, limitToErrors);
});
Expand Down

0 comments on commit 9d954de

Please sign in to comment.