-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoscaler.js
160 lines (127 loc) · 4.18 KB
/
autoscaler.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
var url = require('url');
var querystring = require('querystring');
var https = require('https');
var _ = require('underscore');
//heroku log api call
var applicationName = 'APP_NAME';
var apiKey = 'API_KEY';
//count requests for the last n milliseconds
var periods = 60;
var interval = 1000;
//how many requests per server - CHANGE THESE VARIABLES
var requestsPerServer = 750 * 60;
var minServers = 1;
var servers = 1;
var threshold = 0.2;
//attaches to the heroku logging service
function consume (data) {
var address = url.parse('' + data);
var buffer = '';
var total = 0;
var last = 0;
var queue = [];
var dynos = {};
var options = {
host: address.host,
path: address.path
};
//consumes data from the heroku logging service
var responseHandler = function (res) {
res.on('data', function(chunk) {
var lines = ('' + chunk).split('\n');
lines.forEach(function(line) {
var dyno = /(web\.[\d]+)/ig.exec(line);
if(dyno) {
dyno = dyno[0];
if(!dynos[dyno]) dynos[dyno] = { total: 0, errors: 0 };
dynos[dyno].total ++;
if(line.indexOf('error') > -1) dynos[dyno].errors ++;
total++;
}
});
});
res.on('end', function() {
console.log('END');
});
};
//counts the total number of active users in the specified time period
var counter = function () {
var current = total - last;
//clear console
console.log('\033[2J');
queue.push(current);
if(queue.length > periods) queue = queue.slice(1);
var sum = _.reduce(queue, function(memo, num) { return memo + num; }, 0);
console.log(servers + ' dynos');
console.log(current + ' reqs/s');
console.log(sum + ' active users');
console.log('-----------------------')
Object.keys(dynos).forEach(function(key) {
console.log(key + ': ' + dynos[key].total + ' / ' + dynos[key].errors);
});
var serversRequired = Math.max(minServers, sum / requestsPerServer);
if(parseInt(serversRequired) > servers && serversRequired > servers * (1+threshold)) {
servers = parseInt(serversRequired);
console.log('increasing active servers to ' + servers);
scale(servers);
}
if(parseInt(serversRequired) < servers && serversRequired < servers / (1+threshold)) {
servers = parseInt(serversRequired);
console.log('decreasing active servers to ' + servers);
scale(servers);
}
last = total;
};
var req = https.request(options, responseHandler);
req.write('');
req.end();
setInterval(counter, interval);
}
//scales the number of servers automatically
function scale (servers) {
var data = querystring.stringify({
type: 'web',
qty: servers
});
var headers = {
Accept: "application/json",
Authorization: new Buffer(':' + apiKey).toString('base64'),
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
};
var options = {
headers: headers,
hostname: 'api.heroku.com',
path: '/apps/' + applicationName + '/ps/scale',
method: 'POST'
};
var req = https.request(options, function (res) {
res.on('data', function(data) {
console.log('' + data + ' dynos running');
});
});
req.write(data);
req.end();
}
//starts up the autoscaler service
function start () {
scale(minServers);
var headers = {
Accept: "application/json",
Authorization: new Buffer(':' + apiKey).toString('base64')
};
var options = {
headers: headers,
hostname: 'api.heroku.com',
path: '/apps/' + applicationName + '/logs?logplex=true&ps=router&tail=1&num=0',
method: 'GET'
};
var req = https.request(options, function (res) {
res.on('data', function(url) {
consume(url);
});
});
req.end();
}
//and now start the autoscaler application
start();