rewrite the cache control script

This commit is contained in:
teddit 2021-11-26 21:33:12 +01:00
parent 660e5681be
commit d895e400ef
2 changed files with 21 additions and 60 deletions

View File

@ -1,66 +1,28 @@
module.exports.removeCacheFiles = function() { module.exports.removeCacheFiles = function() {
const fs = require('fs') const config = require('./config');
const config = require('./config')
const pics = './static/pics' async function deleteStatic() {
const flairs = './static/pics/flairs' const fs = require('fs');
const icons = './static/pics/icons' const pics = './static/pics/';
const thumbs = './static/pics/thumbs' const vids = './static/vids/';
const vids = './static/vids'
let util = require('util') fs.rmdir(pics, { recursive: true, force: true }, () => {
let spawn = require('child_process').spawn fs.rmdir(vids, { recursive: true, force: true }, () => {
['pics/thumbs', 'pics/flairs', 'pics/icons', 'vids'].map((d) => `./static/${d}`)
let usage .filter((d) => !fs.existsSync(d))
const limit = config.cache_max_size .forEach((d) => fs.mkdirSync(d, { recursive: true }));
function getUsage() { console.log('Cleared cached static media files. You can turn this off by setting the config.cache_control to false.');
return new Promise((resolve, reject) => { });
let size = spawn('du', ['-sBM', './static/']) });
size.stdout.on('data', function (data) {
data = data.toString()
let lines = data.split('\n')
if(lines) {
for(let i = lines.length; i >= 0; i--) {
if(lines[i] && lines[i].includes('./static/')) {
usage = parseInt(lines[i])
}
}
}
resolve(usage)
})
})
} }
function deleteFiles() { if(config.cache_control) {
return new Promise(async (resolve, reject) => { deleteStatic();
usage = await getUsage()
if(usage > limit) {
const { exec } = require('child_process')
exec(`cd ${pics} && ls -1btr -Iflairs -Iicons -Ithumbs -I.gitignore | head -50 | xargs rm -f --`)
exec(`cd ${flairs} && ls -1btr -I.gitignore | head -6 | xargs rm -f --`)
exec(`cd ${icons} && ls -1btr -I.gitignore | head -6 | xargs rm -f --`)
exec(`cd ${thumbs} && ls -1btr -I.gitignore | head -80 | xargs rm -f --`)
exec(`cd ${vids} && ls -1btr -I.gitignore | head -2 | xargs rm -f --`)
}
resolve(1)
})
}
async function main() {
usage = await getUsage()
if(usage > limit) {
console.log('Started removeCacheFiles()')
while(usage > limit) {
await deleteFiles()
}
}
}
if(config.cache_control) {
main()
const interval_ms = config.cache_control_interval const interval_ms = config.cache_control_interval
setInterval(() => { setInterval(() => {
main() deleteStatic();
}, interval_ms) }, interval_ms);
} }
} }

View File

@ -33,8 +33,7 @@ const config = {
? (JSON.parse(process.env.DOMAIN_REPLACEMENTS).map(([p, r]) => [new RegExp(p, 'gm'), r])) ? (JSON.parse(process.env.DOMAIN_REPLACEMENTS).map(([p, r]) => [new RegExp(p, 'gm'), r]))
: [], // Replacements for domains in outgoing links. Tuples with regular expressions to match, and replacement values. This is in addition to user-level configuration of privacyDomains. : [], // Replacements for domains in outgoing links. Tuples with regular expressions to match, and replacement values. This is in addition to user-level configuration of privacyDomains.
cache_control: process.env.CACHE_CONTROL !== 'true' || true, // If true, teddit will automatically try to keep the size of the cache directory (static) under config.cache_max_size. By default this is set to true. cache_control: process.env.CACHE_CONTROL !== 'true' || true, // If true, teddit will automatically try to keep the size of the cache directory (static) under config.cache_max_size. By default this is set to true.
cache_max_size: process.env.CACHE_MAX_SIZE || 3000, // How much can we cache to the disk? Default is 3000 MB (~3 GB). Note: This is not perfectly exact limit. cache_control_interval: process.env.CACHE_CONTROL_INTERVAL || 1000 * 60 * 30, // How often the cached static directory is emptied. Default is every 24 hours minutes.
cache_control_interval: process.env.CACHE_CONTROL_INTERVAL || 1000 * 60 * 30, // How often the size of the cache directory (static/) is checked. Default is every 30 minutes.
post_media_max_heights: { post_media_max_heights: {
/** /**
* Sets the max-height value for images and videos in posts. * Sets the max-height value for images and videos in posts.