-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
69 lines (56 loc) · 1.73 KB
/
core.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
const http = require('./http'),
logger = require('./logger'),
python = require('./python'),
CONFIG = require('./config'),
FILE_ID = 'core',
REGISTRATION_TIMEOUT = 2000;
module.exports = new class Alb3rtCore {
constructor() {
this.registrationTimeoutModifier = 1;
this.register = this.register.bind(this);
this.onRegisterError = this.onRegisterError.bind(this);
this.startServer();
this.registrationTimeout = setTimeout(this.register, this.registrationTimeoutModifier * REGISTRATION_TIMEOUT);
}
startServer() {
logger.log(FILE_ID, `Booting ${CONFIG.APP.NAME}...`);
require('./http-server').boot();
}
register() {
const urlRegistry = CONFIG.URL.REGISTRY;
if (urlRegistry) {
http.post({
url: `http://${urlRegistry}/api/registry`,
body: CONFIG.APP
})
.then(this.onRegisterSuccess)
.catch(this.onRegisterError);
}
}
onRegisterError(error) {
logger.error(FILE_ID, `Registration failed - ${error}`);
if (error !== CONFIG.CONSTANTS.HTTP_CODE.CONFLICT) {
clearTimeout(this.registrationTimeout);
this.registrationTimeoutModifier += 1;
this.registrationTimeout = setTimeout(this.register, REGISTRATION_TIMEOUT * this.registrationTimeoutModifier);
}
}
onRegisterSuccess() {
logger.log(FILE_ID, 'Registration successful.');
}
get api() {
return require('./api');
}
get config() {
return require('./config');
}
get http() {
return http;
}
get logger() {
return logger;
}
get python() {
return python;
}
};