added timer to purge old files after X days

This commit is contained in:
Daniel Schick 2023-03-06 09:10:29 +01:00
parent 7e23f5a98a
commit c44a257209
10 changed files with 153 additions and 25 deletions

View File

@ -34,6 +34,12 @@
<setting name="ConnectionString" serializeAs="String"> <setting name="ConnectionString" serializeAs="String">
<value>Initial Catalog=nswtest;Data Source=192.168.2.24\SQLEXPRESS;Uid=dfuser;pwd=dfpasswd;Persist Security Info=False;Connection Reset=false</value> <value>Initial Catalog=nswtest;Data Source=192.168.2.24\SQLEXPRESS;Uid=dfuser;pwd=dfpasswd;Persist Security Info=False;Connection Reset=false</value>
</setting> </setting>
<setting name="PurgeFilesTimerIntervalHours" serializeAs="String">
<value>24</value>
</setting>
<setting name="TempFilesMaxAgeDays" serializeAs="String">
<value>10</value>
</setting>
</SendNSWMessageService.Properties.Settings> </SendNSWMessageService.Properties.Settings>
</applicationSettings> </applicationSettings>
</configuration> </configuration>

View File

@ -13,6 +13,7 @@ namespace SendNSWMessageService
public partial class NSWSendService : ServiceBase public partial class NSWSendService : ServiceBase
{ {
private Timer _timer; private Timer _timer;
private Timer _filesTimer;
private readonly object _timerlock = new object(); private readonly object _timerlock = new object();
private bool processRunning = false; private bool processRunning = false;
private readonly ILog _log = LogManager.GetLogger(typeof(NSWSendService)); private readonly ILog _log = LogManager.GetLogger(typeof(NSWSendService));
@ -62,11 +63,23 @@ namespace SendNSWMessageService
this._timer = new Timer(); this._timer = new Timer();
this._timer.Interval = Properties.Settings.Default.SleepSeconds * 1000; this._timer.Interval = Properties.Settings.Default.SleepSeconds * 1000;
this._timer.Elapsed += _timer_Elapsed; this._timer.Elapsed += _timer_Elapsed;
this._timer.Enabled = true; this._timer.Enabled = true;
this._filesTimer = new Timer();
this._filesTimer.Interval = Properties.Settings.Default.PurgeFilesTimerIntervalHours * 60 * 60 * 1000; // hours to millisecs
this._filesTimer.Elapsed += _filesTimer_Elapsed;
this._filesTimer.Enabled = true;
}
private void _filesTimer_Elapsed(object sender, ElapsedEventArgs e)
{
bsmd.dbh.MessageController.PurgeOldFiles(Properties.Settings.Default.TempFilesMaxAgeDays);
bsmd.hisnord.transmitter.PurgeOldFiles(Properties.Settings.Default.TempFilesMaxAgeDays);
} }
public void DoOnce() public void DoOnce()
{ {
this._filesTimer_Elapsed(null, null);
this._timer_Elapsed(null, null); this._timer_Elapsed(null, null);
} }
@ -307,17 +320,20 @@ namespace SendNSWMessageService
protected override void OnPause() protected override void OnPause()
{ {
this._timer.Stop(); this._timer.Stop();
this._filesTimer.Stop();
} }
protected override void OnContinue() protected override void OnContinue()
{ {
this._timer.Start(); this._timer.Start();
this._filesTimer.Start();
} }
protected override void OnStop() protected override void OnStop()
{ {
this._timer.Enabled = false; this._timer.Enabled = false;
this._filesTimer.Enabled = false;
this.EventLog.WriteEntry("NSW Send Service stopped.", EventLogEntryType.Information); this.EventLog.WriteEntry("NSW Send Service stopped.", EventLogEntryType.Information);
_log.Info("NSW Send Service stopped"); _log.Info("NSW Send Service stopped");
} }

View File

@ -41,5 +41,23 @@ namespace SendNSWMessageService.Properties {
return ((string)(this["ConnectionString"])); return ((string)(this["ConnectionString"]));
} }
} }
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("24")]
public int PurgeFilesTimerIntervalHours {
get {
return ((int)(this["PurgeFilesTimerIntervalHours"]));
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("10")]
public int TempFilesMaxAgeDays {
get {
return ((int)(this["TempFilesMaxAgeDays"]));
}
}
} }
} }

