// ReSharper restore FunctionNeverReturns private void CheckChange(DynamicChangesMonitorInfo monitorInfo, ICollection <string> modifiedResourcePaths, Func <Resource, bool> resourceTypeFunc) { var resourcesToCheck = monitorInfo.DynamicResourcesToWatch.Where(resourceTypeFunc); foreach (var resource in resourcesToCheck) { var cachedContent = resource.ReadFromCache(false); /* * If content is null, means two things: * - No request ever made to that dynamic resource by Combres * Combres will make such request anyway, so there's no need to post a change here. * - The resource doesn't exist anymore in the latest Setting object, * thus a change should not be posted here. */ if (cachedContent == null) { continue; } /* * URL of local dynamic resources (i.e. starting with ~) must be resolved * with a reference to HttpContext.Current, which does not really exist * because we're in a custom thread. This injects a fake context which * has enough information for the URL resolution to work. */ HttpContext.Current = fakeContext; var newContent = resource.ReadNewContent(); if (!cachedContent.Equals(newContent)) { modifiedResourcePaths.Add(resource.Path); } } }
public DynamicChangesMonitorInfo(DynamicChangesMonitorInfo other) { DynamicResourcesToWatch = other.DynamicResourcesToWatch; LocalChangeMonitorInterval = other.LocalChangeMonitorInterval; RemoteChangeMonitorInterval = other.RemoteChangeMonitorInterval; }
private void MonitorDynamicChanges(object state) { if (Log.IsDebugEnabled) { Log.Debug("Dynamic resource change monitor thread starts..."); } long localIntervalElapsed = 0; long remoteIntervalElapsed = 0; var watch = new Stopwatch(); while (true) { try { watch.Start(); DynamicChangesMonitorInfo tmpMonitorInfo; lock (currentMonitorInfo) { /* * local.DynamicResourcesToWatch may be referring to an old Setting object here. * The code should deal with that accordingly. */ tmpMonitorInfo = new DynamicChangesMonitorInfo(currentMonitorInfo); } bool nothingToWatch = false; if (tmpMonitorInfo.DynamicResourcesToWatch.Count == 0 || (tmpMonitorInfo.LocalChangeMonitorInterval == null && tmpMonitorInfo.RemoteChangeMonitorInterval == null)) { nothingToWatch = true; } else { var modifiedResourcePaths = new List <string>(); var localCheckShouldRun = tmpMonitorInfo.LocalChangeMonitorInterval != null && localIntervalElapsed >= tmpMonitorInfo.LocalChangeMonitorInterval; var remoteCheckShouldRun = tmpMonitorInfo.RemoteChangeMonitorInterval != null && remoteIntervalElapsed >= tmpMonitorInfo.RemoteChangeMonitorInterval; if (localCheckShouldRun || remoteCheckShouldRun) { if (localCheckShouldRun) { if (Log.IsDebugEnabled) { Log.Debug("Checking changes to local dynamic resources..."); } CheckChange(tmpMonitorInfo, modifiedResourcePaths, r => r.IsInSameApplication); localIntervalElapsed = 0; } if (remoteCheckShouldRun) { if (Log.IsDebugEnabled) { Log.Debug("Checking changes to remote dynamic resources..."); } CheckChange(tmpMonitorInfo, modifiedResourcePaths, r => !r.IsInSameApplication); remoteIntervalElapsed = 0; } if (modifiedResourcePaths.Count > 0) { if (Log.IsDebugEnabled) { Log.Debug("Dynamic resources change count: " + modifiedResourcePaths.Count); } OnChange(ChangeType.Resource, modifiedResourcePaths); } } } // nothingToWatch is a status that won't change unless there's a configuration change // so a reasonably long sleep till the next check shouldn't hurt. // Otherwise, sleep for 1 second, which is the smallest value that can be specified for an interval. Thread.Sleep(nothingToWatch ? 5000 : 1000); // Accummulate elapsed periods watch.Stop(); var elapsed = watch.ElapsedMilliseconds; localIntervalElapsed += elapsed; remoteIntervalElapsed += elapsed; watch.Reset(); } catch (Exception ex) { if (Log.IsWarnEnabled) { Log.Warn("Error in dynamic resource monitor thread", ex); } } } // ReSharper disable FunctionNeverReturns }
public DynamicChangesMonitorInfo(DynamicChangesMonitorInfo other) { DynamicResourcesToWatch = other.DynamicResourcesToWatch; LocalChangeMonitorInterval = other.LocalChangeMonitorInterval; RemoteChangeMonitorInterval = other.RemoteChangeMonitorInterval; }
// ReSharper restore FunctionNeverReturns private void CheckChange(DynamicChangesMonitorInfo monitorInfo, ICollection<string> modifiedResourcePaths, Func<Resource, bool> resourceTypeFunc) { var resourcesToCheck = monitorInfo.DynamicResourcesToWatch.Where(resourceTypeFunc); foreach (var resource in resourcesToCheck) { var cachedContent = resource.ReadFromCache(false); /* * If content is null, means two things: * - No request ever made to that dynamic resource by Combres * Combres will make such request anyway, so there's no need to post a change here. * - The resource doesn't exist anymore in the latest Setting object, * thus a change should not be posted here. */ if (cachedContent == null) continue; /* * URL of local dynamic resources (i.e. starting with ~) must be resolved * with a reference to HttpContext.Current, which does not really exist * because we're in a custom thread. This injects a fake context which * has enough information for the URL resolution to work. */ HttpContext.Current = fakeContext; var newContent = resource.ReadNewContent(); if (!cachedContent.Equals(newContent)) { modifiedResourcePaths.Add(resource.Path); } } }
private void MonitorDynamicChanges(object state) { if (Log.IsDebugEnabled) Log.Debug("Dynamic resource change monitor thread starts..."); long localIntervalElapsed = 0; long remoteIntervalElapsed = 0; var watch = new Stopwatch(); while (true) { try { watch.Start(); DynamicChangesMonitorInfo tmpMonitorInfo; lock (currentMonitorInfo) { /* * local.DynamicResourcesToWatch may be referring to an old Setting object here. * The code should deal with that accordingly. */ tmpMonitorInfo = new DynamicChangesMonitorInfo(currentMonitorInfo); } bool nothingToWatch = false; if (tmpMonitorInfo.DynamicResourcesToWatch.Count == 0 || (tmpMonitorInfo.LocalChangeMonitorInterval == null && tmpMonitorInfo.RemoteChangeMonitorInterval == null)) { nothingToWatch = true; } else { var modifiedResourcePaths = new List<string>(); var localCheckShouldRun = tmpMonitorInfo.LocalChangeMonitorInterval != null && localIntervalElapsed >= tmpMonitorInfo.LocalChangeMonitorInterval; var remoteCheckShouldRun = tmpMonitorInfo.RemoteChangeMonitorInterval != null && remoteIntervalElapsed >= tmpMonitorInfo.RemoteChangeMonitorInterval; if (localCheckShouldRun || remoteCheckShouldRun) { if (localCheckShouldRun) { if (Log.IsDebugEnabled) Log.Debug("Checking changes to local dynamic resources..."); CheckChange(tmpMonitorInfo, modifiedResourcePaths, r => r.IsInSameApplication); localIntervalElapsed = 0; } if (remoteCheckShouldRun) { if (Log.IsDebugEnabled) Log.Debug("Checking changes to remote dynamic resources..."); CheckChange(tmpMonitorInfo, modifiedResourcePaths, r => !r.IsInSameApplication); remoteIntervalElapsed = 0; } if (modifiedResourcePaths.Count > 0) { if (Log.IsDebugEnabled) Log.Debug("Dynamic resources change count: " + modifiedResourcePaths.Count); OnChange(ChangeType.Resource, modifiedResourcePaths); } } } // nothingToWatch is a status that won't change unless there's a configuration change // so a reasonably long sleep till the next check shouldn't hurt. // Otherwise, sleep for 1 second, which is the smallest value that can be specified for an interval. Thread.Sleep(nothingToWatch ? 5000 : 1000); // Accummulate elapsed periods watch.Stop(); var elapsed = watch.ElapsedMilliseconds; localIntervalElapsed += elapsed; remoteIntervalElapsed += elapsed; watch.Reset(); } catch (Exception ex) { if (Log.IsWarnEnabled) Log.Warn("Error in dynamic resource monitor thread", ex); } } // ReSharper disable FunctionNeverReturns }