Skip to content

Commit

Permalink
wip: handling cache limitations
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomontero committed Jan 29, 2025
1 parent 5795d84 commit 507c54e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
3 changes: 2 additions & 1 deletion settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ let settings = {
'tinker': true
},

tachyonMeta: 'https://tachyon-ci.particle.io/meta'
tachyonMeta: 'https://tachyon-ci.particle.io/meta',
tachyonCacheLimit: 10
};

function envValue(varName, defaultValue) {
Expand Down
72 changes: 61 additions & 11 deletions src/lib/download-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,61 @@ class DownloadManager {
}
}

async _validateCacheLimit({ url, maxCacheSize = settings.tachyonCacheLimit, currentFileName }) {
const fileHeaders = await fetch(url, { method: 'HEAD' });
const contentLength = parseInt(fileHeaders.headers.get('content-length') || '0', 10);
// get the size of the download directory
const downloadDirStats = await this.getDownloadedFilesStats(currentFileName);
const totalSize = downloadDirStats.totalSize + contentLength;
if (totalSize > maxCacheSize) {
// prompt to delete files
const question = {
type: 'confirm',
name: 'cleanCache',
message: 'Downloaded files exceed the cache limit. Do you want to clean up the cache?',
default: true
};
const answer = await this.ui.prompt(question);
if (answer.cleanCache) {
const files = downloadDirStats.fileStats;
for (const file of files) {
await fs.remove(file.filePath);
this.ui.write(`Removed file: ${file.filePath}`);
const { totalSize } = await this.getDownloadedFilesStats(currentFileName);
if (totalSize <= maxCacheSize) {
return;
}
}
} else {
// remove files until the cache limit is satisfied
this.ui.write('Cache cleanup skipped. Remove files manually to free up space.');
return;
}
}
return;
}

async getDownloadedFilesStats(currentFile) {
const files = await fs.readdir(this.downloadDir);
// filter out the current file in progress
const filteredFiles = currentFile ? files.filter(file => file !== currentFile || file !== `${currentFile}.progress`) : files;
// sort files by date modified
const sortedFileStats = filteredFiles.sort((a, b) => {
const aStats = fs.statSync(path.join(this.downloadDir, a));
const bStats = fs.statSync(path.join(this.downloadDir, b));
return aStats.mtime - bStats.mtime;
});
const fileStats = await Promise.all(sortedFileStats.map(async (file) => {
const filePath = path.join(this.downloadDir, file);
const stats = await fs.stat(filePath);
return { filePath, size: stats.size };
}));
return {
totalSize: fileStats.reduce((sum, file) => sum + file.size, 0),
fileStats
};
}

async _attemptDownload(url, outputFileName, progressFilePath, finalFilePath, timeout) {
const progressBar = this.ui.createProgressBar();
let downloadedBytes = 0;
Expand Down Expand Up @@ -196,23 +251,18 @@ class DownloadManager {

}

async cleanup({ fileName, cleanInProgress = false, cleanDownload = true } = {}) {
async cleanup({ fileName, cleanAll = false, downloadDirStats }= {}) {
try {
if (fileName) {
// Remove specific file and its progress file
await fs.remove(path.join(this.downloadDir, fileName));
await fs.remove(path.join(this.downloadDir, `${fileName}.progress`));
} else if (cleanInProgress) {
// Remove all in-progress files
const files = (await fs.readdir(this.downloadDir)).filter(file => file.endsWith('.progress'));
await Promise.all(files.map(file => fs.remove(path.join(this.downloadDir, file))));
files.forEach(file => this.ui.write(`Removed in-progress file: ${file}`));
if (cleanDownload) {
await fs.remove(this.downloadDir);
} else {
if (cleanAll) {

} else {

}
} else if (cleanDownload) {
// Remove the entire download directory
await fs.remove(this.downloadDir);
}
} catch (error) {
this.ui.error(`Error cleaning up directory: ${error.message}`);
Expand Down

0 comments on commit 507c54e

Please sign in to comment.