-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
47 lines (40 loc) · 1.13 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
'use strict';
const { Deferred } = require('./lib/util');
module.exports = app => {
let cachedInstances = {};
let isEurekaReady = false;
const eurekaDeffered = new Deferred();
app.messenger.on('eureka:RegistryUpdated', instances => {
cachedInstances = instances;
if (!isEurekaReady) {
isEurekaReady = true;
eurekaDeffered.resolve();
}
});
app.messenger.once('egg-ready', () => {
setTimeout(() => {
!isEurekaReady && app.messenger.sendToAgent('eureka:RegistryUpdated');
}, 2000);
});
app.eureka = {
async getAllInstances () {
if (!isEurekaReady) {
await eurekaDeffered.promise;
}
return cachedInstances;
},
async getInstancesByAppId (appId) {
if (!appId) {
throw new Error('Unable to query instances with no appId');
}
if (!isEurekaReady) {
await eurekaDeffered.promise;
}
const instances = cachedInstances[appId.toUpperCase()] || [];
if (instances.length === 0) {
app.logger.warn('[egg-eureka-pro] Unable to retrieve instances for appId: ' + appId);
}
return instances;
}
};
};