diff --git a/Vrh.Web.Reporting/HangfireBootstrapper.cs b/Vrh.Web.Reporting/HangfireBootstrapper.cs index bbbd6a1..dc68a72 100644 --- a/Vrh.Web.Reporting/HangfireBootstrapper.cs +++ b/Vrh.Web.Reporting/HangfireBootstrapper.cs @@ -117,7 +117,7 @@ namespace Vrh.Web.HangfireBootstrapperNS foreach (var ei in externalinitializerList) { var externalinitializername = $"External initializer: {ei.Method.DeclaringType.FullName}.{ei.Method.Name}()"; - try { ei.Invoke(HangfireBootstrapper.Instance._backgroundJobServer); le.AddDataField($"{externalinitializername} SUCCESS",$"{counter}of{numofall})"); } + try { ei.Invoke(HangfireBootstrapper.Instance._backgroundJobServer); le.AddDataField($"{externalinitializername} SUCCESS",$"{counter}of{numofall}"); } catch (Exception ex) { le.AddDataField($"{externalinitializername} EXCEPTION", ex.Message); } counter++; } @@ -210,23 +210,6 @@ namespace Vrh.Web.HangfireBootstrapperNS const string CONFIGFILENAME = @"applicationHost.config"; //const string FILEPATH = @"C:\temp\applicationHost.config"; - const string SYSTEMAPPLICATIONHOST_ELEMENT = "system.applicationHost"; - const string APPLICATIONPOOLS_ELEMENT = "applicationPools"; - const string SITES_ELEMENT = "sites"; - const string SERVICEAUTOSTARTPROVIDERS_ELEMENT = "serviceAutoStartProviders"; - const string SITE_ELEMENT = "site"; - const string APPLICATION_ELEMENT = "application"; - const string ADD_ELEMENT = "add"; - const string NAME_ATTRIBUTE = "name"; - const string TYPE_ATTRIBUTE = "type"; - const string SERVICEAUTOSTARTENABLED_ATTRIBUTE = "serviceAutoStartEnabled"; - const string SERVICEAUTOSTARTPROVIDER_ATTRIBUTE = "serviceAutoStartProvider"; - const string APPLICATIONPOOL_ATTRIBUTE = "applicationPool"; - const string PATH_ATTRIBUTE = "path"; - const string AUTOSTART_ATTRIBUTE = "autoStart"; - const string STARTMODE_ATTRIBUTE = "startMode"; - const string PRELOADENABLED_ATTRIBUTE = "preloadEnabled"; - bool writele = le == null; if (le == null) { le = new DCLogEntry(LogLevel.Information, $"{typeof(HangfireBootstrapper).FullName}.{System.Reflection.MethodBase.GetCurrentMethod().Name}"); }; try @@ -238,9 +221,6 @@ namespace Vrh.Web.HangfireBootstrapperNS string serviceAutoStartProviderTypeFullName = serviceAutoStartProviderType.FullName; string serviceAutoStartProviderAssassemblyName = serviceAutoStartProviderType.Assembly.GetName().Name; string configfilepath = Path.Combine(NativeSystemPath, CONFIGFILEDIRECTORY, CONFIGFILENAME); - var filecontent = System.IO.File.ReadAllText(configfilepath); - XElement configxml = XElement.Parse(filecontent, LoadOptions.PreserveWhitespace); //XElement.Load(configfilepath); - XElement myconfigrootxml = configxml.Element(XName.Get(SYSTEMAPPLICATIONHOST_ELEMENT)); le.AddDataField("SetupAutoStart configfilepath", configfilepath); le.AddDataField("SetupAutoStart applicationpoolname", applicationpoolname); @@ -251,50 +231,107 @@ namespace Vrh.Web.HangfireBootstrapperNS le.AddDataField("SetupAutoStart serviceAutoStartProviderTypeFullName", serviceAutoStartProviderTypeFullName); le.AddDataField("SetupAutoStart serviceAutoStartProviderAssassemblyName", serviceAutoStartProviderAssassemblyName); - XElement myapplicationpooladdelement = myconfigrootxml?.Element(XName.Get(APPLICATIONPOOLS_ELEMENT))? - .Elements(XName.Get(ADD_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(NAME_ATTRIBUTE)).Value == applicationpoolname); - XElement mysiteapplicationelement = myconfigrootxml?.Element(XName.Get(SITES_ELEMENT))? - .Elements(XName.Get(SITE_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(NAME_ATTRIBUTE)).Value == websitename) - .Elements(XName.Get(APPLICATION_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(PATH_ATTRIBUTE)).Value == webapplicationname && e.Attribute(XName.Get(APPLICATIONPOOL_ATTRIBUTE)).Value == applicationpoolname); - if (myapplicationpooladdelement == null) return; - if (mysiteapplicationelement == null) return; - - bool configchanged=false; - if (disablemode) - { - configchanged = SetOrAddAttribute(AUTOSTART_ATTRIBUTE, myapplicationpooladdelement, bool.FalseString.ToLower()) || configchanged; - configchanged = SetOrAddAttribute(STARTMODE_ATTRIBUTE, myapplicationpooladdelement, StartMode.OnDemand.ToString()) || configchanged; - configchanged = SetOrAddAttribute(SERVICEAUTOSTARTENABLED_ATTRIBUTE, mysiteapplicationelement, bool.FalseString.ToLower()) || configchanged; - configchanged = SetOrAddAttribute(PRELOADENABLED_ATTRIBUTE, mysiteapplicationelement, bool.FalseString.ToLower()) || configchanged; - configchanged = RemoveAttribute(SERVICEAUTOSTARTPROVIDER_ATTRIBUTE, mysiteapplicationelement) || configchanged; - - XElement myautostartproviderselement = myconfigrootxml.Element(XName.Get(SERVICEAUTOSTARTPROVIDERS_ELEMENT)); - configchanged = RemoveElementWithSelectorAttribute(ADD_ELEMENT, myautostartproviderselement, NAME_ATTRIBUTE, autostartprovidername,removecontainerifnochild:true) || configchanged; - } - else + string filecontent; + bool configchanged; + lock (lockerFile) { - configchanged = SetOrAddAttribute(AUTOSTART_ATTRIBUTE, myapplicationpooladdelement, bool.TrueString.ToLower()) || configchanged; - configchanged = SetOrAddAttribute(STARTMODE_ATTRIBUTE, myapplicationpooladdelement, StartMode.AlwaysRunning.ToString()) || configchanged; - configchanged = SetOrAddAttribute(SERVICEAUTOSTARTENABLED_ATTRIBUTE, mysiteapplicationelement, bool.TrueString.ToLower()) || configchanged; - configchanged = SetOrAddAttribute(PRELOADENABLED_ATTRIBUTE, mysiteapplicationelement, bool.TrueString.ToLower()) || configchanged; - configchanged = SetOrAddAttribute(SERVICEAUTOSTARTPROVIDER_ATTRIBUTE, mysiteapplicationelement, autostartprovidername) || configchanged; + bool usefilestream = false; + if (usefilestream) + { + FileStream file=null; StreamReader reader = null; StreamWriter writer = null; + try + { + int opentriescounter = 0; + while (true) + { + try { file = new FileStream(configfilepath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); break; } + catch + { + if (opentriescounter++ > 5) throw new Exception("ERROR! in accessing config file. Maybe it is open in other application!"); + Thread.Sleep(20); + } + } - configchanged = SetOrAddElement(SERVICEAUTOSTARTPROVIDERS_ELEMENT, myconfigrootxml, "", out XElement myautostartproviderselement) || configchanged; - configchanged = SetOrAddElementWithSelectorAttribute(ADD_ELEMENT, myautostartproviderselement, NAME_ATTRIBUTE, autostartprovidername, "", out XElement myautostartprovideraddelement) || configchanged; - configchanged = SetOrAddAttribute(TYPE_ATTRIBUTE, myautostartprovideraddelement, $"{serviceAutoStartProviderTypeFullName},{serviceAutoStartProviderAssassemblyName }") || configchanged; - } - - if (configchanged) - { - //configxml.Save(configfilepath); - System.IO.File.WriteAllText(configfilepath, configxml.ToString()); + reader = new StreamReader(file, Encoding.ASCII); + filecontent = ProcessFileContent(le, reader.ReadToEnd(), applicationpoolname, websitename, webapplicationname, autostartprovidername, serviceAutoStartProviderAssassemblyName, serviceAutoStartProviderTypeFullName, disablemode); + configchanged = filecontent != null; + if (configchanged) + { + writer = new StreamWriter(file, Encoding.ASCII); + writer.Write(filecontent); + } + } + finally { try { writer?.Dispose(); } catch { } try { reader?.Dispose();} catch { } try { file?.Dispose(); } catch { } } + } + else + { + filecontent = ProcessFileContent(le, System.IO.File.ReadAllText(configfilepath), applicationpoolname, websitename, webapplicationname, autostartprovidername, serviceAutoStartProviderAssassemblyName, serviceAutoStartProviderTypeFullName, disablemode); + configchanged = filecontent != null; + if (configchanged) System.IO.File.WriteAllText(configfilepath, filecontent); + } } string modetxt = disablemode ? "DISABLED" : "ENABLED"; - le.AddSuccessResult("SetupAutoStart " + (configchanged ? $"SUCCESSFULLY {modetxt}" : $"NO CHANGE, ALREADY {modetxt}")); + le.AddDataField("SetupAutoStart SUCCESS RESULT",configchanged ? modetxt : $"NO CHANGE, ALREADY {modetxt}"); } - catch (Exception ex) { le.AddExceptionResult(ex); le.SetLogLevel(LogLevel.Error); return; } + catch (Exception ex) { le.AddDataField("SetupAutoStart EXCEPTION",ex.Message); le.SetLogLevel(LogLevel.Error); return; } finally { if (writele) le.Write(); } } + private static string ProcessFileContent(DCLogEntry le, string filecontent,string applicationpoolname,string websitename,string webapplicationname,string autostartprovidername,string serviceAutoStartProviderAssassemblyName, string serviceAutoStartProviderTypeFullName,bool disablemode) + { + const string SYSTEMAPPLICATIONHOST_ELEMENT = "system.applicationHost"; + const string APPLICATIONPOOLS_ELEMENT = "applicationPools"; + const string SITES_ELEMENT = "sites"; + const string SERVICEAUTOSTARTPROVIDERS_ELEMENT = "serviceAutoStartProviders"; + const string SITE_ELEMENT = "site"; + const string APPLICATION_ELEMENT = "application"; + const string ADD_ELEMENT = "add"; + const string NAME_ATTRIBUTE = "name"; + const string TYPE_ATTRIBUTE = "type"; + const string SERVICEAUTOSTARTENABLED_ATTRIBUTE = "serviceAutoStartEnabled"; + const string SERVICEAUTOSTARTPROVIDER_ATTRIBUTE = "serviceAutoStartProvider"; + const string APPLICATIONPOOL_ATTRIBUTE = "applicationPool"; + const string PATH_ATTRIBUTE = "path"; + const string AUTOSTART_ATTRIBUTE = "autoStart"; + const string STARTMODE_ATTRIBUTE = "startMode"; + const string PRELOADENABLED_ATTRIBUTE = "preloadEnabled"; + + XElement configxml = XElement.Parse(filecontent, LoadOptions.PreserveWhitespace); + XElement myconfigrootxml = configxml.Element(XName.Get(SYSTEMAPPLICATIONHOST_ELEMENT)); + + XElement myapplicationpooladdelement = myconfigrootxml?.Element(XName.Get(APPLICATIONPOOLS_ELEMENT))? + .Elements(XName.Get(ADD_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(NAME_ATTRIBUTE)).Value == applicationpoolname); + XElement mysiteapplicationelement = myconfigrootxml?.Element(XName.Get(SITES_ELEMENT))? + .Elements(XName.Get(SITE_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(NAME_ATTRIBUTE)).Value == websitename) + .Elements(XName.Get(APPLICATION_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(PATH_ATTRIBUTE)).Value == webapplicationname && e.Attribute(XName.Get(APPLICATIONPOOL_ATTRIBUTE)).Value == applicationpoolname); + if (myapplicationpooladdelement == null || mysiteapplicationelement == null) return null; + + bool configchanged = false; + if (disablemode) + { + configchanged = SetOrAddAttribute(AUTOSTART_ATTRIBUTE, myapplicationpooladdelement, bool.FalseString.ToLower()) || configchanged; + configchanged = SetOrAddAttribute(STARTMODE_ATTRIBUTE, myapplicationpooladdelement, StartMode.OnDemand.ToString()) || configchanged; + configchanged = SetOrAddAttribute(SERVICEAUTOSTARTENABLED_ATTRIBUTE, mysiteapplicationelement, bool.FalseString.ToLower()) || configchanged; + configchanged = SetOrAddAttribute(PRELOADENABLED_ATTRIBUTE, mysiteapplicationelement, bool.FalseString.ToLower()) || configchanged; + configchanged = RemoveAttribute(SERVICEAUTOSTARTPROVIDER_ATTRIBUTE, mysiteapplicationelement) || configchanged; + + XElement myautostartproviderselement = myconfigrootxml.Element(XName.Get(SERVICEAUTOSTARTPROVIDERS_ELEMENT)); + configchanged = RemoveElementWithSelectorAttribute(ADD_ELEMENT, myautostartproviderselement, NAME_ATTRIBUTE, autostartprovidername, removecontainerifnochild: true) || configchanged; + } + else + { + configchanged = SetOrAddAttribute(AUTOSTART_ATTRIBUTE, myapplicationpooladdelement, bool.TrueString.ToLower()) || configchanged; + configchanged = SetOrAddAttribute(STARTMODE_ATTRIBUTE, myapplicationpooladdelement, StartMode.AlwaysRunning.ToString()) || configchanged; + configchanged = SetOrAddAttribute(SERVICEAUTOSTARTENABLED_ATTRIBUTE, mysiteapplicationelement, bool.TrueString.ToLower()) || configchanged; + configchanged = SetOrAddAttribute(PRELOADENABLED_ATTRIBUTE, mysiteapplicationelement, bool.TrueString.ToLower()) || configchanged; + configchanged = SetOrAddAttribute(SERVICEAUTOSTARTPROVIDER_ATTRIBUTE, mysiteapplicationelement, autostartprovidername) || configchanged; + + configchanged = SetOrAddElement(SERVICEAUTOSTARTPROVIDERS_ELEMENT, myconfigrootxml, "", out XElement myautostartproviderselement) || configchanged; + configchanged = SetOrAddElementWithSelectorAttribute(ADD_ELEMENT, myautostartproviderselement, NAME_ATTRIBUTE, autostartprovidername, "", out XElement myautostartprovideraddelement) || configchanged; + configchanged = SetOrAddAttribute(TYPE_ATTRIBUTE, myautostartprovideraddelement, $"{serviceAutoStartProviderTypeFullName},{serviceAutoStartProviderAssassemblyName }") || configchanged; + } + return configxml.ToString(); + } + private static object lockerFile = new Object(); private static bool RemoveAttribute(string attributetoremovename, XElement attrcontainer) { XAttribute attr = attrcontainer.Attribute(XName.Get(attributetoremovename)); -- libgit2 0.21.2