View File

@ -8,5 +8,11 @@
<Setting Name="ConnectionString" Type="System.String" Scope="Application"> <Setting Name="ConnectionString" Type="System.String" Scope="Application">
<Value Profile="(Default)">Initial Catalog=nswtest;Data Source=192.168.2.24\SQLEXPRESS;Uid=dfuser;pwd=dfpasswd;Persist Security Info=False;Connection Reset=false</Value> <Value Profile="(Default)">Initial Catalog=nswtest;Data Source=192.168.2.24\SQLEXPRESS;Uid=dfuser;pwd=dfpasswd;Persist Security Info=False;Connection Reset=false</Value>
</Setting> </Setting>
<Setting Name="PurgeFilesTimerIntervalHours" Type="System.Int32" Scope="Application">
<Value Profile="(Default)">24</Value>
</Setting>
<Setting Name="TempFilesMaxAgeDays" Type="System.Int32" Scope="Application">
<Value Profile="(Default)">10</Value>
</Setting>
</Settings> </Settings>
</SettingsFile> </SettingsFile>

View File

@ -6,6 +6,7 @@ using System;
using System.IO; using System.IO;
using bsmd.database; using bsmd.database;
using System.Linq;
namespace bsmd.dbh namespace bsmd.dbh
{ {
@ -14,6 +15,8 @@ namespace bsmd.dbh
private static readonly ILog _log = LogManager.GetLogger(typeof(MessageController)); private static readonly ILog _log = LogManager.GetLogger(typeof(MessageController));
private static int? _fileSequenceCounter = null; private static int? _fileSequenceCounter = null;
#region send single message
public static bool SendMessage(MessageCore core, Message message) public static bool SendMessage(MessageCore core, Message message)
{ {
bool result = true; bool result = true;
@ -63,6 +66,10 @@ namespace bsmd.dbh
return result; return result;
} }
#endregion
#region send cancel core messaage
public static bool SendCancelCore(MessageCore core) public static bool SendCancelCore(MessageCore core)
{ {
bool result = true; bool result = true;
@ -103,6 +110,10 @@ namespace bsmd.dbh
return result; return result;
} }
#endregion
#region send and receive files (SFTP)
public static void SendAndReceive() public static void SendAndReceive()
{ {
// sent unsent messages in output folder // sent unsent messages in output folder
@ -144,5 +155,35 @@ namespace bsmd.dbh
} }
} }
#endregion
#region Purge old files
public static void PurgeOldFiles(int maxAgeDays)
{
DirectoryInfo info = new DirectoryInfo(Properties.Settings.Default.IncomingArchiveFolder);
FileInfo[] files = info.GetFiles();
foreach (FileInfo file in files)
{
if (file.CreationTime < DateTime.Now.AddDays(-maxAgeDays))
{
_log.Debug($"deleting {file.Name}");
file.Delete();
}
}
info = new DirectoryInfo(Properties.Settings.Default.OutgoingArchiveFolder);
files = info.GetFiles();
foreach (FileInfo file in files)
{
if (file.CreationTime < DateTime.Now.AddDays(-maxAgeDays))
{
_log.Debug($"deleting {file.Name}");
file.Delete();
}
}
}
#endregion
} }
} }

View File

