removing old position reports from SQLite storage

This commit is contained in:
Daniel Schick 2022-10-14 08:02:51 +02:00
parent e3e829b006
commit bc6fcb50c4
5 changed files with 51 additions and 4 deletions

View File

@ -17,7 +17,8 @@ namespace bsmd.AIS2Service
private static readonly ILog _log = LogManager.GetLogger(typeof(AISManager));
private static readonly ConcurrentDictionary<int, AIS_Target> _sitRepList = new ConcurrentDictionary<int, AIS_Target>();
private static readonly ConcurrentQueue<AIS_Target> _dbSaveTargets = new ConcurrentQueue<AIS_Target>();
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<int> removeKeyList = new List<int>();
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)

View File

@ -220,11 +220,33 @@ namespace bsmd.AIS2Service
_storageTargets.Add(target.MMSI, target);
}
reader.Close();
cmd.Dispose();
});
return _storageTargets;
}
/// <summary>
/// cleanup DB by removing past pos reports
/// </summary>
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

View File

@ -52,6 +52,9 @@
<setting name="SQLiteDBConnectionString" serializeAs="String">
<value>Data Source=ais_initial.db;Version=3;</value>
</setting>
<setting name="PosReportDBCleanupDays" serializeAs="String">
<value>7</value>
</setting>
</bsmd.AIS2Service.Properties.Settings>
</applicationSettings>
</configuration>

View File

@ -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"]));
}
}
}
}

View File

@ -20,5 +20,8 @@
<Setting Name="SQLiteDBConnectionString" Type="System.String" Scope="Application">
<Value Profile="(Default)">Data Source=ais_initial.db;Version=3;</Value>
</Setting>
<Setting Name="PosReportDBCleanupDays" Type="System.Int32" Scope="Application">
<Value Profile="(Default)">7</Value>
</Setting>
</Settings>
</SettingsFile>