3 Date Created: 08/24/2016
4 ---------------------------------------------------
8 using System.Collections.Generic;
9 using System.Configuration;
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;
17 namespace CPE.App.NotifyConsole {
18 internal class Program {
19 private static bool _receivedArgsAtStart;
23 /// Base application start method
25 /// <param name="args">Application arguments</param>
26 private static void Main(string[] args) {
27 "Application started".Log(LoggingLevels.Info);
30 if(!args.IsTesting()) {
31 _receivedArgsAtStart = true;
32 foreach (var arg in args) {
33 arg.Log(LoggingLevels.Info);
37 "Application ended".Log(LoggingLevels.Info);
39 while(!_receivedArgsAtStart) {
44 // ReSharper disable once FunctionNeverReturns
48 /// The main method for processing all requests
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();
55 switch (applicationMethod) {
56 case ApplicationMethods.Help:
57 case ApplicationMethods.NotFound:
58 case ApplicationMethods.Invalid:
68 /// Processes the session console for stop/archive sessions
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();
76 var waitInMinutes = 0;
77 var waitTimeMilliseconds = 0;
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
92 $"Application waiting {waitInMinutes} minutes...".Log();
93 Thread.Sleep(waitTimeMilliseconds);
97 // Determine which type of method was requested from the console
98 var applicationMethod = args.WhichMethod();
100 if(applicationMethod == ApplicationMethods.Invalid) {
101 // Invalid console arg was given
104 // Get all keys passed through the args
105 // Multiple keys can be passed
108 keys = args.ArgumentValues();
109 } catch (KeyNotFoundException keyNotFoundException) {
110 keyNotFoundException.Log(LoggingLevels.Fatal);
113 switch (applicationMethod) {
114 case ApplicationMethods.StopSession:
115 foreach (var key in keys) {
117 SessionHelper.StopSession(key);
118 } catch (Exception exception) {
119 exception.Log(LoggingLevels.Fatal);
123 case ApplicationMethods.ArchiveSession:
124 foreach (var key in keys) {
126 // Was the key passed meant for a meeting session?
127 if(args.IsMeetingSession()) {
128 SessionHelper.ArchiveSessionByMeetingSession(key);
130 SessionHelper.ArchiveSession(key);
132 } catch (Exception exception) {
133 exception.Log(LoggingLevels.Fatal);
138 Console.WriteLine("Job complete");
141 if(args.Unattended() || _receivedArgsAtStart) {
148 /// Prints help documentation to the console
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();
167 Console.Write(outputString);
171 /// This will handle the client interface to use application manually
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()) {
180 // Output help message
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")) {
189 if(string.IsNullOrEmpty(consoleValue)) {
192 // Create an array of the command arguments
193 args = consoleValue.Split(Convert.ToChar(" "));
194 // Reprocess the request
195 processRequest(args);
199 /// Close the application
201 /// <param name="args"></param>
202 private static void closeConsole(string[] args) {
203 if(!args.Unattended()) {
204 Console.Write("Goodbye!");