using System;
using System.Configuration;
using System.Linq;
using System.ServiceProcess;
using System.Timers;
using CPE.App.Notify.Extensions;
using CPE.App.Notify.Helpers;
using CPE.App.Notify.Models;
using CPE.App.Notify.Models.Enums;
namespace CPE.App.NotifyService {
public partial class NotifyMonitor : ServiceBase {
#region Delegates
public delegate void AsyncMethodCaller();
#endregion
///
/// Main service class for Windows service import.
///
public NotifyMonitor() {
_notifyConsoleFilePath = ConfigurationManager.AppSettings["NotifyFilePath"];
InitializeComponent();
}
///
/// Is the service configured for debugging in the config file?
///
private static bool debugging {
get { return bool.Parse(ConfigurationManager.AppSettings["Debugging"]); }
}
///
/// Get the service interval for time in milliseconds for processing inbetween requests
///
private static double serviceInterval {
get { return double.Parse(ConfigurationManager.AppSettings["ServiceInterval"]); }
}
private static int poolCount {
get { return int.Parse(ConfigurationManager.AppSettings["PoolCount"]); }
}
///
/// Thios is the method that is called by Windows service.
///
///
protected override void OnStart(string[] args) {
_processTimer.Enabled = true;
_processTimer.Elapsed += ProcessTimerOnElapsed;
_processTimer.Start();
"Notify Monitor started...".Log(LoggingLevels.Info);
$"Debugging is set to {debugging}".Log();
}
///
/// Managed timer for processing notify candidates.
///
///
///
private void ProcessTimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs) {
"Process timer elapsed...".Log();
// This is here to set the interval to 1 minute
// If set to debug then initial start is 2 minutes to allow for attaching to process
// The default start timeout is 100 so the service and begin processing right away
_processTimer.Interval = serviceInterval;
// Only process if all worker jobs are completed
if((_workerCount < poolCount) && !_inProgress) {
_inProgress = true;
IQueryable candidates = null;
try {
candidates = SessionHelper.getRecordingCandidates()
.Log();
} catch (Exception exception) {
exception.Log(LoggingLevels.Fatal);
}
if((candidates != null) && candidates.Any()) {
foreach (var candidate in candidates) {
_workerCount += 1;
try {
_notifyConsoleFilePath.Log();
candidate.Log();
new AsyncMethodCaller(new Worker(candidate.MeetingParticipantSessionKey, _notifyConsoleFilePath, debugging).Process).BeginInvoke(WorkerComplete, null);
} catch (Exception exception) {
exception.Log(LoggingLevels.Fatal);
}
}
} else {
_workerCount = 0;
}
_inProgress = false;
}
}
///
/// Service stop
///
protected override void OnStop() {
_processTimer.Stop();
"Notify Monitor stopped...".Log(LoggingLevels.Info);
}
///
/// Service pause
///
protected override void OnPause() {
_processTimer.Stop();
"Notify Monitor paused...".Log(LoggingLevels.Info);
}
///
/// Service continue
///
protected override void OnContinue() {
_processTimer.Start();
"Notify Monitor continued...".Log(LoggingLevels.Info);
}
///
/// Service shutdown
///
protected override void OnShutdown() {
_processTimer.Stop();
"Notify Monitor shutdown...".Log(LoggingLevels.Info);
}
///
/// Notify process is complete
///
///
public void WorkerComplete(IAsyncResult asyncResult) {
if(_workerCount > 0) {
_workerCount -= 1;
}
}
#region Private Variables
private readonly string _notifyConsoleFilePath;
private readonly Timer _processTimer = new Timer {Enabled = false, Interval = debugging ? 120000 : 100};
private bool _inProgress;
private int _workerCount;
#endregion
}
}