From a710cf03210ce174d2d7d54c38c92e02db190bb0 Mon Sep 17 00:00:00 2001 From: Ivar Date: Thu, 7 Sep 2017 22:52:24 +0200 Subject: [PATCH] Clean up intervals. We use intervals in three places and we could probably organise them better in the future. As long as they all do unref they do not form any issues for us and I will just let them be as is for now. This closes #186 --- lib/db/client-instance-store.js | 15 +++++---------- lib/db/client-metrics-db.js | 10 +++++----- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/lib/db/client-instance-store.js b/lib/db/client-instance-store.js index 08666c77e2..00e9ade47d 100644 --- a/lib/db/client-instance-store.js +++ b/lib/db/client-instance-store.js @@ -12,6 +12,8 @@ const COLUMNS = [ ]; const TABLE = 'client_instances'; +const ONE_DAY = 24 * 61 * 60 * 1000; + const mapRow = row => ({ appName: row.app_name, instanceId: row.instance_id, @@ -21,19 +23,12 @@ const mapRow = row => ({ createdAt: row.created_at, }); -// const mapAppsRow = (row) => ({ -// appName: row.app_name, -// createdAt: row.created_at, -// }); - class ClientInstanceStore { constructor(db) { this.db = db; - setTimeout(() => this._removeInstancesOlderThanTwoDays(), 10).unref(); - setInterval( - () => this._removeInstancesOlderThanTwoDays(), - 24 * 61 * 60 * 1000 - ).unref(); + const clearer = () => this._removeInstancesOlderThanTwoDays(); + setTimeout(clearer, 10).unref(); + setInterval(clearer, ONE_DAY).unref(); } _removeInstancesOlderThanTwoDays() { diff --git a/lib/db/client-metrics-db.js b/lib/db/client-metrics-db.js index 1ee94adfc0..3512206f35 100644 --- a/lib/db/client-metrics-db.js +++ b/lib/db/client-metrics-db.js @@ -5,6 +5,8 @@ const logger = require('../logger')('client-metrics-db.js'); const METRICS_COLUMNS = ['id', 'created_at', 'metrics']; const TABLE = 'client_metrics'; +const ONE_MINUTE = 60 * 1000; + const mapRow = row => ({ id: row.id, createdAt: row.created_at, @@ -16,11 +18,9 @@ class ClientMetricsDb { this.db = db; // Clear old metrics regulary - setTimeout(() => this.removeMetricsOlderThanOneHour(), 10).unref(); - setInterval( - () => this.removeMetricsOlderThanOneHour(), - 60 * 1000 - ).unref(); + const clearer = () => this.removeMetricsOlderThanOneHour(); + setTimeout(clearer, 10).unref(); + setInterval(clearer, ONE_MINUTE).unref(); } removeMetricsOlderThanOneHour() {