-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrus.js
46 lines (37 loc) · 1.09 KB
/
grus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* jshint node:true */
"use strict";
module.exports = function(options) {
var CB = require("circuit-breakerjs").CircuitBreaker,
circuit = new CB(options),
run = circuit.run.bind(circuit),
name = options.name || "foo"; //TODO: better default name
// this doesn't watch the callback, but the action that triggers
// the callback
var wrap_callback = function(cb, fallback_fn) {
return function(err) {
var args = arguments;
var fallback = function() {
return fallback_fn(err);
};
var wrapper = function(success, failure, _) {
try {
if (err) {
console.warn("grus: action for '%s' failed: ", name, err);
failure();
return fallback();
}
success();
} catch (e) {
console.error("grus: callback for '%s' failed: ", name, e);
console.error(e.stack);
throw e;
}
return cb.apply(null, args);
};
run(wrapper, fallback);
};
};
return {name: name,
run: run,
wrap_callback: wrap_callback};
};