mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Merge pull request #3158 from SillyTavern/max-total-chat-backups
Add maxTotalChatBackups config.yaml value
This commit is contained in:
@@ -98,6 +98,8 @@ skipContentCheck: false
|
||||
disableChatBackup: false
|
||||
# Number of backups to keep for each chat and settings file
|
||||
numberOfBackups: 50
|
||||
# Maximum number of chat backups to keep per user (starting from the most recent). Set to -1 to keep all backups.
|
||||
maxTotalChatBackups: -1
|
||||
# Interval in milliseconds to throttle chat backups per user
|
||||
chatBackupThrottleInterval: 10000
|
||||
# Allowed hosts for card downloads
|
||||
|
@@ -11,6 +11,10 @@ import _ from 'lodash';
|
||||
import { jsonParser, urlencodedParser } from '../express-common.js';
|
||||
import { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, removeOldBackups } from '../util.js';
|
||||
|
||||
const isBackupDisabled = getConfigValue('disableChatBackup', false);
|
||||
const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', -1));
|
||||
const throttleInterval = getConfigValue('chatBackupThrottleInterval', 10_000);
|
||||
|
||||
/**
|
||||
* Saves a chat to the backups directory.
|
||||
* @param {string} directory The user's backups directory.
|
||||
@@ -19,7 +23,6 @@ import { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp,
|
||||
*/
|
||||
function backupChat(directory, name, chat) {
|
||||
try {
|
||||
const isBackupDisabled = getConfigValue('disableChatBackup', false);
|
||||
|
||||
if (isBackupDisabled) {
|
||||
return;
|
||||
@@ -32,6 +35,12 @@ function backupChat(directory, name, chat) {
|
||||
writeFileAtomicSync(backupFile, chat, 'utf-8');
|
||||
|
||||
removeOldBackups(directory, `chat_${name}_`);
|
||||
|
||||
if (isNaN(maxTotalChatBackups) || maxTotalChatBackups < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
removeOldBackups(directory, 'chat_', maxTotalChatBackups);
|
||||
} catch (err) {
|
||||
console.log(`Could not backup chat for ${name}`, err);
|
||||
}
|
||||
@@ -45,7 +54,6 @@ const backupFunctions = new Map();
|
||||
* @returns {function(string, string, string): void} Backup function
|
||||
*/
|
||||
function getBackupFunction(handle) {
|
||||
const throttleInterval = getConfigValue('chatBackupThrottleInterval', 10_000);
|
||||
if (!backupFunctions.has(handle)) {
|
||||
backupFunctions.set(handle, _.throttle(backupChat, throttleInterval, { leading: true, trailing: true }));
|
||||
}
|
||||
|
14
src/util.js
14
src/util.js
@@ -376,16 +376,24 @@ export function generateTimestamp() {
|
||||
* Remove old backups with the given prefix from a specified directory.
|
||||
* @param {string} directory The root directory to remove backups from.
|
||||
* @param {string} prefix File prefix to filter backups by.
|
||||
* @param {number?} limit Maximum number of backups to keep. If null, the limit is determined by the `numberOfBackups` config value.
|
||||
*/
|
||||
export function removeOldBackups(directory, prefix) {
|
||||
const MAX_BACKUPS = Number(getConfigValue('numberOfBackups', 50));
|
||||
export function removeOldBackups(directory, prefix, limit = null) {
|
||||
const MAX_BACKUPS = limit ?? Number(getConfigValue('numberOfBackups', 50));
|
||||
|
||||
let files = fs.readdirSync(directory).filter(f => f.startsWith(prefix));
|
||||
if (files.length > MAX_BACKUPS) {
|
||||
files = files.map(f => path.join(directory, f));
|
||||
files.sort((a, b) => fs.statSync(a).mtimeMs - fs.statSync(b).mtimeMs);
|
||||
|
||||
fs.rmSync(files[0]);
|
||||
while (files.length > MAX_BACKUPS) {
|
||||
const oldest = files.shift();
|
||||
if (!oldest) {
|
||||
break;
|
||||
}
|
||||
|
||||
fs.rmSync(oldest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user