Skip to content

Commit

Permalink
Can use 'GB', 'MB', etc. for mem usage along with %s
Browse files Browse the repository at this point in the history
  • Loading branch information
KentoNishi authored Apr 1, 2021
1 parent 146f02e commit 9ef7bd9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
LIVETL_API_KEY= # livetl api key
CONTROLLER_URL= # controller address
WATCHER_IMAGE=ghcr.io/livetl/watcher # watcher image
CHROME_REFRESH=10 # refresh interval (should be 10)
API_URL=https://api.livetl.app # api url
MAX_CPU_PERCENT=100 # max cpu usage at which container can start
MAX_MEM_PERCENT=100 # max mem usage at which container can start
MAX_CPU=100% # max cpu usage at which container can start. (ex. 69%)
MAX_MEM=100% # max mem usage at which container can start. can be either % or standard file size notation (ex. 69%, 420MB, 21GB)
```
1. Run the pre-built image
```bash
Expand Down
5 changes: 5 additions & 0 deletions runner/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"main": "index.js",
"dependencies": {
"bytes": "^3.1.0",
"dockerstats": "^2.2.0",
"node-docker-monitor": "^1.0.11",
"tar-fs": "^2.1.1",
Expand Down
29 changes: 19 additions & 10 deletions runner/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,51 @@ const dockerstats = require('dockerstats');
const ENDPOINT = process.env.CONTROLLER_URL || 'ws://localhost:8000';
const WebSocket = require('ws');
const monitor = require('node-docker-monitor');
const bytes = require('bytes');
const { exit } = require('process');
let ws = null;
const IMAGE_NAME = process.env.WATCHER_IMAGE || 'watcher';
const LIVETL_API_KEY = process.env.LIVETL_API_KEY || 'KEY_WAS_BAD';
const API_URL = process.env.API_URL || 'https://api.livetl.app';
let initDone = false;
let MAX_USAGES = {
cpuPercent: process.env.MAX_CPU_PERCENT || 50,
memPercent: process.env.MAX_MEM_PERCENT || 50
};
let MAX_MEM_VAR = process.env.MAX_MEM || '100';
const MEM_IS_BYTES = MAX_MEM_VAR.toLowerCase().endsWith('b');
if (MEM_IS_BYTES) MAX_MEM_VAR = bytes(MAX_MEM_VAR);
else MAX_MEM_VAR = parseInt(MAX_MEM_VAR);
const MAX_CPU = parseInt(process.env.MAX_CPU || '') || 100;

const log = console.log;
console.log = (...args) => log(new Date(), ...args);

let playing = {};
let shutdown = false;

const round = num => Math.round(num * 10000) / 100;

setInterval(statsGetter, 1000);
async function statsGetter() {
if (ws && initDone) {
const stats = await dockerstats.dockerContainerStats();
let usages = {
memPercent: 0,
totalMem: 0,
cpuPercent: 0
};
stats.forEach(container => {
usages.memPercent += container.memPercent;
usages.totalMem += container.memUsage;
usages.cpuPercent += container.cpuPercent;
});
let relativeLoad = 0.0;
Object.keys(usages).forEach(metric => {
usages[metric] /= MAX_USAGES[metric];
relativeLoad = Math.max(Math.round(usages[metric] * 10000) / 100, relativeLoad);
});
if (MEM_IS_BYTES) usages.memPercent = usages.totalMem / MAX_MEM_VAR;
else usages.memPercent /= MAX_MEM_VAR;
usages.memPercent = round(usages.memPercent / 100);
usages.cpuPercent = round(usages.cpuPercent / MAX_CPU);
let relativeLoad = Math.max(
usages.memPercent,
usages.cpuPercent
);
ws.send(JSON.stringify({
event: 'usage',
relativeLoads: usages,
relativeLoad
}));
}
Expand Down Expand Up @@ -76,6 +84,7 @@ let dockerMonitor = null;
function connect () {
ws = new WebSocket(ENDPOINT, clientVersion);
ws.on('open', () => {
initDone = false;
console.log('Runner is active!');

if (!dockerMonitor) monitor({
Expand Down

0 comments on commit 9ef7bd9

Please sign in to comment.