File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { FlatCache } from 'flat-cache' ;
22
3+ import { PSGames } from '@/cache' ;
34import { fsPath } from '@/utils/fsPath' ;
5+ import { fromHumanTime } from '@/utils/humanTime' ;
46
57import 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
2225const 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} ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11import { CronJob } from 'cron' ;
22
33import { PSCronJobs } from '@/cache' ;
4+ import { register as registerGeneral } from '@/ps/handlers/cron/general' ;
45import { register as registerHindi } from '@/ps/handlers/cron/hindi' ;
56import { register as registerUGO } from '@/ps/handlers/cron/ugo' ;
67
@@ -22,6 +23,7 @@ export class PSCronJobManager {
2223
2324export 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
You can’t perform that action at this time.
0 commit comments