-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathServices.js
378 lines (342 loc) · 11.9 KB
/
Services.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
var Orchestrator = require('uipath-orchestrator');
function Services(settings) {
this.settings = settings;
this.settings.queues = {};
this.settings.processes = {};
this.orchestrator = new Orchestrator(settings.connection);
}
function odataEscape(str) {
return str.replace(/'/g, "''");
}
Services.prototype.getJobDetails = function(jobId) {
return new Promise(function (fulfill, reject){
this.orchestrator.v2.odata.getJob(jobId, {}, function(err, data) {
if (err) {
reject(err);
} else {
fulfill(data);
}
});
}.bind(this));
}
// updates the service object with the number of jobs currently running
Services.prototype.getRunningJobs = function(service) {
return new Promise(function (fulfill, reject){
this.orchestrator.v2.odata.getJobs({"$filter": "ReleaseName eq '" + odataEscape(service.processName) + "_" + odataEscape(service.environmentName) + "' and (State eq 'Pending' or State eq 'Running')", "$top": 0, "$count": "true"}, function(err, data) {
if (err) {
reject(err);
} else {
try {
service.count = data["@odata.count"];
fulfill();
} catch(err) {
reject("Malformed response: Cannot get jobs count");
}
}
});
}.bind(this));
}
// updates the service object with the process key, needed for further API calls
Services.prototype.getProcessKey = function(processName, environmentName) {
return new Promise(function (fulfill, reject){
if (this.settings.processes[processName + "_" + environmentName]) {
fulfill();
return;
}
this.orchestrator.v2.odata.getReleases({"$filter": "ProcessKey eq '" + odataEscape(processName) + "' and EnvironmentName eq '" + odataEscape(environmentName) + "'"}, function(err, data) {
if (err) {
reject(err);
} else {
try {
this.settings.processes[processName + "_" + environmentName] = data.value[0].Key;
fulfill();
} catch(err) {
reject("Malformed response: Cannot get process key for " + processName +" on " + environmentName + ": " + err);
}
}
}.bind(this));
}.bind(this));
}
// updates the service object with the queue Id
Services.prototype.getQueueId = function(queueName) {
return new Promise(function (fulfill, reject){
this.orchestrator.v2.odata.getQueueDefinitions({"$filter": "Name eq '" + odataEscape(queueName) + "'"}, function(err, data) {
if (err) {
reject(err);
} else {
try {
this.settings.queues[queueName] = data.value[0].Id;
fulfill();
} catch(err) {
reject("Malformed response: Cannot get queue dewfinition for " + queueName);
}
}
}.bind(this));
}.bind(this));
}
// gets number of running jobs and process keys for all services
Services.prototype.getProcessDetails = function() {
return new Promise(function (fulfill, reject){
var servicesPromises = [];
this.settings.services.forEach(function(service) {
servicesPromises.push(this.getRunningJobs(service));
if (!this.settings.processes[service.processName + "_" + service.environmentName]) {
servicesPromises.push(this.getProcessKey(service.processName, service.environmentName));
}
if (!this.settings.queues[service.queueName]) {
servicesPromises.push(this.getQueueId(service.queueName));
}
}.bind(this));
this.settings.processRetries.forEach(function(process) {
servicesPromises.push(this.getProcessKey(process.processName, process.environmentName));
}.bind(this));
this.settings.processLinks.forEach(function(processLink) {
processLink.output.forEach(function(linkOutput) {
servicesPromises.push(this.getProcessKey(linkOutput.processName, linkOutput.environmentName));
}.bind(this));
servicesPromises.push(this.getProcessKey(processLink.input.processName, processLink.input.environmentName));
}.bind(this));
Promise.all(servicesPromises)
.then(function() {
console.log("Got all current running jobs details");
fulfill();
})
.catch(reject);
}.bind(this));
}
Services.prototype.getQueueDetails = function() {
return new Promise(function (fulfill, reject){
var queuePromises = [];
this.settings.queueLinks.forEach(function(queueLink) {
queueLink.input.forEach(function(queueName) {
if (!this.settings.queues[queueName]) {
queuePromises.push(this.getQueueId(queueName));
}
}.bind(this));
queueLink.output.forEach(function(queueName) {
if (!this.settings.queues[queueName]) {
queuePromises.push(this.getQueueId(queueName));
}
}.bind(this));
}.bind(this));
Promise.all(queuePromises)
.then(function() {
console.log("Got all current queue details");
fulfill();
})
.catch(reject);
}.bind(this));
}
// Start processing for a service.
// This will try to strat as many jobs as possible to process asap the items in the corresponding queue
Services.prototype.startProcessing = function(service) {
return new Promise(function (fulfill, reject){
console.log(service.processName + ": " + service.count + " jobs running");
console.log(service.processName + ": " + service.maxRobots + " max jobs");
if (service.count >= service.maxRobots) {
// no need to start new jobs, they are already running
fulfill();
}
// get the number of items to be processed in this queue
this.orchestrator.v2.odata.getRetrieveQueuesProcessingStatus({"$filter": "QueueDefinitionName eq '" + service.queueName + "'"}, function(err, data) {
if (err) {
reject(err);
} else {
try {
var itemsToProcess = data.value[0].ItemsToProcess;
console.log(service.queueName + ": " + itemsToProcess + " items to process");
var newJobsCount = Math.min(itemsToProcess, service.maxRobots - service.count);
console.log(service.processName + ": " + newJobsCount + " jobs to start");
if (newJobsCount > 0) {
this.startJobForQueue(this.settings.queues[service.queueName], newJobsCount);
}
fulfill();
} catch(err) {
reject("Malformed response: Cannot get Queue size");
}
}
}.bind(this));
fulfill();
}.bind(this));
}
// starts processing jobs for all services
Services.prototype.startProcessingJobs = function() {
return new Promise(function (fulfill, reject){
var servicesPromises = [];
this.settings.services.forEach(function(service) {
servicesPromises.push(this.startProcessing(service));
}.bind(this));
Promise.all(servicesPromises)
.then(function() {
fulfill();
})
.catch(reject);
}.bind(this));
}
Services.prototype.startJob = function(jobName, environmentName, runs, inputArgs) {
console.log("Starting " + jobName + " on " + environmentName + " " + runs + " time(s) with " + JSON.stringify(inputArgs));
this.getProcessKey(jobName, environmentName).then(function() {
jobParams = {
"startInfo": {
"ReleaseKey": this.settings.processes[jobName + "_" + environmentName],
"Strategy": "JobsCount",
"JobsCount": runs,
"Source": "Schedule",
"InputArguments": JSON.stringify(inputArgs)
}
};
this.orchestrator.post("/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs", jobParams, function(err, data){
if (err) {
console.log(err);
}
});
}.bind(this)).catch(function(e) {
console.log(e);
});
};
// queues a number of jobs for a specific process corresponding to a queue id
Services.prototype.startJobForQueue = function(queueId, runs) {
var key = '';
var shouldRun = false;
var service = this.settings.services.find(function(service) {
return this.settings.queues[service.queueName] == queueId;
}.bind(this));
if (service) {
if (service.count + 1 > service.maxRobots) {
console.log(service.queueName + ": max robots reached");
return;
}
this.startJob(service.processName, service.environmentName, runs, {});
}
}
Services.prototype.onJobFinished = function(job) {
var jobName;
if (!job) return;
Object.keys(this.settings.processes).forEach(function(processName) {
if (this.settings.processes[processName] == job.Release.Key) {
jobName = processName;
}
}.bind(this));
var service = this.settings.services.find(function(service) {
return jobName == service.processName + "_" + service.environmentName;
});
if (service) {
service.count--;
console.log(jobName + ": is running " + service.count + " time(s)");
}
var processRetry = this.settings.processRetries.find(function(process) {
return jobName == process.processName + "_" + process.environmentName;
});
switch (job.State) {
case "Successful":
if (processRetry) {
processRetry.failCount = 0;
}
var processLink = this.settings.processLinks.find(function(processLink) {
return jobName == processLink.input.processName + "_" + processLink.input.environmentName;
});
if (processLink) {
processLink.output.forEach(function(outputLink) {
this.startJob(outputLink.processName, outputLink.environmentName, 1, job.OutputArguments);
}.bind(this));
}
break;
case "Faulted":
if (processRetry) {
if (!processRetry.failCount) {
processRetry.failCount = 0;
}
processRetry.failCount++;
if (processRetry.failCount <= processRetry.retries) {
this.getJobDetails(job.Id).then(function(job) {
console.log("Process execution failed for " + processRetry.processName + " on " + processRetry.environmentName + ". Retrying...");
this.startJob(processRetry.processName, processRetry.environmentName, 1, JSON.parse(job.InputArguments));
}.bind(this));
} else {
console.log("Retry count exceded for " + processRetry.processName + " on " + processRetry.environmentName);
}
}
break;
}
}
Services.prototype.onJobCreated = function(jobName) {
var service = this.settings.services.find(function(service) {
return jobName == service.processName + "_" + service.environmentName;
});
if (service) {
service.count++;
console.log(jobName + ": is running " + service.count + " time(s)");
}
}
function arrayContainsArray (superset, subset) {
if (0 === subset.length) {
return false;
}
return subset.every(function (value) {
return (superset.indexOf(value) >= 0);
});
}
Services.prototype.checkQueueLinks = function(queue) {
var partOfALink = false;
var queueLinks = [];
this.settings.queueLinks.forEach(function(queueLink) {
queueLink.input.forEach(function(queueName) {
if (queue.QueueDefinitionId == this.settings.queues[queueName]) {
queueLinks.push(queueLink);
partOfALink = true;
}
}.bind(this));
}.bind(this));
if (partOfALink) {
this.orchestrator.v2.odata.getQueueItems({"$filter": "Reference eq '" + queue.Reference + "' and Status eq 'Successful'"}, function(err, data) {
if (err) {
console.log(err);
} else {
try {
var queueIDs = {};
var queueNames = [];
var queueOutput = {};
data.value.forEach(function(queueItem) {
if (!queueOutput[queueItem.QueueDefinitionId]) {
queueIDs[queueItem.QueueDefinitionId] = true;
queueOutput = Object.assign(queueOutput, queueItem.Output);
}
}.bind(this));
Object.keys(this.settings.queues).forEach(function(queueName) {
if (queueIDs[this.settings.queues[queueName]]) {
queueNames.push(queueName);
}
}.bind(this));
queueLinks.forEach(function(queueLink) {
if (arrayContainsArray(queueNames, queueLink.input)) {
// all items were processed, create new queue item
queueLink.output.forEach(function(queueName) {
var newQueueItem = {
"itemData": {
"Name": queueName,
"SpecificContent": queueOutput,
"Reference": queue.Reference
}
};
this.orchestrator.v2.odata.postAddQueueItem(newQueueItem, function(err, data) {
if (err) {
if (data && data.message) {
console.log(data.message);
return;
}
console.log(err);
return;
}
console.log("Queue Link matched, created new queue item");
});
}.bind(this));
}
}.bind(this));
} catch(err) {
console.log("Malformed response: Cannot get queue items by reference: " + err);
}
}
}.bind(this));
}
}
module.exports = Services;