void InitDequeue() { if (syslogCancel == null) { lock (sysLogQueue) { if (syslogCancel == null) { syslogCancel = new CancellationTokenSource(); TheCommonUtils.cdeRunAsync("SysLogDequeue", true, DequeueSysLog, syslogCancel.Token); } } } }
public void WriteToLog(int pLogID, TSM MyText, bool NoLog) { if (MyText == null) { return; } if (!UseSysLogQueue) { TheCommonUtils.cdeRunAsync(string.Format("WriteLog:{0}", pLogID), false, o => { WriteToLogInternal(pLogID, MyText, NoLog, TheCommonUtils.CLng(o)); }, Interlocked.Increment(ref LogSerial)); } else { if (!sysLogQueue.TryAdd(new LogArgs { pLogID = pLogID, MyText = MyText, NoLog = NoLog, tLogSerial = Interlocked.Increment(ref LogSerial) })) { // queue is full, log entry dropped } } }
/// <summary> /// Shutsdown the C-DEngine and all plugins /// Applications can override this function to add custom shutdown code /// </summary> /// <param name="ForceExit">If True and the C-DEngine will Stop or restart the hosting application. If it is hosted in IIS, the Application Pool hosting the app will be Stopped or Restarted depending on the next parameter</param> /// <param name="waitIfPending">Waits in case The shutdown is already initiated</param> public virtual void Shutdown(bool ForceExit, bool waitIfPending = false) { if (!waitIfPending) { if (!TheBaseAssets.MasterSwitch || TheCommonUtils.cdeIsLocked(ShutdownLock)) { return; //Make sure we dont do this twice } } lock (ShutdownLock) { if (TheBaseAssets.MasterSwitch) { if (TheBaseAssets.IsStarting) { TheBaseAssets.MySYSLOG?.WriteToLog(3, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM(TheBaseAssets.MyServiceHostInfo.ApplicationName, "Shutdown was requested, but startup has not finished. Waiting for startup to finish.", eMsgLevel.l3_ImportantMessage)); // Ensure we wait for user manager so CUOFS first-run-only settings are applied var sw = System.Diagnostics.Stopwatch.StartNew(); while (TheBaseAssets.IsStarting && sw.ElapsedMilliseconds < 60000) { Thread.Sleep(100); } if (TheBaseAssets.IsStarting) { TheBaseAssets.MySYSLOG?.WriteToLog(3, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(TheBaseAssets.MyServiceHostInfo.ApplicationName, "Shutdown was requested, but startup has not finished after wait time (60 seconds). Shutting down regardless. Some first-run-only settings may not have been applied.", eMsgLevel.l1_Error)); } } if (TheBaseAssets.MyServiceHostInfo.PreShutDownDelay > 0) { var tcsPreHandlers = new TaskCompletionSource <bool>(); TheCommonUtils.cdeRunAsync("PreShutdownHandlers", true, (o) => { TheCDEngines.MyContentEngine?.FireEvent(eEngineEvents.PreShutdown, null, TheBaseAssets.MyServiceHostInfo.PreShutDownDelay, false); tcsPreHandlers.TrySetResult(true); }); tcsPreHandlers.Task.Wait(TheBaseAssets.MyServiceHostInfo.PreShutDownDelay); } while (Interlocked.Read(ref TheBaseAssets.DelayShutDownCount) > 0) { TheCommonUtils.SleepOneEye(100, 100); } TheBaseAssets.MySYSLOG?.WriteToLog(3, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(TheBaseAssets.MyServiceHostInfo.ApplicationName, TheBaseAssets.MyServiceHostInfo.ApplicationTitle + " Initiating shutdown at : " + TheCommonUtils.GetDateTimeString(DateTimeOffset.Now), eMsgLevel.l6_Debug)); TheBaseAssets.MasterSwitch = false; TheBaseAssets.ForceShutdown = ForceExit; TheCDEngines.StopAllEngines(); TheBaseAssets.MySYSLOG?.WriteToLog(3, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(TheBaseAssets.MyServiceHostInfo.ApplicationName, TheBaseAssets.MyServiceHostInfo.ApplicationTitle + " Shutdown: Engines stopped at: " + TheCommonUtils.GetDateTimeString(DateTimeOffset.Now), eMsgLevel.l6_Debug)); TheCommCore.StopCommunication(); TheBaseAssets.MySYSLOG?.WriteToLog(3, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(TheBaseAssets.MyServiceHostInfo.ApplicationName, TheBaseAssets.MyServiceHostInfo.ApplicationTitle + " Shutdown: Communications stopped at: " + TheCommonUtils.GetDateTimeString(DateTimeOffset.Now), eMsgLevel.l6_Debug)); if (MyCommonDisco != null) { MyCommonDisco.ShutdownDiscoService(); TheBaseAssets.MySYSLOG?.WriteToLog(3, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(TheBaseAssets.MyServiceHostInfo.ApplicationName, TheBaseAssets.MyServiceHostInfo.ApplicationTitle + " Shutdown: Discovery Service stopped at: " + TheCommonUtils.GetDateTimeString(DateTimeOffset.Now), eMsgLevel.l6_Debug)); } if (TheBaseAssets.MySYSLOG != null) { string iis = ""; switch (TheBaseAssets.MyServiceHostInfo.cdeHostingType) { case cdeHostType.IIS: iis = " (Hosted in IIS)"; break; case cdeHostType.ASPCore: iis = " (Hosted in ASP.NET Core)"; break; } TheBaseAssets.MySYSLOG.WriteToLog(3, new TSM(TheBaseAssets.MyServiceHostInfo.ApplicationName, TheBaseAssets.MyServiceHostInfo.ApplicationTitle + iis + " Stopped at : " + TheCommonUtils.GetDateTimeString(DateTimeOffset.Now), eMsgLevel.l4_Message)); //Log Entry that service has been started TheBaseAssets.MySYSLOG.Shutdown(); } } } }
/// <summary> /// Override to initalize the app with custom values but still call the base! /// </summary> /// <param name="pPlugInLst">List if ICDEPlugin instances</param> /// <param name="ArgList">Additional Parameters of the Application</param> /// <returns>True if all went well during startup</returns> public virtual bool StartBaseApplication2(List <ICDEPlugin> pPlugInLst, IDictionary <string, string> ArgList) { if (TheBaseAssets.MasterSwitch) { return(false); //SECURITY REVIEW: do not allow to call StartBaseApplication twice } TheBaseAssets.MasterSwitch = true; if (TheBaseAssets.MyServiceHostInfo == null) { TheBaseAssets.MasterSwitch = false; TheSystemMessageLog.ToCo("MyServiceHostInfo is not set! Exiting..."); //Will becalled because if MyServiceHostInfo==null TOCO will be called return(false); } if (TheBaseAssets.CryptoLoadMessage != null) { TheBaseAssets.MasterSwitch = false; TheSystemMessageLog.ToCo($"Security initialization failed with {TheBaseAssets.CryptoLoadMessage}. Exiting..."); return(false); } try { TheBaseAssets.IsStarting = true; //AppDomain.CurrentDomain.UnhandledException += MyUnhandledExceptionHandler; if (ArgList == null) { ArgList = new Dictionary <string, string>(); //DIC-Allowed! } if (!TheBaseAssets.MySettings.ReadAllAppSettings(ref ArgList)) { TheSystemMessageLog.ToCo("Not reading app.config: not supported by platform."); //Will never be called! } if (!TheBaseAssets.MasterSwitch) { return(false); } TheBaseAssets.MyCmdArgs = ArgList; //MyCmdArgs ONLY contains "public" settings coming from app.config or via the host MyUserManager = new TheUserManager(); TheBaseAssets.InitAssets(this); if (!TheBaseAssets.MasterSwitch) { return(false); } TheBaseAssets.MyScopeManager.RegisterScopeChanged(EventScopeChanged); TheBaseAssets.MySYSLOG.WriteToLog(4140, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("TheBaseApplication", "Assets and SystemLog Initialized", eMsgLevel.l3_ImportantMessage)); int minWorker = -1, minCompletionPort = -1, maxWorker = -1, maxCompletionPort = -1; try { System.Threading.ThreadPool.GetMinThreads(out minWorker, out minCompletionPort); System.Threading.ThreadPool.GetMaxThreads(out maxWorker, out maxCompletionPort); } catch { }; #if CDE_STANDARD var largeObjectHeapCompactionMode = System.Runtime.GCSettings.LargeObjectHeapCompactionMode; #else const string largeObjectHeapCompactionMode = "n/a"; #endif TheBaseAssets.MySYSLOG.WriteToLog(4144, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(eEngineName.ContentService, $"CLR Info", eMsgLevel.l4_Message, $"Server GC: {System.Runtime.GCSettings.IsServerGC}. GC Latency Mode: {System.Runtime.GCSettings.LatencyMode}. LOHC Mode: {largeObjectHeapCompactionMode}. Bitness: {System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) * 8}. Threads Min W/CP, Max W/CP: {minWorker}/{minCompletionPort},{maxWorker}/{maxCompletionPort}. GC TotalMemory: {GC.GetTotalMemory(false)}")); if (!TheBaseAssets.MyActivationManager.InitializeLicenses()) { Shutdown(true); TheBaseAssets.MySYSLOG.WriteToLog(4145, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("TheBaseApplication", "Licensing error", eMsgLevel.l3_ImportantMessage)); return(false); } //4.304: Workitem #2289 - Removed from OSS Release (as CheckLicense is always returning true in the CoreCrypto Lib if (!TheBaseAssets.MyActivationManager.CheckLicense(new Guid("{5737240C-AA66-417C-9B66-3919E18F9F4A}"), "", null, 1, false)) { if (TheBaseAssets.MyServiceHostInfo.ShutdownOnLicenseFailure) { TheBaseAssets.MySYSLOG.WriteToLog(4146, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("TheBaseApplication", "Licensing error: no activated license for Thing Service. Shutting down immediately as configured.", eMsgLevel.l2_Warning)); return(false); } else { TheBaseAssets.MySYSLOG.WriteToLog(4147, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("TheBaseApplication", "Licensing error: no activated license for Thing Service. Will shut down in 60 minutes if not activated.", eMsgLevel.l2_Warning)); TheCommonUtils.cdeRunAsync("LicenseCheck", true, (o) => { TheCommonUtils.TaskDelayOneEye(60 * 60 * 1000, 100).ContinueWith((t) => { if (TheBaseAssets.MasterSwitch) { if (!TheBaseAssets.MyActivationManager.CheckLicense(new Guid("{5737240C-AA66-417C-9B66-3919E18F9F4A}"), "", null, 1, false)) { TheBaseAssets.MySYSLOG.WriteToLog(4148, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("TheBaseApplication", "Licensing error: no activated license for Thing Service. Shutting down.", eMsgLevel.l1_Error)); if (TheBaseAssets.IsInAgentStartup) { MyISMRoot?.Restart(true); } else { Shutdown(true); } } } }); }); } } TheCommCore.MyHttpService = new TheHttpService(); TheBaseAssets.MySYSLOG.WriteToLog(4141, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("TheBaseApplication", "HttpService created", eMsgLevel.l3_ImportantMessage)); if (TheBaseAssets.MyServiceHostInfo.StartISM && MyISMRoot == null && !string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.BaseDirectory)) { MyISMRoot = new TheISMManager { eventShutdownRequired = sinkAppShutdown }; MyISMRoot.InitISM("", "", "", TheBaseAssets.MyServiceHostInfo.CurrentVersion, TheBaseAssets.MyServiceHostInfo.ISMScanForUpdatesOnUSB, TheBaseAssets.MyServiceHostInfo.ISMScanOnStartup); TheBaseAssets.MySYSLOG.WriteToLog(4142, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("TheBaseApplication", "ISM Started", eMsgLevel.l3_ImportantMessage)); } else { TheBaseAssets.MyServiceHostInfo.StartISM = false; } if (!TheBaseAssets.MasterSwitch) { return(false); } if (MyCommonDisco == null) { if (!TheBaseAssets.MyServiceHostInfo.IsIsolated && (TheCommonUtils.IsHostADevice() || TheBaseAssets.MyServiceHostInfo.cdeNodeType == cdeNodeType.Active)) { TheBaseAssets.MySettings.SetSetting("DISCO_IsClientOnly", "True"); } MyCommonDisco = new TheCommonDisco(); // Initialized the Discovery Service } TheCDEngines.eventAllEnginesStarted += sinkEnginesStarted; if (!TheCDEngines.StartEngines(pPlugInLst, null)) //Starts all SubEngines and Plugins. MyCmdArgs is a copy of the tParas sent to the Init Assets Function and can be used to set parameters for each engine during startup { Shutdown(true); TheBaseAssets.MySYSLOG.WriteToLog(4149, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("TheBaseApplication", "Failed to start engines", eMsgLevel.l3_ImportantMessage)); return(false); } return(true); } finally { TheBaseAssets.IsStarting = false; } }