initial commit
[CPE_learningsite] / CPE / CPE.App / CPE.App.NotifyConsole / Program.cs
1 /*
2 Developer: Tyler Allen
3 Date Created: 08/24/2016
4 ---------------------------------------------------
5 */
6
7 using System;
8 using System.Collections.Generic;
9 using System.Configuration;
10 using System.Text;
11 using System.Threading;
12 using CPE.App.Notify.Extensions;
13 using CPE.App.Notify.Helpers;
14 using CPE.App.Notify.Models.Enums;
15 using CPE.App.NotifyConsole.Extensions;
16
17 namespace CPE.App.NotifyConsole {
18     internal class Program {
19         private static bool _receivedArgsAtStart;
20
21
22         /// <summary>
23         ///     Base application start method
24         /// </summary>
25         /// <param name="args">Application arguments</param>
26         private static void Main(string[] args) {
27             "Application started".Log(LoggingLevels.Info);
28
29             if(args.Length > 0) {
30                 if(!args.IsTesting()) {
31                     _receivedArgsAtStart = true;
32                     foreach (var arg in args) {
33                         arg.Log(LoggingLevels.Info);
34                     }
35                     processRequest(args);
36                 }
37                 "Application ended".Log(LoggingLevels.Info);
38             } else {
39                 while(!_receivedArgsAtStart) {
40                     processRequest(args);
41                 }
42             }
43
44             // ReSharper disable once FunctionNeverReturns
45         }
46
47         /// <summary>
48         ///     The main method for processing all requests
49         /// </summary>
50         /// <param name="args">Application arguments</param>
51         private static void processRequest(string[] args) {
52             // Determine which type of method was requested from the console
53             var applicationMethod = args.WhichMethod();
54
55             switch (applicationMethod) {
56                 case ApplicationMethods.Help:
57                 case ApplicationMethods.NotFound:
58                 case ApplicationMethods.Invalid:
59                     manualConsole(args);
60                     break;
61                 default:
62                     sessionConsole(args);
63                     break;
64             }
65         }
66
67         /// <summary>
68         ///     Processes the session console for stop/archive sessions
69         /// </summary>
70         /// <param name="args">Application arguments</param>
71         private static void sessionConsole(string[] args) {
72             // Check if the application is processed to run immediately
73             // This will aide in troubleshooting and failover
74             var appWait = args.Wait();
75             if(appWait) {
76                 var waitInMinutes = 0;
77                 var waitTimeMilliseconds = 0;
78                 try {
79                     // Get minutes to wait from the application configuration file
80                     waitInMinutes = int.Parse(ConfigurationManager.AppSettings["WaitInMinutes"]);
81                     var timeSpan = new TimeSpan(0, waitInMinutes, 0);
82                     // Calculate milliseconds
83                     waitTimeMilliseconds = Convert.ToInt32(timeSpan.TotalMilliseconds);
84                 } catch (Exception exception) {
85                     exception.Log(LoggingLevels.Fatal);
86                     "The application was asked to wait but was given an invalid wait time in the configuration".Log();
87                     // End the application
88                     return;
89                 }
90
91                 // Wait
92                 $"Application waiting {waitInMinutes} minutes...".Log();
93                 Thread.Sleep(waitTimeMilliseconds);
94             }
95
96
97             // Determine which type of method was requested from the console
98             var applicationMethod = args.WhichMethod();
99
100             if(applicationMethod == ApplicationMethods.Invalid) {
101                 // Invalid console arg was given
102                 closeConsole(args);
103             } else {
104                 // Get all keys passed through the args
105                 // Multiple keys can be passed
106                 int[] keys = null;
107                 try {
108                     keys = args.ArgumentValues();
109                 } catch (KeyNotFoundException keyNotFoundException) {
110                     keyNotFoundException.Log(LoggingLevels.Fatal);
111                 }
112                 if(keys != null) {
113                     switch (applicationMethod) {
114                         case ApplicationMethods.StopSession:
115                             foreach (var key in keys) {
116                                 try {
117                                     SessionHelper.StopSession(key);
118                                 } catch (Exception exception) {
119                                     exception.Log(LoggingLevels.Fatal);
120                                 }
121                             }
122                             break;
123                         case ApplicationMethods.ArchiveSession:
124                             foreach (var key in keys) {
125                                 try {
126                                     // Was the key passed meant for a meeting session?
127                                     if(args.IsMeetingSession()) {
128                                         SessionHelper.ArchiveSessionByMeetingSession(key);
129                                     } else {
130                                         SessionHelper.ArchiveSession(key);
131                                     }
132                                 } catch (Exception exception) {
133                                     exception.Log(LoggingLevels.Fatal);
134                                 }
135                             }
136                             break;
137                     }
138                     Console.WriteLine("Job complete");
139                     Thread.Sleep(1000);
140                 }
141                 if(args.Unattended() || _receivedArgsAtStart) {
142                     closeConsole(args);
143                 }
144             }
145         }
146
147         /// <summary>
148         ///     Prints help documentation to the console
149         /// </summary>
150         private static void helpConsole() {
151             var outputString = new StringBuilder();
152             outputString.AppendLine("Processes the stop/archive session notifications.");
153             outputString.AppendLine();
154             outputString.AppendLine("Stop session");
155             outputString.AppendLine("NOTIFY [{S, -S, /S}[meetingsessionkeys]]");
156             outputString.AppendLine();
157             outputString.AppendLine("Archive session");
158             outputString.AppendLine("NOTIFY [{A, -A, /A}[meetingparticipantsessionkeys]]");
159             outputString.AppendLine();
160             outputString.AppendLine("    NOWAIT    This will skip the configured process waiting period.");
161             outputString.AppendLine("    -NOWAIT");
162             outputString.AppendLine("    /NOWAIT");
163             outputString.AppendLine();
164             outputString.AppendLine();
165             outputString.AppendLine();
166
167             Console.Write(outputString);
168         }
169
170         /// <summary>
171         ///     This will handle the client interface to use application manually
172         /// </summary>
173         /// <param name="args">Application arguments</param>
174         private static void manualConsole(string[] args) {
175             // If this is ran unattended then we do not want an open process.
176             if(args.Unattended()) {
177                 closeConsole(args);
178             }
179
180             // Output help message
181             helpConsole();
182             // No arguments were supplied
183             // This allows for the entry of args directly into the command window
184             Console.Write("[ENTER PARAMETERS ('quit' to close application)]: ");
185             var consoleValue = Console.ReadLine();
186             if((consoleValue.ToLower() == "exit") || (consoleValue.ToLower() == "quit")) {
187                 closeConsole(args);
188             }
189             if(string.IsNullOrEmpty(consoleValue)) {
190                 return;
191             }
192             // Create an array of the command arguments
193             args = consoleValue.Split(Convert.ToChar(" "));
194             // Reprocess the request
195             processRequest(args);
196         }
197
198         /// <summary>
199         ///     Close the application
200         /// </summary>
201         /// <param name="args"></param>
202         private static void closeConsole(string[] args) {
203             if(!args.Unattended()) {
204                 Console.Write("Goodbye!");
205                 Thread.Sleep(500);
206             }
207             Environment.Exit(0);
208         }
209     }
210 }