-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathmaintenance-service.ts
More file actions
38 lines (33 loc) · 1.39 KB
/
maintenance-service.ts
File metadata and controls
38 lines (33 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { createLogger } from '../factories/logger-factory'
import { IEventRepository } from '../@types/repositories'
import { IMaintenanceService } from '../@types/services'
import { Settings } from '../@types/settings'
const debug = createLogger('maintenance-service')
export class MaintenanceService implements IMaintenanceService {
public constructor(
private readonly eventRepository: IEventRepository,
private readonly settings: () => Settings,
) {}
public async clearOldEvents(): Promise<void> {
const currentSettings = this.settings()
const retention = currentSettings.limits?.event?.retention
const maxDays = retention?.maxDays
if (typeof maxDays !== 'number' || isNaN(maxDays) || maxDays <= 0) {
return
}
try {
debug('purging deleted, expired and old events')
const deletedCounts = await this.eventRepository.deleteExpiredAndRetained({
maxDays,
kindWhitelist: retention?.kind?.whitelist,
pubkeyWhitelist: retention?.pubkey?.whitelist,
})
const totalDeleted = deletedCounts.deleted + deletedCounts.expired + deletedCounts.retained
if (totalDeleted > 0) {
console.info(`[Maintenance] Deleted events: deleted=${deletedCounts.deleted}, expired=${deletedCounts.expired}, retained=${deletedCounts.retained}.`)
}
} catch (error) {
console.error('Unable to purge events. Reason:', error)
}
}
}