This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EngineEntry.js
114 lines (103 loc) · 4.04 KB
/
EngineEntry.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const logger = require('./logger/Logger').get();
const EngineStatusFetcher = require('./EngineStatusFetcher');
const Config = require('./Config');
/**
* Helper for periodical health checking.
* An {@link EngineEntry} object must be set as this before calling.
*/
async function checkStatus() {
let health;
let metrics;
if (!this.running) {
return;
}
try {
health = await this.statusFetcher.fetch(this.properties.statusIp, this.properties.engine.port, '/healthcheck');
this.properties.engine.health = health;
metrics = await this.statusFetcher.fetch(this.properties.statusIp, this.properties.engine.metricsPort, '/metrics');
this.properties.engine.metrics = metrics;
this.properties.engine.status = 'OK';
// Log and reset flag if last status check was failed
if (!this.statusSuccessful) {
logger.info(`Engine health and metrics check restored on ${this.properties.statusIp}:${this.properties.engine.port}`);
this.statusSuccessful = true;
}
} catch (err) {
// Only log on first failure
if (this.statusSuccessful) {
if (!health) {
logger.warn(`Engine health check failed on ${this.properties.statusIp}:${this.properties.engine.port}`);
this.properties.engine.health = undefined;
this.properties.engine.status = 'UNHEALTHY';
} else if (!metrics) {
logger.warn(`Engine metrics check failed on ${this.properties.statusIp}:${this.properties.engine.metricsPort}`);
this.properties.engine.metrics = undefined;
this.properties.engine.status = 'NO_METRICS';
}
this.statusSuccessful = false;
}
}
setTimeout(() => checkStatus.call(this), this.updateInterval);
}
/**
* Engine entry class definition.
* @prop {object} properties - Properties of the engine instance.
* @prop {number} updateInterval - The status update interval in milliseconds.
* @prop {EngineStatusFetcher} statusFetcher - The status fetcher to use.
* Optional and mainly used for testing; if not supplied, a default
* implementation will be used.
*/
class EngineEntry {
/**
* Creates new {@link EngineEntry} object.
* @param {object} properties - Properties of the engine instance.
* @param {number} updateInterval - The status update interval in milliseconds.
* @param {EngineStatusFetcher} statusFetcher - The status fetcher to use.
* Optional and mainly used for testing; if not supplied, a default
* implementation will be used.
*/
constructor(properties, updateInterval, statusFetcher) {
this.running = false;
this.statusSuccessful = true;
this.properties = properties;
this.updateInterval = updateInterval;
this.statusFetcher = statusFetcher || new EngineStatusFetcher();
// Set api and metrics port of the engine
const labels = this.properties.engine.labels || {};
if (labels[Config.engineAPIPortLabel]) {
this.properties.engine.port = parseInt(labels[Config.engineAPIPortLabel], 10);
} else {
logger.debug(`Engine entry missing api port label: ${Config.engineAPIPortLabel}, defaulting to port: ${Config.defaultEngineAPIPort}`);
this.properties.engine.port = Config.defaultEngineAPIPort;
}
if (labels[Config.engineMetricsPortLabel]) {
this.properties.engine.metricsPort = parseInt(labels[Config.engineMetricsPortLabel], 10);
} else {
logger.debug(`Engine entry missing metrics port label: ${Config.engineMetricsPortLabel}, defaulting to port: ${Config.defaultEngineMetricsPort}`);
this.properties.engine.metricsPort = Config.defaultEngineMetricsPort;
}
}
updateOrchestrationProperties(properties) {
Object.keys(properties).forEach((key) => {
if (key !== 'key' && key !== 'engine' && key !== 'statusIp') {
this.properties[key] = properties[key];
}
});
}
/**
* Starts periodical status checking.
*/
startStatusChecks() {
if (!this.running) {
this.running = true;
checkStatus.call(this);
}
}
/**
* Stops periodical status checking.
*/
stopStatusChecks() {
this.running = false;
}
}
module.exports = EngineEntry;