feat: implement user pruning functionality with configurable intervals and maximum inactivity days

This commit is contained in:
Viren070
2025-06-06 21:35:22 +01:00
parent 3d7b0da93f
commit 0bf6fcbe9c
3 changed files with 24 additions and 4 deletions
+7 -4
View File
@@ -297,14 +297,17 @@ export class UserRepository {
});
}
static async pruneUsers(maxDays: number = 30): Promise<void> {
static async pruneUsers(maxDays: number = 30): Promise<number> {
try {
const query =
db.getDialect() === 'postgres'
? `DELETE FROM users WHERE accessed_at < NOW() - INTERVAL ${maxDays} DAY`
? `DELETE FROM users WHERE accessed_at < NOW() - INTERVAL '${maxDays} days'`
: `DELETE FROM users WHERE accessed_at < datetime('now', '-' || ${maxDays} || ' days')`;
await db.execute(query);
logger.info(`Pruned users older than ${maxDays} days`);
const result = await db.execute(query);
const deletedCount = result.changes || result.rowCount || 0;
logger.info(`Pruned ${deletedCount} users older than ${maxDays} days`);
return deletedCount;
} catch (error) {
logger.error('Failed to prune users:', error);
return Promise.reject(new APIError(constants.ErrorCode.USER_ERROR));
+9
View File
@@ -284,6 +284,15 @@ export const Env = cleanEnv(process.env, {
desc: 'Signature for the Stremio addons config',
}),
PRUNE_INTERVAL: num({
default: 86400, // 24 hours
desc: 'Interval for pruning inactive users in seconds',
}),
PRUNE_MAX_DAYS: num({
default: 30,
desc: 'Maximum days of inactivity before pruning',
}),
DEFAULT_USER_AGENT: userAgent({
default: `AIOStreams/${metadata?.version || 'unknown'}`,
desc: 'Default user agent for the addon',
+8
View File
@@ -14,9 +14,17 @@ async function initialiseDatabase() {
}
}
async function startAutoPrune() {
try {
await UserRepository.pruneUsers(Env.PRUNE_MAX_DAYS);
} catch {}
setTimeout(startAutoPrune, Env.PRUNE_INTERVAL * 1000);
}
async function start() {
try {
await initialiseDatabase();
startAutoPrune();
app.listen(Env.PORT, () => {
logger.info(`Server running on port ${Env.PORT}`);
});