-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
103 lines (89 loc) · 3.37 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const redis = require('redis');
const log = require('debug')('vasco');
let db;
function connectDB() {
db = redis.createClient(process.env.VASCO_URL);
db.on('error', err => { throw err; });
}
function findDependencies(dependencies, mockDeps, done) {
const depUrls = {};
const depNames = Object.keys(dependencies);
depNames
.filter(name => !!mockDeps[name])
.forEach(name => depUrls[name] = mockDeps[name]);
const getServiceKey = name => name + '@' + dependencies[name];
const getURLKey = name => 'endpoints.' + getServiceKey(name);
const getAliveKey = url => 'alive.' + url;
const serviceDepNames = depNames.filter(name => !mockDeps[name]);
getAliveURLs(serviceDepNames, depUrls, done);
function getAliveURLs(deps, aliveURLs, callback) {
if (!deps.length) { return callback(null, aliveURLs); }
log('reqeusted dependencies:', deps);
const urlBatch = db.batch();
deps.forEach(name => urlBatch.srandmember(getURLKey(name)));
urlBatch.exec((urlErr, urls) => {
if (urlErr) { return callback(urlErr); }
log('found endpoints:', urls);
for (let i = 0; i < urls.length; i++) {
if (!urls[i]) {
return callback(new Error('No url for: ' + deps[i]));
}
}
const aliveBatch = db.batch();
urls.forEach(url => aliveBatch.get(getAliveKey(url)));
aliveBatch.exec((aliveErr, aliveValues) => {
if (aliveErr) { return callback(aliveErr); }
log('alive status for endpoints:', aliveValues);
const cleanUpBatch = db.batch();
deps.forEach((name, index) => {
if (aliveValues[index] === getServiceKey(name)) {
aliveURLs[name] = urls[index];
} else if (!aliveValues[index]) {
log('cleanup endpoint:', urls[index]);
cleanUpBatch.srem(getURLKey(name), urls[index]);
}
});
cleanUpBatch.exec(cleanUpErr => {
if (cleanUpErr) { return callback(cleanUpErr); }
const missingDeps = deps.filter((name, index) =>
aliveValues[index] !== getServiceKey(name));
log('request missing dependencies', missingDeps);
getAliveURLs(missingDeps, aliveURLs, callback);
});
});
});
}
}
function register(url, pkg, done) {
if (!url) { return done(new Error('Need url of service')); }
if (!pkg || !pkg.name || !pkg.version) {
return done(new Error('Invalid package'));
}
const opts = pkg.vasco || {};
const aliveDuration = opts.aliveDuration || 10; // seconds
const pkgNameVersion = pkg.name + '@' + pkg.version;
connectDB();
findDependencies(opts.dependencies || {}, opts.mocks || {}, (err, depUrls) => {
log('found dependencies:', depUrls);
if (err) { return done(err); }
const urlKey = 'endpoints.' + pkgNameVersion;
db.sadd(urlKey, url, err => {
if (err) { return done(err); }
db.end(true);
log('registered endpoint:', url);
setServiceHealth(url, err => done(err, depUrls));
});
});
function setServiceHealth(url, callback) {
const key = 'alive.' + url;
callback = callback || (err => { if (err) throw err; });
connectDB();
db.set(key, pkgNameVersion, 'EX', aliveDuration, err => {
if (err) { return callback(err); }
db.end(true);
setTimeout(setServiceHealth, aliveDuration * 1000, url);
callback();
});
}
}
module.exports = { findDependencies, register };