-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
297 lines (249 loc) · 12 KB
/
Program.cs
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using System;
using System.Collections.Generic;
using System.Threading;
using PRISM;
using PRISM.Logging;
namespace MyEMSL_MTS_File_Cache_Manager
{
/// <summary>
/// This program processes the MyEMSL Download Queue in MTS to download requested files
/// </summary>
/// <remarks>
/// <para>
/// Written by Matthew Monroe for the Department of Energy (PNNL, Richland, WA)</para>
/// <para>
/// E-mail: matthew.monroe@pnnl.gov or proteomics@pnnl.gov
/// Website: https://github.com/PNNL-Comp-Mass-Spec/ or https://panomics.pnnl.gov/ or https://www.pnnl.gov/integrative-omics
/// </para>
/// </remarks>
internal static class Program
{
public const string PROGRAM_DATE = "May 9, 2023";
private static BaseLogger.LogLevels mLogLevel;
private static string mMTSServer;
private static string mLogDBConnectionString;
private static int mMinimumCacheFreeSpaceGB;
private static bool mLocalServerMode;
private static bool mPreviewMode;
private static bool mTraceMode;
private static double mPercentComplete;
private static DateTime mLastProgressUpdateTime;
/// <summary>
/// Main entry method
/// </summary>
public static int Main()
{
var commandLineParser = new clsParseCommandLine();
mLogLevel = BaseLogger.LogLevels.INFO;
mMTSServer = string.Empty;
mLogDBConnectionString = clsMyEMSLMTSFileCacher.LOG_DB_CONNECTION_STRING;
mMinimumCacheFreeSpaceGB = clsMyEMSLMTSFileCacher.DEFAULT_MINIMUM_CACHE_FREE_SPACE_GB;
mLocalServerMode = false;
mPreviewMode = false;
mTraceMode = false;
try
{
var success = false;
if (commandLineParser.ParseCommandLine())
{
if (SetOptionsUsingCommandLineParameters(commandLineParser))
success = true;
}
if (!success ||
commandLineParser.NeedToShowHelp ||
commandLineParser.ParameterCount + commandLineParser.NonSwitchParameterCount == 0 ||
mMTSServer.Length == 0 && !mLocalServerMode)
{
ShowProgramHelp();
return -1;
}
if (mLocalServerMode)
{
mMTSServer = string.Empty;
}
else
{
var updatesArePending = WindowsUpdateStatus.UpdatesArePending(out var pendingWindowsUpdateMessage);
if (updatesArePending)
{
Console.WriteLine(pendingWindowsUpdateMessage);
Console.WriteLine("Will not contact the MTS server to process cache requests");
return 0;
}
}
var downloader = new clsMyEMSLMTSFileCacher(mMTSServer, mLogLevel, mLogDBConnectionString)
{
MinimumCacheFreeSpaceGB = mMinimumCacheFreeSpaceGB,
TraceMode = mTraceMode
};
// Attach the events
downloader.DebugEvent += Downloader_DebugEvent;
downloader.ErrorEvent += Downloader_ErrorEvent;
downloader.StatusEvent += Downloader_StatusEvent;
downloader.WarningEvent += Downloader_WarningEvent;
downloader.ProgressUpdate += Downloader_ProgressUpdate;
mPercentComplete = 0;
mLastProgressUpdateTime = DateTime.UtcNow;
// Initiate processing, which will contact the MTS Server to see if any files need to be cached
success = downloader.Start(mPreviewMode);
LogTools.FlushPendingMessages();
if (!success)
{
ShowErrorMessage("Error processing cache requests for MTS server " + mMTSServer + ": " + downloader.ErrorMessage);
return -3;
}
}
catch (Exception ex)
{
LogTools.FlushPendingMessages();
Console.WriteLine("Error occurred in Program->Main: " + Environment.NewLine + ex.Message);
Console.WriteLine(ex.StackTrace);
Thread.Sleep(1500);
return -1;
}
return 0;
}
/// <summary>
/// Set options using command line parameters
/// </summary>
/// <param name="commandLineParser"></param>
/// <returns>True if no problems; otherwise, returns false</returns>
private static bool SetOptionsUsingCommandLineParameters(clsParseCommandLine commandLineParser)
{
var validParameters = new List<string> { "Local", "Preview", "Trace", "LogDB", "FS" };
try
{
// Make sure no invalid parameters are present
if (commandLineParser.InvalidParametersPresent(validParameters))
{
var badArguments = new List<string>();
foreach (var item in commandLineParser.InvalidParameters(validParameters))
{
badArguments.Add("/" + item);
}
ShowErrorMessage("Invalid command line parameters", badArguments);
return false;
}
// Query commandLineParser to see if various parameters are present
if (commandLineParser.NonSwitchParameterCount > 0)
{
mMTSServer = commandLineParser.RetrieveNonSwitchParameter(0);
}
if (commandLineParser.IsParameterPresent("Local"))
{
mLocalServerMode = true;
}
if (commandLineParser.IsParameterPresent("Preview"))
{
mPreviewMode = true;
}
if (commandLineParser.IsParameterPresent("Trace"))
{
mTraceMode = true;
}
if (commandLineParser.RetrieveValueForParameter("LogDB", out var strValue))
{
if (string.IsNullOrWhiteSpace(strValue))
ShowErrorMessage("/LogDB does not have a value; not overriding the logging connection string");
else
mLogDBConnectionString = strValue;
}
if (commandLineParser.RetrieveValueForParameter("FS", out strValue))
{
if (string.IsNullOrWhiteSpace(strValue))
{
ShowErrorMessage("/FS does not have a value; not overriding the minimum free space");
}
else
{
if (!int.TryParse(strValue, out mMinimumCacheFreeSpaceGB))
ShowErrorMessage("Error converting " + strValue + " to an integer for parameter /FS");
}
}
return true;
}
catch (Exception ex)
{
ShowErrorMessage("Error parsing the command line parameters: " + Environment.NewLine + ex.Message);
}
return false;
}
private static void ShowErrorMessage(string message, Exception ex = null)
{
ConsoleMsgUtils.ShowError(message, ex);
}
private static void ShowErrorMessage(string message, IEnumerable<string> additionalInfo)
{
ConsoleMsgUtils.ShowErrors(message, additionalInfo);
}
private static void ShowProgramHelp()
{
var exeName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);
try
{
Console.WriteLine();
Console.WriteLine("This program processes the MyEMSL Download Queue in MTS to download requested files");
Console.WriteLine();
Console.WriteLine("Program syntax #1:" + Environment.NewLine + exeName);
Console.WriteLine(" MTS_Server [/Preview] [/Trace] [/LogDB] [/FS:MinimumFreeSpaceGB]");
Console.WriteLine();
Console.WriteLine("Program syntax #2:" + Environment.NewLine + exeName);
Console.WriteLine(" /Local [/Preview] [/Trace] [/LogDB] [/FS:MinimumFreeSpaceGB]");
Console.WriteLine();
Console.WriteLine("MTS_Server specifies the MTS server to contact (using database MT_Main)");
Console.WriteLine("Alternatively, use /Local to contact MT_Main on the local server running this program");
Console.WriteLine();
Console.WriteLine("Use /Preview to preview any files that would be uploaded");
Console.WriteLine("Use /Trace to show additional log messages");
Console.WriteLine();
Console.WriteLine("Use /LogDB to override the default connection string for logging messages to a database");
Console.WriteLine("The default is /LogDB:" + clsMyEMSLMTSFileCacher.LOG_DB_CONNECTION_STRING);
Console.WriteLine();
Console.WriteLine("Use /FS to customize the minimum free disk space that must be present on the cache drive");
Console.WriteLine("The default is /FS:" + clsMyEMSLMTSFileCacher.DEFAULT_MINIMUM_CACHE_FREE_SPACE_GB);
Console.WriteLine("Use /FS:0 to disable examining the free disk space");
Console.WriteLine();
Console.WriteLine("Program written by Matthew Monroe for the Department of Energy (PNNL, Richland, WA)");
Console.WriteLine("Version: " + AppUtils.GetAppVersion(PROGRAM_DATE));
Console.WriteLine();
Console.WriteLine("E-mail: matthew.monroe@pnnl.gov or proteomics@pnnl.gov");
Console.WriteLine("Website: https://github.com/PNNL-Comp-Mass-Spec/ or https://panomics.pnnl.gov/ or https://www.pnnl.gov/integrative-omics");
Console.WriteLine();
// Delay for 1500 msec in case the user double clicked this file from within Windows Explorer (or started the program via a shortcut)
Thread.Sleep(1500);
}
catch (Exception ex)
{
Console.WriteLine("Error displaying the program syntax: " + ex.Message);
}
}
#region "Event Handlers"
private static void Downloader_DebugEvent(string strMessage)
{
ConsoleMsgUtils.ShowDebug(strMessage);
}
private static void Downloader_ErrorEvent(string errorMessage, Exception ex)
{
ConsoleMsgUtils.ShowError(errorMessage, ex);
}
private static void Downloader_StatusEvent(string message)
{
Console.WriteLine(message);
}
private static void Downloader_WarningEvent(string strMessage)
{
ConsoleMsgUtils.ShowWarning(strMessage);
}
private static void Downloader_ProgressUpdate(string progressMessage, float percentComplete)
{
if (!(percentComplete > mPercentComplete) && !(DateTime.UtcNow.Subtract(mLastProgressUpdateTime).TotalSeconds >= 30))
return;
if (!(DateTime.UtcNow.Subtract(mLastProgressUpdateTime).TotalSeconds >= 1))
return;
Console.WriteLine("Percent complete: " + percentComplete.ToString("0.0") + "%");
mPercentComplete = percentComplete;
mLastProgressUpdateTime = DateTime.UtcNow;
}
#endregion
}
}