@ -34,7 +34,7 @@ namespace bsmd.hisnord.Properties {
[global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("E:\\svnlager\\BSMD\\nsw\\HIS-NORD\\Transmitter-Tool\\IMP")] [global::System.Configuration.DefaultSettingValueAttribute("Transmitter-Tool\\IMP")]
public string OutputDir { public string OutputDir {
get { get {
return ((string)(this["OutputDir"])); return ((string)(this["OutputDir"]));
@ -43,7 +43,7 @@ namespace bsmd.hisnord.Properties {
[global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("E:\\svnlager\\BSMD\\nsw\\HIS-NORD\\Transmitter-Tool\\client.bat")] [global::System.Configuration.DefaultSettingValueAttribute("Transmitter-Tool\\client.bat")]
public string Transmitter { public string Transmitter {
get { get {
return ((string)(this["Transmitter"])); return ((string)(this["Transmitter"]));
@ -52,7 +52,7 @@ namespace bsmd.hisnord.Properties {
[global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("E:\\svnlager\\BSMD\\nsw\\HIS-NORD\\Transmitter-Tool\\RESULTS")] [global::System.Configuration.DefaultSettingValueAttribute("Transmitter-Tool\\RESULTS")]
public string ResultDir { public string ResultDir {
get { get {
return ((string)(this["ResultDir"])); return ((string)(this["ResultDir"]));
@ -61,7 +61,7 @@ namespace bsmd.hisnord.Properties {
[global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("E:\\svnlager\\BSMD\\nsw\\HIS-NORD\\Transmitter-Tool\\ANSWERS")] [global::System.Configuration.DefaultSettingValueAttribute("Transmitter-Tool\\ANSWERS")]
public string AnswerDir { public string AnswerDir {
get { get {
return ((string)(this["AnswerDir"])); return ((string)(this["AnswerDir"]));
@ -70,7 +70,7 @@ namespace bsmd.hisnord.Properties {
[global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("E:\\svnlager\\BSMD\\nsw\\HIS-NORD\\Transmitter-Tool\\ANSWERS_DONE")] [global::System.Configuration.DefaultSettingValueAttribute("Transmitter-Tool\\ANSWERS_DONE")]
public string AnswerArchiveDir { public string AnswerArchiveDir {
get { get {
return ((string)(this["AnswerArchiveDir"])); return ((string)(this["AnswerArchiveDir"]));
@ -79,7 +79,7 @@ namespace bsmd.hisnord.Properties {
[global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("E:\\svnlager\\BSMD\\nsw\\HIS-NORD\\Transmitter-Tool\\ANSWERS_CORRUPT")] [global::System.Configuration.DefaultSettingValueAttribute("Transmitter-Tool\\ANSWERS_CORRUPT")]
public string AnswerCorruptDir { public string AnswerCorruptDir {
get { get {
return ((string)(this["AnswerCorruptDir"])); return ((string)(this["AnswerCorruptDir"]));

View File

@ -6,22 +6,22 @@
<Value Profile="(Default)">1</Value> <Value Profile="(Default)">1</Value>
</Setting> </Setting>
<Setting Name="OutputDir" Type="System.String" Scope="Application"> <Setting Name="OutputDir" Type="System.String" Scope="Application">
<Value Profile="(Default)">E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\IMP</Value> <Value Profile="(Default)">Transmitter-Tool\IMP</Value>
</Setting> </Setting>
<Setting Name="Transmitter" Type="System.String" Scope="Application"> <Setting Name="Transmitter" Type="System.String" Scope="Application">
<Value Profile="(Default)">E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\client.bat</Value> <Value Profile="(Default)">Transmitter-Tool\client.bat</Value>
</Setting> </Setting>
<Setting Name="ResultDir" Type="System.String" Scope="Application"> <Setting Name="ResultDir" Type="System.String" Scope="Application">
<Value Profile="(Default)">E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\RESULTS</Value> <Value Profile="(Default)">Transmitter-Tool\RESULTS</Value>
</Setting> </Setting>
<Setting Name="AnswerDir" Type="System.String" Scope="Application"> <Setting Name="AnswerDir" Type="System.String" Scope="Application">
<Value Profile="(Default)">E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\ANSWERS</Value> <Value Profile="(Default)">Transmitter-Tool\ANSWERS</Value>
</Setting> </Setting>
<Setting Name="AnswerArchiveDir" Type="System.String" Scope="Application"> <Setting Name="AnswerArchiveDir" Type="System.String" Scope="Application">
<Value Profile="(Default)">E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\ANSWERS_DONE</Value> <Value Profile="(Default)">Transmitter-Tool\ANSWERS_DONE</Value>
</Setting> </Setting>
<Setting Name="AnswerCorruptDir" Type="System.String" Scope="Application"> <Setting Name="AnswerCorruptDir" Type="System.String" Scope="Application">
<Value Profile="(Default)">E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\ANSWERS_CORRUPT</Value> <Value Profile="(Default)">Transmitter-Tool\ANSWERS_CORRUPT</Value>
</Setting> </Setting>
<Setting Name="TransmitterRoot" Type="System.String" Scope="Application"> <Setting Name="TransmitterRoot" Type="System.String" Scope="Application">
<Value Profile="(Default)">E:\svnlager\BSMD\nsw\HIS-NORD\</Value> <Value Profile="(Default)">E:\svnlager\BSMD\nsw\HIS-NORD\</Value>

View File

@ -74,7 +74,9 @@ namespace bsmd.hisnord
} }
} }
} }
#region Create message file to send
public static bool? CreateSendFile(MessageCore core, Message message) public static bool? CreateSendFile(MessageCore core, Message message)
{ {
@ -1807,7 +1809,9 @@ namespace bsmd.hisnord
} }
return retval; return retval;
} }
#endregion
#region helper func for HAZ positions #region helper func for HAZ positions

View File

@ -11,22 +11,22 @@
<value>1</value> <value>1</value>
</setting> </setting>
<setting name="OutputDir" serializeAs="String"> <setting name="OutputDir" serializeAs="String">
<value>E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\IMP</value> <value>Transmitter-Tool\IMP</value>
</setting> </setting>
<setting name="Transmitter" serializeAs="String"> <setting name="Transmitter" serializeAs="String">
<value>E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\client.bat</value> <value>Transmitter-Tool\client.bat</value>
</setting> </setting>
<setting name="ResultDir" serializeAs="String"> <setting name="ResultDir" serializeAs="String">
<value>E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\RESULTS</value> <value>Transmitter-Tool\RESULTS</value>
</setting> </setting>
<setting name="AnswerDir" serializeAs="String"> <setting name="AnswerDir" serializeAs="String">
<value>E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\ANSWERS</value> <value>Transmitter-Tool\ANSWERS</value>
</setting> </setting>
<setting name="AnswerArchiveDir" serializeAs="String"> <setting name="AnswerArchiveDir" serializeAs="String">
<value>E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\ANSWERS_DONE</value> <value>Transmitter-Tool\ANSWERS_DONE</value>
</setting> </setting>
<setting name="AnswerCorruptDir" serializeAs="String"> <setting name="AnswerCorruptDir" serializeAs="String">
<value>E:\svnlager\BSMD\nsw\HIS-NORD\Transmitter-Tool\ANSWERS_CORRUPT</value> <value>Transmitter-Tool\ANSWERS_CORRUPT</value>
</setting> </setting>
<setting name="TransmitterRoot" serializeAs="String"> <setting name="TransmitterRoot" serializeAs="String">
<value>E:\svnlager\BSMD\nsw\HIS-NORD\</value> <value>E:\svnlager\BSMD\nsw\HIS-NORD\</value>

View File

@ -106,8 +106,45 @@ namespace bsmd.hisnord
string resultDir = Path.Combine(Properties.Settings.Default.TransmitterRoot, Properties.Settings.Default.ResultDir); string resultDir = Path.Combine(Properties.Settings.Default.TransmitterRoot, Properties.Settings.Default.ResultDir);
string resultFullPath = Path.Combine(resultDir, resultFilename); string resultFullPath = Path.Combine(resultDir, resultFilename);
return result.ReadResult(resultFullPath); return result.ReadResult(resultFullPath);
} }
public static void PurgeOldFiles(int maxAgeDays)
{
// "ANSWERS_DONE"
DirectoryInfo info = new DirectoryInfo(Path.Combine(Properties.Settings.Default.TransmitterRoot, Properties.Settings.Default.AnswerArchiveDir));
FileInfo[] files = info.GetFiles();
foreach (FileInfo file in files)
{
if (file.CreationTime < DateTime.Now.AddDays(-maxAgeDays))
{
_log.Debug($"deleting {file.Name}");
file.Delete();
}
}
info = new DirectoryInfo(Path.Combine(Properties.Settings.Default.TransmitterRoot, Properties.Settings.Default.ResultDir)); // "RESULTS"
files = info.GetFiles();
foreach (FileInfo file in files)
{
if (file.CreationTime < DateTime.Now.AddDays(-maxAgeDays))
{
_log.Debug($"deleting {file.Name}");
file.Delete();
}
}
info = new DirectoryInfo(Path.Combine(Properties.Settings.Default.TransmitterRoot, "READY"));
files = info.GetFiles();
foreach (FileInfo file in files)
{
if (file.CreationTime < DateTime.Now.AddDays(-maxAgeDays))
{
_log.Debug($"deleting {file.Name}");
file.Delete();
}
}
}
/// <summary> /// <summary>
/// class to read transmitter result xml files /// class to read transmitter result xml files
/// </summary> /// </summary>