-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.js
72 lines (57 loc) · 2 KB
/
app.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
/* global $ circuitBreaker */
(function appInitialization () {
$(() => {
$('#flakey').click(_ => circuit.fire().catch(console.error));
$('.clear').click(_ => {
$('.clear').parent().find('#flakeyResponse').remove();
});
});
const route = '/flakeyService';
const element = '#flakeyResponse';
const circuitBreakerOptions = {
timeout: 500,
errorThresholdPercentage: 50,
resetTimeout: 5000
};
const circuit = new circuitBreaker(_ => $.get(route), circuitBreakerOptions);
circuit.fallback(_ =>
({ body: `${route} unavailable right now. Try later.` }));
circuit.status.on('snapshot', stats => {
const response = document.createElement('p');
$(response).addClass('stats');
Object.keys(stats).forEach(key => {
const p = document.createElement('p');
p.append(`${key}: ${stats[key]}`);
$(response).append(p);
});
$('#stats').children().replaceWith($(response));
});
circuit.on('success',
result => $(element).prepend(
makeNode(`SUCCESS: ${JSON.stringify(result)}`)));
circuit.on('timeout',
() => $(element).prepend(
makeNode(`TIMEOUT: ${route} is taking too long to respond.`)));
circuit.on('reject',
() => $(element).prepend(
makeNode(`REJECTED: The breaker for ${route} is open. Failing fast.`)));
circuit.on('open',
() => $(element).prepend(
makeNode(`OPEN: The breaker for ${route} just opened.`)));
circuit.on('halfOpen',
() => $(element).prepend(
makeNode(`HALF_OPEN: The breaker for ${route} is half open.`)));
circuit.on('close',
() => $(element).prepend(
makeNode(`CLOSE: The breaker for ${route} has closed. Service OK.`)));
circuit.on('fallback',
data => $(element).prepend(
makeNode(`FALLBACK: ${JSON.stringify(data)}`)));
function makeNode (body) {
const response = document.createElement('p');
$(response).addClass(body.substring(0, body.indexOf(':')).toLowerCase());
response.append(body);
return response;
}
})();