Delete old pings also on startup

This commit is contained in:
mjezek 2022-05-22 16:28:58 +02:00
parent 23b46a3dd5
commit 37a1579b37
No known key found for this signature in database
GPG Key ID: 724CD28A57A1A1BE
2 changed files with 21 additions and 9 deletions

@ -22,11 +22,13 @@ class App {
// Setup database instance
this.database.ensureIndexes(() => {
this.database.loadGraphPoints(config.graphDuration, () => {
if (config.deleteOldPings) {
this.database.initOldPingsDeleter()
}
this.database.loadRecords(callback)
this.database.loadRecords(() => {
if (config.deleteOldPings) {
this.database.initOldPingsDelete(callback)
} else {
callback()
}
})
})
})
}

@ -267,12 +267,18 @@ class Database {
statement.finalize()
}
initOldPingsDeleter () {
// Delete old records every hour
setInterval(() => this.deleteOldPingRecords(), 3600000)
initOldPingsDelete (callback) {
// Delete old ping records on startup
logger.info('Deleting old ping records..')
this.deleteOldPingRecords(() => {
// Delete old ping records every hour
setInterval(() => this.deleteOldPingRecords(), 3600000)
callback()
})
}
deleteOldPingRecords () {
deleteOldPingRecords (callback) {
// The oldest timestamp that will be kept
const oldestTimestamp = TimeTracker.getEpochMillis() - config.graphDuration
@ -285,6 +291,10 @@ class Database {
} else {
const deleteTook = TimeTracker.getEpochMillis() - deleteStart
logger.info(`Old ping records deleted in ${deleteTook}ms`)
if (callback !== undefined) {
callback()
}
}
})
statement.finalize()