git_bsmd/bsmd.dakosy.ResponseService/SFTPService.cs

87 lines
2.8 KiB
C#

using bsmd.database;
using log4net;
using System;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Timers;
namespace bsmd.dakosy.ResponseService
{
/// <summary>
/// Windows service zum Abruf von NSW Response Dateien via SFTP und Import der Dateien in die DB
/// </summary>
public partial class SFTPService : ServiceBase
{
private Timer _timer;
private object _timerlock = new object();
private bool processRunning = false;
private ILog _log = LogManager.GetLogger(typeof(SFTPService));
public SFTPService()
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.EventLog.Source = this.ServiceName;
this.EventLog.Log = "Application";
this.Init(args);
this.EventLog.WriteEntry("NSW SFTP Response Service started.", EventLogEntryType.Information);
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
_log.InfoFormat("Dakosy SFTP Response Service started. v.{0}", version);
this._timer_Elapsed(null, null);
}
protected override void OnStop()
{
_log.Info("SFTPService stopped");
}
public void Init(string[] args)
{
this._timer = new Timer();
this._timer.Interval = Properties.Settings.Default.SleepSeconds * 1000;
this._timer.Elapsed += _timer_Elapsed;
this._timer.Enabled = true;
if (Debugger.IsAttached)
this._timer_Elapsed(null, null);
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
lock (this._timerlock)
{
if (this.processRunning) return;
else this.processRunning = true;
}
if (DBManager.Instance.Connect(Properties.Settings.Default.ConnectionString))
{
try
{
Response.ReadAll();
}
catch (Exception ex)
{
_log.ErrorFormat("Exception occurred: {0}", ex.ToString());
}
}
else
{
this.EventLog.WriteEntry("FormService DB connection failure", EventLogEntryType.Error);
}
lock (this._timerlock)
{
this.processRunning = false;
}
}
}
}