update signutare
[CPE_learningsite] / CPE / CPE.App / CPE.App.NotifyService / CPEMonitor.cs
1 using System;
2 using System.Configuration;
3 using System.Linq;
4 using System.ServiceProcess;
5 using System.Timers;
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;
10
11 namespace CPE.App.NotifyService {
12     public partial class NotifyMonitor : ServiceBase {
13         #region Delegates
14
15         public delegate void AsyncMethodCaller();
16
17         #endregion
18
19         /// <summary>
20         ///     Main service class for Windows service import.
21         /// </summary>
22         public NotifyMonitor() {
23             _notifyConsoleFilePath = ConfigurationManager.AppSettings["NotifyFilePath"];
24             InitializeComponent();
25         }
26
27         /// <summary>
28         ///     Is the service configured for debugging in the config file?
29         /// </summary>
30         private static bool debugging {
31             get { return bool.Parse(ConfigurationManager.AppSettings["Debugging"]); }
32         }
33
34         /// <summary>
35         ///     Get the service interval for time in milliseconds for processing inbetween requests
36         /// </summary>
37         private static double serviceInterval {
38             get { return double.Parse(ConfigurationManager.AppSettings["ServiceInterval"]); }
39         }
40
41         private static int poolCount {
42             get { return int.Parse(ConfigurationManager.AppSettings["PoolCount"]); }
43         }
44
45         /// <summary>
46         ///     Thios is the method that is called by Windows service.
47         /// </summary>
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();
55         }
56
57         /// <summary>
58         ///     Managed timer for processing notify candidates.
59         /// </summary>
60         /// <param name="sender"></param>
61         /// <param name="elapsedEventArgs"></param>
62         private void ProcessTimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs) {
63             "Process timer elapsed...".Log();
64
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;
69
70             // Only process if all worker jobs are completed
71             if((_workerCount < poolCount) && !_inProgress) {
72                 _inProgress = true;
73                 IQueryable<GetRecordingCandidatesResult> candidates = null;
74                 try {
75                     candidates = SessionHelper.getRecordingCandidates()
76                                               .Log();
77                 } catch (Exception exception) {
78                     exception.Log(LoggingLevels.Fatal);
79                 }
80                 if((candidates != null) && candidates.Any()) {
81                     foreach (var candidate in candidates) {
82                         _workerCount += 1;
83                         try {
84                             _notifyConsoleFilePath.Log();
85                             candidate.Log();
86                             new AsyncMethodCaller(new Worker(candidate.MeetingParticipantSessionKey, _notifyConsoleFilePath, debugging).Process).BeginInvoke(WorkerComplete, null);
87                         } catch (Exception exception) {
88                             exception.Log(LoggingLevels.Fatal);
89                         }
90                     }
91                 } else {
92                     _workerCount = 0;
93                 }
94
95                 _inProgress = false;
96             }
97         }
98
99         /// <summary>
100         ///     Service stop
101         /// </summary>
102         protected override void OnStop() {
103             _processTimer.Stop();
104             "Notify Monitor stopped...".Log(LoggingLevels.Info);
105         }
106
107         /// <summary>
108         ///     Service pause
109         /// </summary>
110         protected override void OnPause() {
111             _processTimer.Stop();
112             "Notify Monitor paused...".Log(LoggingLevels.Info);
113         }
114
115         /// <summary>
116         ///     Service continue
117         /// </summary>
118         protected override void OnContinue() {
119             _processTimer.Start();
120             "Notify Monitor continued...".Log(LoggingLevels.Info);
121         }
122
123         /// <summary>
124         ///     Service shutdown
125         /// </summary>
126         protected override void OnShutdown() {
127             _processTimer.Stop();
128             "Notify Monitor shutdown...".Log(LoggingLevels.Info);
129         }
130
131         /// <summary>
132         ///     Notify process is complete
133         /// </summary>
134         /// <param name="asyncResult"></param>
135         public void WorkerComplete(IAsyncResult asyncResult) {
136             if(_workerCount > 0) {
137                 _workerCount -= 1;
138             }
139         }
140
141         #region Private Variables
142
143         
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;
148
149         #endregion
150     }
151 }