mirror of
https://github.com/Viren070/AIOStreams.git
synced 2025-12-01 23:14:04 +01:00
feat: implement user pruning functionality with configurable intervals and maximum inactivity days
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user