| 6 | const logger = createLogger('maintenance-service') |
| 7 | |
| 8 | export class MaintenanceService implements IMaintenanceService { |
| 9 | public constructor( |
| 10 | private readonly eventRepository: IEventRepository, |
| 11 | private readonly settings: () => Settings, |
| 12 | ) {} |
| 13 | |
| 14 | public async clearOldEvents(): Promise<void> { |
| 15 | const currentSettings = this.settings() |
| 16 | const retention = currentSettings.limits?.event?.retention |
| 17 | const maxDays = retention?.maxDays |
| 18 | |
| 19 | if (typeof maxDays !== 'number' || isNaN(maxDays) || maxDays <= 0) { |
| 20 | return |
| 21 | } |
| 22 | |
| 23 | try { |
| 24 | logger('purging deleted, expired and old events') |
| 25 | const deletedCounts = await this.eventRepository.deleteExpiredAndRetained({ |
| 26 | maxDays, |
| 27 | kindWhitelist: retention?.kind?.whitelist, |
| 28 | pubkeyWhitelist: retention?.pubkey?.whitelist, |
| 29 | }) |
| 30 | const totalDeleted = deletedCounts.deleted + deletedCounts.expired + deletedCounts.retained |
| 31 | if (totalDeleted > 0) { |
| 32 | logger.info( |
| 33 | `[Maintenance] Deleted events: deleted=${deletedCounts.deleted}, expired=${deletedCounts.expired}, retained=${deletedCounts.retained}.`, |
| 34 | ) |
| 35 | } |
| 36 | } catch (error) { |
| 37 | logger.error('Unable to purge events. Reason:', error) |
| 38 | } |
| 39 | } |
| 40 | } |
nothing calls this directly
no outgoing calls
no test coverage detected