diff --git a/lib/app.js b/lib/app.js index 2048d90..ddc4408 100644 --- a/lib/app.js +++ b/lib/app.js @@ -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() + } + }) }) }) } diff --git a/lib/database.js b/lib/database.js index 201384f..9b4828b 100644 --- a/lib/database.js +++ b/lib/database.js @@ -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()