This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rojob.js
213 lines (183 loc) · 6.05 KB
/
rojob.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
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
/**
* <odoc>
* <key>roJob.roJob(aURL, aAPIKey)</key>
* Creates a roJob instance for aURL with aAPIKey. If '[oPack roJob]/.apikey' exists it will be used by default if no aAPIKey is
* provided. If aURL is not provided it will default to http://127.0.0.1:8787.
* </odoc>
*/
var roJob = function(aURL, aAPIKey, aRetry, aRetryTime) {
this.retry = _$(aRetry).isNumber().default(40);
this.retryTime = _$(aRetryTime).isNumber().default(1500);
if (isArray(aURL)) {
this.urls = aURL;
} else {
if (isString(aURL)) {
this.urls = [ aURL ];
} else {
var nodes;
if (io.fileExists(getOPackPath("roJob") + "/work/.rojob")) {
var nodes = $from(io.listFilenames(getOPackPath("roJob") + "/work/.rojob"))
.ends(".pid")
.starts(getOPackPath("roJob") + "/work/.rojob/roJob_")
.select((r) => { var a = r.split("_"); return "http://" + a[1] + ":" + a[2].replace(".pid", ""); });
if (nodes.length > 0) {
this.urls = nodes;
}
}
if (isUnDef(this.urls)) {
this.urls = [ "http://127.0.0.1:8787" ];
}
}
}
this.APIKEY = aAPIKey;
if (isUnDef(this.APIKEY) && isDef(getOPackPath("roJob"))) {
if (io.fileExists(getOPackPath("roJob") + "/.apikey")) {
this.APIKEY = io.readFileString(getOPackPath("roJob") + "/.apikey").replace(/\n/g, "");
}
}
};
roJob.prototype.setRetry = function(aNum) {
this.retry = _$(aNum, "retry").isNumber().default(40);
};
roJob.prototype.setRetryTime = function(aNum) {
this.retryTime = _$(aNum, "retryTime").isNumber().default(1500);
};
roJob.prototype.getURL = function() {
return this.urls[Math.floor(Math.random()*this.urls.length)];
};
roJob.prototype.signRequestFn = function(uri, apiKey) {
this.APIKEY = apiKey;
ow.loadFormat();
return (r) => {
var signAuthorization = function(aURI, data) {
if (isDef(apiKey)) {
if (isString(data)) data = data.replace(/[\n \t\r]+$/, "");
//var kaURI = encodeURIComponent(aURI);
var dateStamp = Math.floor(nowUTC()/(1000*60*2));
var kDateStamp = hmacSHA256(dateStamp, "roJob" + apiKey);
var kURI = hmacSHA256(aURI, kDateStamp);
var kData = hmacSHA256(sha256((isMap(data) ? stringify(data, void 0, "") : data)), kURI);
//print(dateStamp);
//print("roJob" + apiKey);
//print(aURI);
//print(stringify(data, void 0, ""));
//print(sha256(stringify(data, void 0, "")));
return ow.format.string.toHex(kData, "").toLowerCase();
} else {
return void 0;
}
};
r.reqHeaders = _$(r.reqHeaders).isMap().default({});
r.reqHeaders.auth = signAuthorization(uri + $rest().index(r.aIdxMap), r.aDataRowMap);
return r;
};
};
roJob.prototype.__req = function(aURI, aURL, aData) {
var res, c = this.retry;
do {
res = $rest({ preAction: this.signRequestFn(aURI, this.APIKEY) })
.post(aURL, aData);
if (isDef(res.error)) {
c--;
sleep(this.retryTime, true);
}
} while(isDef(res.error) && c >= 0);
return res;
}
roJob.prototype.execJob = function(aoJob, aObj) {
var aURI = "/ojob?" + $rest().query(aObj);
return this.__req(aURI, this.getURL() + aURI, aoJob);
};
roJob.prototype.exec = function(aFile, aObj) {
var aURI = "/ojob?" + $rest().query(aObj);
return this.__req(aURI, this.getURL() + aURI, (aFile.endsWith(".yaml") ? io.readFileYAML(aFile) : io.readFile(aFile)));
};
roJob.prototype.execAsync = function(aFile, aObj) {
aObj = merge(aObj, { __async: 1 });
var aURI = "/ojob?" + $rest().query(aObj);
return this.__req(aURI, this.getURL() + aURI, (aFile.endsWith(".yaml") ? io.readFileYAML(aFile) : io.readFile(aFile)));
};
roJob.prototype.run = function(aFile, aObj) {
var aURI = "/run?" + $rest().query(aObj);
return this.__req(aURI, this.getURL() + aURI, aFile);
};
roJob.prototype.runAsync = function(aFile, aObj) {
aObj = merge(aObj, { __async: 1 });
var aURI = "/run?" + $rest().query(aObj);
return this.__req(aURI, this.getURL() + aURI, aFile);
};
/**
* <odoc>
* <key>roJob.listNodes() : Map</key>
* Returns a list of the existing nodes.
* </odoc>
*/
roJob.prototype.listNodes = function() {
return this.run("rojob/listnodes.yaml");
};
/**
* <odoc>
* <key>roJob.listWork(aPath) : Map</key>
* Returns a list of the files on the remote work folder on aPath.
* </odoc>
*/
roJob.prototype.listWork = function(aPath) {
return this.run("rojob/listwork.yaml", { path: aPath });
};
/**
* <odoc>
* <key>roJob.getMemory(notFormat) : Map</key>
* Returns a map with the java heap memory stats. Optionally if notFormat = true values will be returned in bytes instead of human readable.
* </odoc>
*/
roJob.prototype.getMemory = function(notFormat) {
return this.run("rojob/getmem.yaml", { format: !notFormat });
};
/**
* <odoc>
* <key>roJob.cleanWork(aPath) : Map</key>
* Cleans all files on the remote work folder under aPath.
* </odoc>
*/
roJob.prototype.cleanWork = function(aPath) {
return this.run("rojob/cleanwork.yaml", { path: aPath });
};
/**
* <odoc>
* <key>roJob.killJob(aJob) : Map</key>
* Tries to stop a remote aJob.
* </odoc>
*/
roJob.prototype.killJob = function(aJob) {
return this.run("rojob/killjob.yaml", { job: aJob });
};
/**
* <odoc>
* <key>roJob.getResult(aUUID, shouldClean) : Map</key>
* Returns the result of a async job, identified by aUUID. Optionally the result can be cleaned
* from the remote work folder.
* </odoc>
*/
roJob.prototype.getResult = function(aUUID, shouldClean) {
var res = this.run("rojob/getresult.yaml", { uuid: aUUID });
if (shouldClean && isUnDef(res.__error)) this.run("rojob/cleanresult.yaml", { uuid: aUUID });
return res;
};
roJob.prototype.setInitJob = function(aName, aFile) {
return this.run("rojob/setinitjob.yaml", { name: aName, job: io.readFileString(aFile) });
};
roJob.prototype.clearInitJobs = function() {
return this.run("rojob/clearinitjobs.yaml");
};
roJob.prototype.addNode = function(aHost, aPort) {
return this.run("rojob/addnode.yaml", { host: aHost, port: aPort });
};
roJob.prototype.showVars = function() {
return this.run("rojob/showvars.yaml");
};
roJob.prototype.stop = function() {
this.run("rojob/stop.yaml");
};
roJob.prototype.stopAll = function() {
this.run("rojob/stopall.yaml");
};