forked from appium/appium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid_register.js
96 lines (83 loc) · 3.07 KB
/
grid_register.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
"use strict";
var request = require('request')
, fs = require('fs')
, logger = require('./logger').get('appium');
exports.registerNode = function (configFile) {
fs.readFile(configFile, 'utf-8', function (err, data) {
if (err) {
logger.error("Unable to load node configuration file to register with grid");
return;
}
// Check presence of data before posting it to the selenium grid
if (data) {
postRequest(data);
} else {
logger.error("No data found in the node configuration file to send to the grid");
}
});
};
function registerToGrid(options_post, jsonObject) {
var reqPost = request(options_post, function(error, response, body) {
if (error !== null || response.statusCode !== 200) {
logger.error("Request to register with grid was Unsuccessful...");
} else {
var logMessage = "Appium successfully registered with the grid on " + jsonObject.configuration.hubHost + ":" + jsonObject.configuration.hubPort;
logger.info(logMessage.cyan);
}
});
}
function postRequest(data) {
//parse json to get hub host and port
var jsonObject = JSON.parse(data);
// prepare the header
var post_headers = {
'Content-Type' : 'application/json'
, 'Content-Length': data.length
};
// the post options
var options_post = {
url : 'http://' + jsonObject.configuration.hubHost + ':' + jsonObject.configuration.hubPort + '/grid/register'
, method : 'POST'
, body : data
, headers : post_headers
};
if (jsonObject.configuration.register !== true) {
logger.info("no registration sent ( " + jsonObject.configuration.register + " = false )");
} else {
var registerCycleTime = jsonObject.configuration.registerCycle;
if (registerCycleTime !== null && registerCycleTime > 0) {
//initiate a new Thread
var first = true;
logger.info("starting auto register thread for grid. Will try to register every " + registerCycleTime + " ms.");
setInterval(function() {
if (first !== true) {
isAlreadyRegistered(jsonObject, function(isRegistered) {
if (isRegistered !== null && isRegistered !== true) {
// make the http POST to the grid for registration
registerToGrid(options_post, jsonObject);
}
});
} else {
first = false;
registerToGrid(options_post, jsonObject);
}
}, registerCycleTime);
}
}
}
function isAlreadyRegistered(jsonObject, cb) {
//check if node is already registered
var id = "http://" + jsonObject.configuration.host + ":" + jsonObject.configuration.port;
request ({
uri : 'http://' + jsonObject.configuration.hubHost + ':' + jsonObject.configuration.hubPort + '/grid/api/proxy'+ "?id=" + id
, method : "GET"
, timeout : 10000
}, function(error, response, body) {
if (error !== null || response === undefined || response.statusCode !== 200) {
logger.info("hub down or not responding.");
return cb(null);
}
var responseData = JSON.parse(response.body);
return cb(responseData.success);
});
}