-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoscale.js
66 lines (53 loc) · 2.35 KB
/
autoscale.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
// Created by J. Eric Hartzog on 7/19/17
import phantomjs from 'phantomjs-prebuilt';
import webdriverio from 'webdriverio';
import scrapeInfo from './scrape-info';
import scalingLogic from './scaling-logic';
import isLoading from './is-loading';
import login from './login';
import log from './log';
runAutoScale = (opts) => {
phantomjs.run('--webdriver=4444').then(async program => {
let browser;
try {
log({ level: 'info', message: 'Running auto-scaler' });
// Load phantomjs to the Galaxy management page
const wdOpts = { desiredCapabilities: { browserName: 'phantomjs' } }
// For reason using await on this doesn't work, so we just pause after running it
browser = webdriverio.remote(wdOpts)
.init()
.url(`https://galaxy.meteor.com/app/${opts.appName}`);
await browser.pause(opts.stepDelayMs);
// Login to the account
await login(browser, opts);
// Wait until all loading is complete
await isLoading(browser, opts);
// Scrape the current status
const status = await scrapeInfo(browser);
log({ level: 'info', message: `Current status: ${JSON.stringify(status, null, 2)}` })
// Decide if any scaling action is needed
const scaleAction = scalingLogic(status, opts.scalingRules);
if (scaleAction > 0) {
// We should be able to use browser.click() for this, but it doesn't work
await browser.executeAsync((delay, done) => {
$('button.cardinal-action.increment').click();
setTimeout(() => done(), delay);
}, opts.stepDelayMs);
// console.log('Completed scaling up');
}
if (scaleAction < 0) {
await browser.executeAsync((delay, done) => {
$('button.cardinal-action.decrement').click();
setTimeout(() => done(), delay);
}, opts.stepDelayMs);
// console.log('Completed scaling down');
}
} catch (err) {
log({ level: 'error', message: err.toString() });
} finally {
// Make sure we kill phantomjs when we are done
program.kill();
}
})
};
export default runAutoScale;