-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathExecutorHelper.js
More file actions
73 lines (62 loc) · 1.95 KB
/
ExecutorHelper.js
File metadata and controls
73 lines (62 loc) · 1.95 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
/* globals define */
define([
'superagent',
'module',
'q'
], function(
superagent,
module,
Q
) {
const WORKER_ENDPOINT = '/rest/executor/worker';
const JOBS_ENDPOINT = '/rest/executor';
const ExecutorHelper = {};
ExecutorHelper.url = function(urlPath) {
if (typeof window === 'undefined') {
const modulePath = 'src/common/compute/backends/gme/ExecutorHelper.js';
const configPath = module.uri.replace(modulePath, 'config/index.js');
const gmeConfig = require.nodeRequire(configPath);
return `http://127.0.0.1:${gmeConfig.server.port}${urlPath}`;
}
return urlPath;
};
ExecutorHelper.get = function(urlPath, token) {
const deferred = Q.defer();
const url = this.url(urlPath);
const req = superagent.get(url);
if (token) {
req.set('Authorization', 'Bearer ' + token);
}
req.end((err, res) => {
if (err) {
return deferred.reject(err);
}
deferred.resolve(JSON.parse(res.text));
});
return deferred.promise;
};
ExecutorHelper.getWorkers = function(webgmeToken) {
return this.get(WORKER_ENDPOINT, webgmeToken)
.then(workerDict => Object.values(workerDict));
};
ExecutorHelper.getJobs = function(webgmeToken) {
return this.get(JOBS_ENDPOINT, webgmeToken);
};
ExecutorHelper.cancelJob = function(jobId, token) {
const endpoint = `/rest/executor/cancel/${jobId}`;
const deferred = Q.defer();
const url = this.url(endpoint);
const req = superagent.post(url);
if (token) {
req.set('Authorization', 'Bearer ' + token);
}
req.end(err => {
if (err) {
return deferred.reject(err);
}
deferred.resolve();
});
return deferred.promise;
};
return ExecutorHelper;
});