add disk usage task
This commit is contained in:
+38
-1
@@ -5,7 +5,8 @@ exports = module.exports = {
|
||||
checkDiskSpace,
|
||||
getMemory,
|
||||
getMemoryAllocation,
|
||||
du
|
||||
getDiskUsage,
|
||||
updateDiskUsage
|
||||
};
|
||||
|
||||
const apps = require('./apps.js'),
|
||||
@@ -145,3 +146,39 @@ function getMemoryAllocation(limit) {
|
||||
|
||||
return Math.round(Math.round(limit * ratio) / 1048576) * 1048576; // nearest MB
|
||||
}
|
||||
|
||||
function getDiskUsage() {
|
||||
const output = safe.JSON.parse(safe.fs.readFileSync(paths.DISK_USAGE_FILE, 'utf8'));
|
||||
return output;
|
||||
}
|
||||
|
||||
async function updateDiskUsage(progressCallback) {
|
||||
assert.strictEqual(progressCallback, 'function');
|
||||
|
||||
const disks = await getDisks();
|
||||
const filesystems = Object.keys(disks);
|
||||
const now = Date.now();
|
||||
|
||||
let percent = 1;
|
||||
|
||||
for (const filesystem of filesystems) {
|
||||
const disk = disks[filesystem];
|
||||
|
||||
percent += (100/filesystems.length);
|
||||
progressCallback({ percent, message: `Checking contents of ${filesystem}`});
|
||||
|
||||
for (const content of disk.contents) {
|
||||
progressCallback({ message: `Checking du of ${JSON.stringify(content)}`});
|
||||
if (content.type === 'docker') {
|
||||
content.usage = (await docker.du()).LayersSize;
|
||||
} else {
|
||||
content.usage = await du(content.path, { exclude: content.exclude });
|
||||
}
|
||||
progressCallback({ message: `du of ${JSON.stringify(content)}: ${content.usage}`});
|
||||
}
|
||||
}
|
||||
|
||||
if (!safe.fs.writeFileSync(paths.DISK_USAGE_FILE, JSON.stringify({ ts: now, disks }), 'utf8')) throw new BoxError(BoxError.FS_ERROR, `Could not write du cache file: ${safe.error.message}`);
|
||||
|
||||
return disks;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user