Skip to content

Commit 1c63cb2

Browse files
committed
feat: Clear stashed games older than a week
1 parent 0b1785b commit 1c63cb2

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/cache/games.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { FlatCache } from 'flat-cache';
22

3+
import { PSGames } from '@/cache';
34
import { fsPath } from '@/utils/fsPath';
5+
import { fromHumanTime } from '@/utils/humanTime';
46

57
import type { GamesList } from '@/ps/games/types';
68

@@ -17,6 +19,7 @@ export type GameCache = {
1719
getByGame(room: string, game: GamesList | 'all'): GameBackup[];
1820
set(backup: GameBackup): void;
1921
delete(id: string): void;
22+
clearOldBackups(): void;
2023
};
2124

2225
const cacheId = 'games.json';
@@ -44,4 +47,12 @@ export const gameCache: GameCache = {
4447
flatCache.delete(id);
4548
flatCache.save();
4649
},
50+
clearOldBackups() {
51+
// Purge backups older than 7 days
52+
const flatCacheObj: Record<string, GameBackup> = flatCache.all();
53+
const stashedGames = Object.values(flatCacheObj).filter(game => !PSGames[game.game]?.[game.id]);
54+
const oldGames = stashedGames.filter(game => game.at < Date.now() - fromHumanTime('7 days'));
55+
oldGames.forEach(game => flatCache.delete(game.id));
56+
flatCache.save();
57+
},
4758
};

src/ps/handlers/cron/general.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { gameCache } from '@/cache/games';
2+
import { TimeZone } from '@/ps/handlers/cron/constants';
3+
4+
import type { PSCronJobManager } from '@/ps/handlers/cron/index';
5+
import type { Client } from 'ps-client';
6+
7+
export function register(this: Client, Jobs: PSCronJobManager): void {
8+
Jobs.register('clear-old-games', '0 0 * * *', TimeZone.GMT, () => {
9+
gameCache.clearOldBackups();
10+
});
11+
}

src/ps/handlers/cron/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CronJob } from 'cron';
22

33
import { PSCronJobs } from '@/cache';
4+
import { register as registerGeneral } from '@/ps/handlers/cron/general';
45
import { register as registerHindi } from '@/ps/handlers/cron/hindi';
56
import { register as registerUGO } from '@/ps/handlers/cron/ugo';
67

@@ -22,6 +23,7 @@ export class PSCronJobManager {
2223

2324
export function startPSCron(this: Client): PSCronJobManager {
2425
const Jobs = new PSCronJobManager();
26+
registerGeneral.call(this, Jobs);
2527
registerHindi.call(this, Jobs);
2628
registerUGO.call(this, Jobs);
2729

0 commit comments

Comments
 (0)