/* Developer: Tyler Allen Date Created: 08/24/2016 --------------------------------------------------- */ using System; using System.Collections.Generic; using System.Configuration; using System.Text; using System.Threading; using CPE.App.Notify.Extensions; using CPE.App.Notify.Helpers; using CPE.App.Notify.Models.Enums; using CPE.App.NotifyConsole.Extensions; namespace CPE.App.NotifyConsole { internal class Program { private static bool _receivedArgsAtStart; /// /// Base application start method /// /// Application arguments private static void Main(string[] args) { "Application started".Log(LoggingLevels.Info); if(args.Length > 0) { if(!args.IsTesting()) { _receivedArgsAtStart = true; foreach (var arg in args) { arg.Log(LoggingLevels.Info); } processRequest(args); } "Application ended".Log(LoggingLevels.Info); } else { while(!_receivedArgsAtStart) { processRequest(args); } } // ReSharper disable once FunctionNeverReturns } /// /// The main method for processing all requests /// /// Application arguments private static void processRequest(string[] args) { // Determine which type of method was requested from the console var applicationMethod = args.WhichMethod(); switch (applicationMethod) { case ApplicationMethods.Help: case ApplicationMethods.NotFound: case ApplicationMethods.Invalid: manualConsole(args); break; default: sessionConsole(args); break; } } /// /// Processes the session console for stop/archive sessions /// /// Application arguments private static void sessionConsole(string[] args) { // Check if the application is processed to run immediately // This will aide in troubleshooting and failover var appWait = args.Wait(); if(appWait) { var waitInMinutes = 0; var waitTimeMilliseconds = 0; try { // Get minutes to wait from the application configuration file waitInMinutes = int.Parse(ConfigurationManager.AppSettings["WaitInMinutes"]); var timeSpan = new TimeSpan(0, waitInMinutes, 0); // Calculate milliseconds waitTimeMilliseconds = Convert.ToInt32(timeSpan.TotalMilliseconds); } catch (Exception exception) { exception.Log(LoggingLevels.Fatal); "The application was asked to wait but was given an invalid wait time in the configuration".Log(); // End the application return; } // Wait $"Application waiting {waitInMinutes} minutes...".Log(); Thread.Sleep(waitTimeMilliseconds); } // Determine which type of method was requested from the console var applicationMethod = args.WhichMethod(); if(applicationMethod == ApplicationMethods.Invalid) { // Invalid console arg was given closeConsole(args); } else { // Get all keys passed through the args // Multiple keys can be passed int[] keys = null; try { keys = args.ArgumentValues(); } catch (KeyNotFoundException keyNotFoundException) { keyNotFoundException.Log(LoggingLevels.Fatal); } if(keys != null) { switch (applicationMethod) { case ApplicationMethods.StopSession: foreach (var key in keys) { try { SessionHelper.StopSession(key); } catch (Exception exception) { exception.Log(LoggingLevels.Fatal); } } break; case ApplicationMethods.ArchiveSession: foreach (var key in keys) { try { // Was the key passed meant for a meeting session? if(args.IsMeetingSession()) { SessionHelper.ArchiveSessionByMeetingSession(key); } else { SessionHelper.ArchiveSession(key); } } catch (Exception exception) { exception.Log(LoggingLevels.Fatal); } } break; } Console.WriteLine("Job complete"); Thread.Sleep(1000); } if(args.Unattended() || _receivedArgsAtStart) { closeConsole(args); } } } /// /// Prints help documentation to the console /// private static void helpConsole() { var outputString = new StringBuilder(); outputString.AppendLine("Processes the stop/archive session notifications."); outputString.AppendLine(); outputString.AppendLine("Stop session"); outputString.AppendLine("NOTIFY [{S, -S, /S}[meetingsessionkeys]]"); outputString.AppendLine(); outputString.AppendLine("Archive session"); outputString.AppendLine("NOTIFY [{A, -A, /A}[meetingparticipantsessionkeys]]"); outputString.AppendLine(); outputString.AppendLine(" NOWAIT This will skip the configured process waiting period."); outputString.AppendLine(" -NOWAIT"); outputString.AppendLine(" /NOWAIT"); outputString.AppendLine(); outputString.AppendLine(); outputString.AppendLine(); Console.Write(outputString); } /// /// This will handle the client interface to use application manually /// /// Application arguments private static void manualConsole(string[] args) { // If this is ran unattended then we do not want an open process. if(args.Unattended()) { closeConsole(args); } // Output help message helpConsole(); // No arguments were supplied // This allows for the entry of args directly into the command window Console.Write("[ENTER PARAMETERS ('quit' to close application)]: "); var consoleValue = Console.ReadLine(); if((consoleValue.ToLower() == "exit") || (consoleValue.ToLower() == "quit")) { closeConsole(args); } if(string.IsNullOrEmpty(consoleValue)) { return; } // Create an array of the command arguments args = consoleValue.Split(Convert.ToChar(" ")); // Reprocess the request processRequest(args); } /// /// Close the application /// /// private static void closeConsole(string[] args) { if(!args.Unattended()) { Console.Write("Goodbye!"); Thread.Sleep(500); } Environment.Exit(0); } } }