diff --git a/AIS/bsmd.AIS2Service/AISManager.cs b/AIS/bsmd.AIS2Service/AISManager.cs index 28ca2acc..e11c3d3c 100644 --- a/AIS/bsmd.AIS2Service/AISManager.cs +++ b/AIS/bsmd.AIS2Service/AISManager.cs @@ -17,7 +17,8 @@ namespace bsmd.AIS2Service private static readonly ILog _log = LogManager.GetLogger(typeof(AISManager)); private static readonly ConcurrentDictionary _sitRepList = new ConcurrentDictionary(); private static readonly ConcurrentQueue _dbSaveTargets = new ConcurrentQueue(); - private static Timer _staleTargetTimer; + private static Timer _staleTargetTimer; // cleanup sitrep + private static Timer _stalePosReportTimer; // clean db public static async void Start() { @@ -41,8 +42,9 @@ namespace bsmd.AIS2Service task.FatalErrorOccurred += Task_FatalErrorOccurred; } - _staleTargetTimer = new Timer(StaleTimerCheck, null, 0, 60000); // check every minute - + // init timer tasks + _staleTargetTimer = new Timer(StaleTargetTimerCheck, null, 0, 60000); // check every minute, start immediately + _stalePosReportTimer = new Timer(StalePosReportCheck, sqliteStorage, 0, 60000 * 10); // every ten minutes, } private static void Task_FatalErrorOccurred(object sender, EventArgs e) @@ -50,7 +52,7 @@ namespace bsmd.AIS2Service throw new NotImplementedException("TBD: shutdown the whole operation?"); } - private static void StaleTimerCheck(object state) + private static void StaleTargetTimerCheck(object state) { List removeKeyList = new List(); foreach (int key in _sitRepList.Keys) @@ -68,6 +70,14 @@ namespace bsmd.AIS2Service } } + private static void StalePosReportCheck(object state) + { + if (state is AIS_SQLiteStorage storage) + { + storage.RemoveOldPosReports(); + } + } + public static void Stop() { foreach (var task in _tasks) diff --git a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs index eff297cc..1e3123f5 100644 --- a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs +++ b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs @@ -220,11 +220,33 @@ namespace bsmd.AIS2Service _storageTargets.Add(target.MMSI, target); } reader.Close(); + cmd.Dispose(); }); return _storageTargets; } + /// + /// cleanup DB by removing past pos reports + /// + public void RemoveOldPosReports() + { + try + { + string removeQuery = string.Format("DELETE FROM posreport where timestamp <= datetime('now', '-{0} day')", + Properties.Settings.Default.PosReportDBCleanupDays); + SQLiteCommand cmd = new SQLiteCommand(removeQuery, _connection); + int rows = cmd.ExecuteNonQuery(); + if(rows > 0) + _log.InfoFormat("Removed {0} position reports older than {1} days", rows, Properties.Settings.Default.PosReportDBCleanupDays); + cmd.Dispose(); + } + catch(SQLiteException ex) + { + _log.ErrorFormat("Failed to delete stale position reports: {0}", ex.Message); + } + } + #endregion #region IAISThread implementation diff --git a/AIS/bsmd.AIS2Service/App.config b/AIS/bsmd.AIS2Service/App.config index dda61558..9560d53c 100644 --- a/AIS/bsmd.AIS2Service/App.config +++ b/AIS/bsmd.AIS2Service/App.config @@ -52,6 +52,9 @@ Data Source=ais_initial.db;Version=3; + + 7 + \ No newline at end of file diff --git a/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs b/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs index b552994b..2dd9f04e 100644 --- a/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs +++ b/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs @@ -76,5 +76,14 @@ namespace bsmd.AIS2Service.Properties { return ((string)(this["SQLiteDBConnectionString"])); } } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("7")] + public int PosReportDBCleanupDays { + get { + return ((int)(this["PosReportDBCleanupDays"])); + } + } } } diff --git a/AIS/bsmd.AIS2Service/Properties/Settings.settings b/AIS/bsmd.AIS2Service/Properties/Settings.settings index a78537db..5068b48c 100644 --- a/AIS/bsmd.AIS2Service/Properties/Settings.settings +++ b/AIS/bsmd.AIS2Service/Properties/Settings.settings @@ -20,5 +20,8 @@ Data Source=ais_initial.db;Version=3; + + 7 + \ No newline at end of file