private static void SetPurgeTempFilesTimer(Dictionary <string, string> commandlineOptions) { var lastPurge = new DateTime(0); System.Threading.TimerCallback purgeTempFilesCallback = (x) => { try { #if DEBUG if (Math.Abs((DateTime.Now - lastPurge).TotalHours) < 1) { return; } #else if (Math.Abs((DateTime.Now - lastPurge).TotalHours) < 23) { return; } #endif lastPurge = DateTime.Now; foreach (var e in DataConnection.GetTempFiles().Where((f) => f.Expires < DateTime.Now)) { try { if (System.IO.File.Exists(e.Path)) { System.IO.File.Delete(e.Path); } } catch (Exception ex) { DataConnection.LogError(null, $"Failed to delete temp file: {e.Path}", ex); } DataConnection.DeleteTempFile(e.ID); } Library.Utility.TempFile.RemoveOldApplicationTempFiles((path, ex) => { DataConnection.LogError(null, $"Failed to delete temp file: {path}", ex); }); if (!commandlineOptions.TryGetValue("log-retention", out string pts)) { pts = DEFAULT_LOG_RETENTION; } DataConnection.PurgeLogData(Library.Utility.Timeparser.ParseTimeInterval(pts, DateTime.Now, true)); } catch (Exception ex) { DataConnection.LogError(null, "Failed during temp file cleanup", ex); } }; try { #if DEBUG PurgeTempFilesTimer = new System.Threading.Timer(purgeTempFilesCallback, null, TimeSpan.FromSeconds(10), TimeSpan.FromHours(1)); #else PurgeTempFilesTimer = new System.Threading.Timer(purgeTempFilesCallback, null, TimeSpan.FromHours(1), TimeSpan.FromDays(1)); #endif } catch (ArgumentOutOfRangeException) { //Bugfix for older Mono, slightly more resources used to avoid large values in the period field PurgeTempFilesTimer = new System.Threading.Timer(purgeTempFilesCallback, null, TimeSpan.FromHours(1), TimeSpan.FromHours(1)); } }
public static int RealMain(string[] _args) { //If we are on Windows, append the bundled "win-tools" programs to the search path //We add it last, to allow the user to override with other versions if (Platform.IsClientWindows) { Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + System.IO.Path.PathSeparator.ToString() + System.IO.Path.Combine( System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "win-tools") ); } //If this executable is invoked directly, write to console, otherwise throw exceptions var writeToConsole = System.Reflection.Assembly.GetEntryAssembly() == System.Reflection.Assembly.GetExecutingAssembly(); //Find commandline options here for handling special startup cases var args = new List <string>(_args); var optionsWithFilter = Library.Utility.FilterCollector.ExtractOptions(new List <string>(args)); var commandlineOptions = optionsWithFilter.Item1; var filter = optionsWithFilter.Item2; if (_args.Select(s => s.ToLower()).Intersect(AlternativeHelpStrings.ConvertAll(x => x.ToLower())).Any()) { return(ShowHelp(writeToConsole)); } if (commandlineOptions.ContainsKey("tempdir") && !string.IsNullOrEmpty(commandlineOptions["tempdir"])) { Library.Utility.SystemContextSettings.DefaultTempPath = commandlineOptions["tempdir"]; } Library.Utility.SystemContextSettings.StartSession(); var parameterFileOption = commandlineOptions.Keys.Select(s => s.ToLower()) .Intersect(ParameterFileOptionStrings.ConvertAll(x => x.ToLower())).FirstOrDefault(); if (parameterFileOption != null && !string.IsNullOrEmpty(commandlineOptions[parameterFileOption])) { string filename = commandlineOptions[parameterFileOption]; commandlineOptions.Remove(parameterFileOption); if (!ReadOptionsFromFile(filename, ref filter, args, commandlineOptions)) { return(100); } } ConfigureLogging(commandlineOptions); try { DataConnection = GetDatabaseConnection(commandlineOptions); if (!DataConnection.ApplicationSettings.FixedInvalidBackupId) { DataConnection.FixInvalidBackupId(); } CreateApplicationInstance(writeToConsole); StartOrStopUsageReporter(); AdjustApplicationSettings(commandlineOptions); ApplicationExitEvent = new System.Threading.ManualResetEvent(false); Library.AutoUpdater.UpdaterManager.OnError += (Exception obj) => { DataConnection.LogError(null, "Error in updater", obj); }; UpdatePoller = new UpdatePollThread(); SetPurgeTempFilesTimer(commandlineOptions); SetLiveControls(); SetWorkerThread(); StartWebServer(commandlineOptions); if (Library.Utility.Utility.ParseBoolOption(commandlineOptions, "ping-pong-keepalive")) { PingPongThread = new System.Threading.Thread(PingPongMethod) { IsBackground = true }; PingPongThread.Start(); } ServerStartedEvent.Set(); ApplicationExitEvent.WaitOne(); } catch (SingleInstance.MultipleInstanceException mex) { System.Diagnostics.Trace.WriteLine(Strings.Program.SeriousError(mex.ToString())); if (!writeToConsole) { throw; } Console.WriteLine(Strings.Program.SeriousError(mex.ToString())); return(100); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(Strings.Program.SeriousError(ex.ToString())); if (writeToConsole) { Console.WriteLine(Strings.Program.SeriousError(ex.ToString())); return(100); } else { throw new Exception(Strings.Program.SeriousError(ex.ToString()), ex); } } finally { StatusEventNotifyer.SignalNewEvent(); UpdatePoller?.Terminate(); Scheduler?.Terminate(true); WorkThread?.Terminate(true); ApplicationInstance?.Dispose(); PurgeTempFilesTimer?.Dispose(); Library.UsageReporter.Reporter.ShutDown(); try { PingPongThread?.Abort(); } catch { } LogHandler?.Dispose(); } if (UpdatePoller != null && UpdatePoller.IsUpdateRequested) { return(Library.AutoUpdater.UpdaterManager.MAGIC_EXIT_CODE); } return(0); }