thaw-carrots/index.js

57 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2018-10-11 07:19:15 +00:00
#!/usr/bin/env node
const si = require("systeminformation");
2018-10-11 06:19:42 +00:00
const mri = require("mri");
const sll = require("single-line-log").stdout;
const kleur = require("kleur");
const fork = require("child_process").fork;
const sleep = require("sleep");
async function readTemp() {
const temp = await si.cpuTemperature();
return { main: temp.main, cores: temp.cores };
}
function avg(arr) {
return arr.reduce((sum, t) => t.main + sum, 0) / arr.length;
}
2018-10-11 06:19:42 +00:00
async function thawMyCarrots(targetTemp) {
2018-10-11 06:52:54 +00:00
let temp = await readTemp(),
temps = [temp, temp, temp],
children = [];
2018-10-11 06:52:54 +00:00
while (1) {
if (avg(temps) < targetTemp) {
if (children.length < 10) {
children.push(fork("./eat-cpu"));
}
} else if (avg(temps) > targetTemp) {
if (children.length > 0) {
children.pop().kill();
}
2018-10-11 06:52:54 +00:00
}
temp = await readTemp();
temps.shift();
temps.push(temp);
2018-10-11 06:52:54 +00:00
sll(
"Current temp",
kleur.bold.red(temp.main.toFixed(2)),
"\nCores at",
kleur.bold.gray(temp.cores),
`\nChildren: ${children.length}`
2018-10-11 06:52:54 +00:00
);
sleep.msleep(500);
}
}
2018-10-11 06:19:42 +00:00
const args = process.argv.slice(2),
targetTemp = mri(args, {
alias: { temp: "t" },
default: { temp: 85 }
});
thawMyCarrots(targetTemp.temp);