-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.js
More file actions
237 lines (214 loc) · 7.08 KB
/
task.js
File metadata and controls
237 lines (214 loc) · 7.08 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
const config = require('@cheevr/config');
const Database = require('@cheevr/database');
const Logger = require('@cheevr/logging');
const path = require('path');
const Worker = require('./worker');
const log = Logger[config.tasks.logger];
const db = Database.factory(config.tasks.database);
const hostname = require('os').hostname();
/**
* Holds information about one tasks with all its workers
*/
class Task {
constructor(file) {
this._file = file;
this._name = path.basename(this.file, path.extname(this.file));
this._workers = [];
this.broadcast = new Proxy(this, {
get: (obj, method) => {
return (...args) => {
let promises = [];
for (let worker of obj.workers) {
promises.push(worker[method](...args));
}
return Promise.all(promises);
}
}
});
let idx = 0;
this.roundRobin = new Proxy(this, {
get: (obj, method) => {
return (...args) => {
if (!obj.workers.length) {
return;
}
idx = (idx + 1) % obj.workers.length;
return obj.workers[idx][method](...args);
}
}
});
// TODO why is this not a proper async method?
this.ready = () => new Promise(resolve => {
db.get({
index: config.tasks.index,
type: 'task',
id: hostname + '#' + this.name,
ignore: 404
}, (err, result) => {
if (err || !result._source) {
this._enabled = true;
this.workersWanted = 1;
db.create({
index: config.tasks.index,
type: 'task',
id: hostname + '#' + this.name,
refresh: true,
ignore: 409,
body: {
file: this.file,
host: hostname,
workers: 1,
enabled: this.enabled,
modified: Date.now()
}
}, err => resolve());
} else {
this._enabled = result._source.enabled;
this.workersWanted = result._source.workers;
resolve();
}
});
});
}
/**
* Will stop all workers for this tasks by setting the task to be disabled.
*/
kill() {
this._workers.map(worker => worker.kill());
this._workers = [];
}
/**
* Sets this task to be enabled or disabled. The state is saved in the database so that a restart will maintain
* this settings
* @param {boolean} enabled
*/
set enabled(enabled) {
if (this._enabled !== enabled) {
setImmediate(async () => {
await this.ready();
this._enabled = enabled;
enabled ? this.workersWanted = this._workersWanted : this.kill();
db.update({
index: config.tasks.index,
type: 'task',
id: hostname + '#' + this.name,
body: {
doc: {
enabled,
modified: Date.now()
}
}
});
})
}
}
/**
* Returns whether this task has been enabled or not. A disabled tasks will have no running workers.
* @returns {boolean}
*/
get enabled() {
return this._enabled;
}
get id() {
return this.file;
}
get file() {
return this._file;
}
get name() {
return this._name;
}
get workers() {
return this._workers;
}
get workersWanted() {
return this._workersWanted;
}
set workersWanted(count) {
if (this._workersWanted === count) {
return;
}
setImmediate(async () => {
this._workersWanted = count;
await this.ready();
await db.update({
index: config.tasks.index,
type: 'task',
id: hostname + '#' + this.name,
body: {
doc: {
workers: count,
modified: Date.now()
}
}
});
if (this._enabled) {
for (let nr = this._workers.length; nr <= count; nr++) {
this.addWorker();
}
for (let nr = this._workers.length; nr > count; nr--) {
this.removeWorker();
}
}
});
}
addWorker() {
let worker = new Worker(this);
worker._runner._childProcess.on('message', payload => {
if (this[payload.method]) {
payload.args = Array.isArray(payload.args) ? payload.args : [ payload.args ];
this[payload.method](worker, ...payload.args);
}
});
this._workers.push(worker);
return worker;
}
removeWorker() {
let worker = this._workers.pop();
if (!worker) {
return log.warn('Trying to stop worker for task "%s", when there are no more workers running', task.name);
}
worker.kill();
}
worker(id) {
for (let worker of this._workers) {
if (worker.id === id) {
return worker;
}
}
}
/*********************
*
* These methods can be called from anywhere, but are mainly used by the runner to update the task manager.
* The first argument (the worker) is bound automatically when called by a runner.
*
*/
/**
* Receives any error messages from the job runner
* @param {Worker} worker The worker id form which the message came
* @param {string} message An error message
* @param {string} [stack] A printable stack
*/
error(worker, message, stack) {
log.error('Error in worker "%s", task "%s": %s', worker.id, this.name, message);
stack && log.error(stack);
}
/**
* Sets the state of any worker.
* @param {Worker} worker The worker id form which the message came
* @param {string} jobId The job id
* @param {string} state The state the worker is currently in (one of running, idle or error)
*/
state(worker, jobId, state) {
worker.setState(jobId, state);
}
/**
* Allows to set the number of workers a task should be running on. Will either spawn or kill additional processes.
* @param {Worker} worker The worker id form which the message came
* @param {number} count The number of workers we want to have running
*/
setWorkers(worker, count) {
this.workersWanted = count;
}
}
module.exports = Task;