-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathqueues-manager.js
175 lines (163 loc) · 6.29 KB
/
queues-manager.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const EventEmitter = require('events');
const isEqual = require('lodash.isequal');
const log = require('@hkube/logger').GetLogFromContainer();
const etcd = require('./persistency/etcd');
const queueRunner = require('./queue-runner');
const component = require('./consts/component-name').JOBS_CONSUMER;
class QueuesManager extends EventEmitter {
constructor() {
super();
this._active = true;
this._lastActive = Date.now();
this._queues = new Map();
this._actions = {
add: (...args) => this._addAction(...args),
remove: (...args) => this._removeAction(...args, 'deleted', true),
};
}
async init(options) {
const { queueId } = options;
if (!queueId) {
throw new Error('queueId is required');
}
this._options = options;
this._queueId = queueId;
await etcd.watchQueueActions({ queueId });
etcd.onQueueAction((data) => {
this._handleAction(data);
});
this._watch();
const discovery = this._getDiscoveryData();
await etcd.discoveryRegister({ data: discovery });
this._livenessInterval();
log.info(`queue ${queueId} is up and running`, { component });
}
/**
* This method is responsible for updating the discovery data.
* and also doing self preparation for graceful shutdown.
*/
_livenessInterval() {
setTimeout(async () => {
try {
const { queueId, algorithms } = this._getDiscoveryData();
if (algorithms.length === 0) {
const idleTime = Date.now() - this._lastActive;
const isIdle = idleTime > this._options.algorithmQueueBalancer.minIdleTimeMS;
if (isIdle && this._active) {
this._active = false;
log.info(`queue ${queueId} is idle for ${(idleTime / 1000).toFixed(0)} sec, preparing for shutdown`, { component });
await etcd.unWatchQueueActions({ queueId });
}
}
else {
await this._checkIdleAlgorithms();
this._lastActive = Date.now();
}
await this._discoveryUpdate();
}
catch (e) {
log.throttle.error(`fail on discovery interval ${e}`, { component }, e);
}
finally {
this._livenessInterval();
}
}, this._options.algorithmQueueBalancer.livenessInterval);
}
/**
* This method is responsible for removing staled queues.
* stale queue is a queue which idle for X time and also empty and paused.
*/
async _checkIdleAlgorithms() {
const queues = Array.from(this._queues.values());
const isIdle = queues.filter(q => q.isIdle());
const isStaled = queues.filter(q => q.isStaled());
await Promise.all(queues.map(a => a.checkIdle()));
await Promise.all(isIdle.map(q => q.pause()));
await Promise.all(isStaled.map(async q => {
try {
await this._removeAction(q.algorithmName, 'idle', false);
}
catch (e) {
log.throttle.error(`error removing idle queue ${q.algorithmName} ${e.message}`, { component }, e);
}
}));
}
async _handleAction(data) {
try {
if (!this._active) {
log.throttle.error('queue is not active', { component });
return;
}
const { action, algorithmName } = data;
if (!algorithmName) {
log.throttle.error('job arrived without algorithm name', { component });
return;
}
if (!action) {
log.throttle.error('job arrived without action', { component });
return;
}
const method = this._actions[action];
if (!method) {
log.throttle.error(`invalid action ${action}`, { component });
return;
}
await method(algorithmName);
}
catch (e) {
log.throttle.error(`error on handle job ${e}`, { component });
}
}
async _addAction(algorithmName) {
if (this._queues.size === this._options.algorithmQueueBalancer.limit) {
log.throttle.warning(`max queues limit has been reached, total size: ${this._queues.size}`, { component });
return;
}
const queue = this._queues.get(algorithmName);
if (!queue) {
const props = { options: this._options, algorithmName };
const algorithmQueue = queueRunner.create(props);
this._queues.set(algorithmName, algorithmQueue);
await algorithmQueue.start(props);
log.info(`algorithm queue from type ${algorithmName} created`, { component });
}
else {
log.warning(`algorithm queue from type ${algorithmName} already exists`, { component });
}
}
async _removeAction(algorithmName, reason, force) {
const queue = this._queues.get(algorithmName);
if (queue) {
await queue.stop(force);
this._queues.delete(algorithmName);
log.info(`algorithm queue from type ${algorithmName} deleted. reason: ${reason}`, { component });
}
else {
log.warning(`algorithm queue from type ${algorithmName} not exists`, { component });
}
}
async _discoveryUpdate() {
const discovery = this._getDiscoveryData();
if (!isEqual(discovery, this._lastDiscoveryData)) {
this._lastDiscoveryData = discovery;
await etcd.discoveryUpdate({ ...discovery, timestamp: Date.now() });
}
}
_getDiscoveryData() {
const algorithms = Array.from(this._queues.keys());
return { queueId: this._queueId, algorithms, active: this._active };
}
_watch() {
etcd.on('job-change', (data) => {
this._queues.forEach(q => {
q.removeInvalidJob(data);
});
});
etcd.on('exec-change', (data) => {
this._queues.forEach(q => {
q.removeInvalidTasks(data);
});
});
}
}
module.exports = new QueuesManager();