using System;
using System.IO;
using System.ServiceProcess;
using ApplicationLogger = SI.Moh.Phisco_Periph.Global.ApplicationLogger;
namespace SI.Moh.Periph.Submissions.Service
{
public partial class SubmissionService : ServiceBase
{
private const string ERROR_LOG_NAME = "PeriphErrorLog";
private const string INFO_LOG_NAME = "PeriphInfoLog";
private SubmissionProcessor processor;
public SubmissionService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
processor = new SubmissionProcessor();
var path = Properties.Settings.Default.SubmissionsRootFolder;
var filter = Properties.Settings.Default.SubmissionFileNamePattern;
#if DEBUG
ApplicationLogger.Inst.LogInfo(INFO_LOG_NAME, "Path: " + path);
ApplicationLogger.Inst.LogInfo(INFO_LOG_NAME, "Filter: " + filter);
#endif
// Process files already in the submission folder.
foreach (var file in new DirectoryInfo(path).GetFiles(filter))
{
processor.EnqueueFile(file.FullName);
}
processor.ProcessQueuedFilesAsync();
// Watch for new files in the submission folder.
submissionFolderWatcher.Path = path;
submissionFolderWatcher.Filter = filter;
submissionFolderWatcher.EnableRaisingEvents = true;
}
catch (Exception ex)
{
ApplicationLogger.Inst.LogException(ERROR_LOG_NAME, ex.Message, ex);
}
}
protected override void OnStop()
{
}
private void submissionFolderWatcher_Renamed(object sender, RenamedEventArgs e)
{
ApplicationLogger.Inst.LogInfo(INFO_LOG_NAME, "File detected: " + e.FullPath);
processor.EnqueueFile(e.FullPath);
processor.ProcessQueuedFilesAsync();
}
}
}