Git Repository Public Repository

CPE_learningsite

URLs

Copy to Clipboard

This repository has no backups
This repository's network speed is throttled to 100KB/sec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
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;


        /// <summary>
        ///     Base application start method
        /// </summary>
        /// <param name="args">Application arguments</param>
        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
        }

        /// <summary>
        ///     The main method for processing all requests
        /// </summary>
        /// <param name="args">Application arguments</param>
        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;
            }
        }

        /// <summary>
        ///     Processes the session console for stop/archive sessions
        /// </summary>
        /// <param name="args">Application arguments</param>
        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);
                }
            }
        }

        /// <summary>
        ///     Prints help documentation to the console
        /// </summary>
        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);
        }

        /// <summary>
        ///     This will handle the client interface to use application manually
        /// </summary>
        /// <param name="args">Application arguments</param>
        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);
        }

        /// <summary>
        ///     Close the application
        /// </summary>
        /// <param name="args"></param>
        private static void closeConsole(string[] args) {
            if(!args.Unattended()) {
                Console.Write("Goodbye!");
                Thread.Sleep(500);
            }
            Environment.Exit(0);
        }
    }
}

Commits for CPE_learningsite/CPE/CPE.App/CPE.App.NotifyConsole/Program.cs

Diff revisions: vs.
Revision Author Commited Message
4cd176 ... v.shishlov Fri 27 Aug, 2021 14:33:17 +0000

initial commit