-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (70 loc) · 2.42 KB
/
index.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
var request = require('request');
/**
* Checks if a server is alive
*
* @param serverURL the url of the knowhow server to check.
*/
var serverHeartbeat = function(callback) {
request.get(this.serverURL+'/api/serverInfo' ,
function (error, response, body) {
if (error || (response && response.statusCode != 200)) {
if (!error) {
callback(new Error("response: "+response.statusCode+" "+body));
} else {
callback(error);
}
} else {
callback(undefined, {alive: true});
}
}
);
};
var waitForServerStartup = function(callback) {
//timeout after 40 secs
var timeout = setTimeout(function() {
clearInterval(heartbeatCheck);
callback(new Error("server did not start"));
}, 40000);
//wait until a heartbeat is received
var heartbeatCheck = setInterval(function() {
var heartbeat2 = serverHeartbeat;
//console.log(this.agent);
heartbeat2(function (err) {
if (!err) {
clearTimeout(timeout);
clearInterval(heartbeatCheck);
callback();
} else {
console.log(err.message);
//callback(new Error("server did not start"));
}
});
}.bind({serverURL: this.serverURL, callback: callback}), 500);
};
/**
* Factory method for a KHClieent
* This is a programatic representation of a knowhow server. It provides the same functionality that the knowhow
* server UI does.
*
* @param serverURL - the knwohow server being manipulated
*/
function KHClient(serverURL) {
var self = this;
console.log(serverURL);
self.serverURL = serverURL;
self.khEventHandler = new require('./kh-api/kh-events-handler')(serverURL);
self.khRepository = require('./kh-api/kh-repository')(serverURL, self.khEventHandler);
self.khJob = require('./kh-api/kh-job')(serverURL, self.khEventHandler,self);
self.khAgent = new require('./kh-api/kh-agent')(serverURL, self.khEventHandler);
self.khWorkflow = require('./kh-api/kh-workflow')(serverURL, self.khEventHandler,self);
self.serverHeartbeat = serverHeartbeat.bind({serverURL: serverURL});
self.waitForServerStartup = waitForServerStartup.bind({serverURL: serverURL});
self.end = function() {
console.log("ending KH client to: "+serverURL);
self.khEventHandler.removeAllListeners();
self.khEventHandler.close();
self.khEventHandler.disconnect();
}
return self;
}
module.exports = KHClient;