2 using System.Configuration;
4 using System.ServiceProcess;
6 using CPE.App.Notify.Extensions;
7 using CPE.App.Notify.Helpers;
8 using CPE.App.Notify.Models;
9 using CPE.App.Notify.Models.Enums;
11 namespace CPE.App.NotifyService {
12 public partial class NotifyMonitor : ServiceBase {
15 public delegate void AsyncMethodCaller();
20 /// Main service class for Windows service import.
22 public NotifyMonitor() {
23 _notifyConsoleFilePath = ConfigurationManager.AppSettings["NotifyFilePath"];
24 InitializeComponent();
28 /// Is the service configured for debugging in the config file?
30 private static bool debugging {
31 get { return bool.Parse(ConfigurationManager.AppSettings["Debugging"]); }
35 /// Get the service interval for time in milliseconds for processing inbetween requests
37 private static double serviceInterval {
38 get { return double.Parse(ConfigurationManager.AppSettings["ServiceInterval"]); }
41 private static int poolCount {
42 get { return int.Parse(ConfigurationManager.AppSettings["PoolCount"]); }
46 /// Thios is the method that is called by Windows service.
48 /// <param name="args"></param>
49 protected override void OnStart(string[] args) {
50 _processTimer.Enabled = true;
51 _processTimer.Elapsed += ProcessTimerOnElapsed;
52 _processTimer.Start();
53 "Notify Monitor started...".Log(LoggingLevels.Info);
54 $"Debugging is set to {debugging}".Log();
58 /// Managed timer for processing notify candidates.
60 /// <param name="sender"></param>
61 /// <param name="elapsedEventArgs"></param>
62 private void ProcessTimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs) {
63 "Process timer elapsed...".Log();
65 // This is here to set the interval to 1 minute
66 // If set to debug then initial start is 2 minutes to allow for attaching to process
67 // The default start timeout is 100 so the service and begin processing right away
68 _processTimer.Interval = serviceInterval;
70 // Only process if all worker jobs are completed
71 if((_workerCount < poolCount) && !_inProgress) {
73 IQueryable<GetRecordingCandidatesResult> candidates = null;
75 candidates = SessionHelper.getRecordingCandidates()
77 } catch (Exception exception) {
78 exception.Log(LoggingLevels.Fatal);
80 if((candidates != null) && candidates.Any()) {
81 foreach (var candidate in candidates) {
84 _notifyConsoleFilePath.Log();
86 new AsyncMethodCaller(new Worker(candidate.MeetingParticipantSessionKey, _notifyConsoleFilePath, debugging).Process).BeginInvoke(WorkerComplete, null);
87 } catch (Exception exception) {
88 exception.Log(LoggingLevels.Fatal);
102 protected override void OnStop() {
103 _processTimer.Stop();
104 "Notify Monitor stopped...".Log(LoggingLevels.Info);
110 protected override void OnPause() {
111 _processTimer.Stop();
112 "Notify Monitor paused...".Log(LoggingLevels.Info);
118 protected override void OnContinue() {
119 _processTimer.Start();
120 "Notify Monitor continued...".Log(LoggingLevels.Info);
126 protected override void OnShutdown() {
127 _processTimer.Stop();
128 "Notify Monitor shutdown...".Log(LoggingLevels.Info);
132 /// Notify process is complete
134 /// <param name="asyncResult"></param>
135 public void WorkerComplete(IAsyncResult asyncResult) {
136 if(_workerCount > 0) {
141 #region Private Variables
144 private readonly string _notifyConsoleFilePath;
145 private readonly Timer _processTimer = new Timer {Enabled = false, Interval = debugging ? 120000 : 100};
146 private bool _inProgress;
147 private int _workerCount;