| 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 |
1x
1x
1x
1x
1x
152x
152x
7x
30x
7x
23x
10x
13x
12x
1x
145x
145x
162x
162x
3x
3x
2x
160x
163x
163x
163x
86x
163x
143x
5x
5x
1x
5x
4x
4x
4x
4x
4x
1x
4x
3x
1x
2x
5x
1x
5x
5x
1732x
1732x
11565x
11565x
695x
695x
351x
1732x
1730x
1730x
1730x
8519x
605x
7914x
189x
189x
927x
922x
5x
189x
1730x
2x
| "use strict";
var http = require("http");
var pathReq = require("path");
var _ = require('lodash');
var winston = require('winston');
module.exports = {
getLogger: function(path, fileName, logLevel) {
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ level: logLevel || "info" }),
new (require("winston-daily-rotate-file"))({
filename: pathReq.join(__dirname, "../", (path && fileName ? path + "/" + fileName : "logs/mesos-framework.log")),
level: logLevel || "info",
prepend: true,
json: false
})
]
});
return logger;
},
cloneDeep: function(obj) {
return _.cloneDeep(obj);
},
sortTasksByPriority: function (tasks) {
function prioSort(a,b) {
if (a.priority < b.priority) {
return -1;
}
if (a.priority > b.priority) {
return 1;
}
if (a.name < b.name) {
return -1;
}
return 1;
}
var tasksArray = [];
Object.getOwnPropertyNames(tasks).forEach(function (task) {
var instances = tasks[task].instances || 1;
if (tasks[task].resources && tasks[task].resources.staticPorts) {
tasks[task].resources.staticPorts.sort();
if (!tasks[task].resources.ports || tasks[task].resources.staticPorts.length > tasks[task].resources.ports) {
throw new Error("Static ports must be included in the general port count for the task.");
}
}
// Add to tasks array
for (var i = 1; i <= instances; i++) {
// Set defaults
tasks[task].isSubmitted = false;
tasks[task].name = task + "-" + i.toString();
if (!tasks[task].hasOwnProperty("allowScaling")) {
tasks[task].allowScaling = false;
}
tasksArray.push(_.cloneDeep(tasks[task])); // Important!
}
});
return tasksArray.sort(prioSort);
},
doRequest: function (payload, callback) {
var self = this;
// Add mesos-stream-id to header
if (self.mesosStreamId) {
self.requestTemplate.headers["mesos-stream-id"] = self.mesosStreamId;
}
var req = http.request(self.requestTemplate, function (res) {
// Set encoding
res.setEncoding('utf8');
// Buffer for the response body
var body = "";
res.on('data', function (chunk) {
body += chunk;
});
// Watch for errors of the response
res.on('error', function (e) {
callback({ message: "There was a problem with the response: " + e.message }, null);
});
res.on('end', function () {
if (res.statusCode !== 202) {
callback({ message: "Request was not accepted properly. Reponse status code was '" + res.statusCode + "'. Body was '" + body + "'." }, null);
} else {
callback(null, { statusCode: res.statusCode, body: body });
}
});
});
// Watch for errors of the request
req.on('error', function (e) {
callback({ message: "There was a problem with the request: " + e.message }, null);
});
// Write data to request body
req.write(JSON.stringify(payload));
// End request
req.end();
},
stringifyEnums: function (message) {
message = _.clone(message); // We should not modify the source message in place, it causes issues with repeating calls
_.forEach(message.$type.children, function(child) {
var type = _.get(child, 'element.resolvedType', null);
if (type && type.className === 'Enum' && type.children) {
var metaValue = _.find(type.children, {
id: message[child.name]
});
if (metaValue && metaValue.name)
// Alternatively you can do something like:
// message[child.name + '_string'] = metaValue.name;
// To get access to both the raw value and the string.
message[child.name] = metaValue.name;
}
});
return message;
},
fixEnums: function (message) {
var self = this;
var newMessage = self.stringifyEnums(message);
_.forEach(message, function(subMessage, key) {
if (_.isObject(subMessage) && subMessage.$type) {
newMessage[key] = self.fixEnums(message[key]);
} else if (_.isArray(subMessage) && subMessage.length > 0) {
var arrayItems = [];
var index;
for (index = 0; index < subMessage.length; index += 1) {
if (_.isObject(subMessage[index]) && subMessage[index].$type) {
arrayItems.push(self.fixEnums(subMessage[index]));
} else {
arrayItems.push(subMessage[index]);
}
}
newMessage[key] = arrayItems;
}
});
return newMessage;
},
isFunction: function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
}
}; |