/// <summary> /// Configures cache warmup. /// </summary> /// <param name="app">The app.</param> /// <param name="createContext">The context generator.</param> /// <param name="settings">The settings.</param> public static void WarmupCache(this IAppBuilder app, Func <CrmDbContext> createContext, WarmupCacheSettings settings) { var json = CrmJsonConvert.SerializeObject(settings); ADXTrace.Instance.TraceInfo(TraceCategory.Application, json); if (settings.IsEnabled) { if (settings.AsyncWarmupEnabled) { // run warmup job as a scheduled job var registry = new Registry(); registry.Schedule(() => { new WarmupCacheJob(createContext(), settings).Execute(); }) .ToRunOnceIn(settings.AsyncWarmupDelay).Seconds(); JobManager.Initialize(registry); } else { try { // run warmup job synchronously new WarmupCacheJob(createContext(), settings).Execute(); } catch (Exception e) { WebEventSource.Log.GenericErrorException(e); } } // register application shutdown job if (settings.PersistOnAppDisposing) { var properties = new AppProperties(app.Properties); var token = properties.OnAppDisposing; if (token != CancellationToken.None) { token.Register(() => ExecutePersistCachedRequestsJob(settings)); } } // register scheduled job if (settings.PersistOnSchedule) { var registry = new Registry(); registry.Schedule(() => { new PersistCachedRequestsJob(GetCache("Xrm"), settings).Execute(); }) .Reentrant(settings.Reentrant) .ToRunNow().AndEvery(settings.JobInterval).Seconds(); JobManager.Initialize(registry); } } }
private static JObject GetJsonContent(string name, CacheItemDetail detail, CacheItemTelemetry telemetry, object value, Uri removeLink, bool expanded) { var policyContent = GetJsonPolicyContent(detail); var telemetryContent = telemetry != null ? new JObject { { "memberName", telemetry.Caller.MemberName }, { "sourceFilePath", telemetry.Caller.SourceFilePath }, { "sourceLineNumber", telemetry.Caller.SourceLineNumber }, { "duration", telemetry.Duration }, { "accessCount", telemetry.AccessCount }, { "cacheItemStatus", detail.CacheItemStatus.ToString() }, { "staleAccessCount", telemetry.StaleAccessCount }, { "lastAccessedOn", telemetry.LastAccessedOn }, { "isStartup", telemetry.IsStartup }, { "isAllColumns", telemetry.IsAllColumns }, { "attributes", new JArray(telemetry.Attributes) } } : null; var properties = new JObject { { "name", name }, { "type", value != null?value.GetType().ToString() : null }, { "remove", removeLink.AbsoluteUri } }; if (policyContent != null) { properties.Add("policy", policyContent); } if (telemetryContent != null) { properties.Add("telemetry", telemetryContent); } if (expanded) { if (value != null) { properties.Add("value", JToken.FromObject(value, CrmJsonConvert.CreateJsonSerializer())); } if (telemetry != null && telemetry.Request != null) { properties.Add("request", JToken.FromObject(telemetry.Request, CrmJsonConvert.CreateJsonSerializer())); } } var content = new JObject { { "properties", properties } }; return(content); }
/// <summary> /// The body. /// </summary> /// <param name="id">The activity id.</param> protected override void ExecuteInternal(Guid id) { var exceptions = new List <Exception>(); using (PerformanceProfiler.Instance.StartMarker(PerformanceMarkerName.Cache, PerformanceMarkerArea.Cms, PerformanceMarkerTagName.WarmupCache)) { if (!this.Settings.AppDataRetryPolicy.DirectoryExists(this.AppDataFullPath)) { return; } var directory = this.Settings.AppDataRetryPolicy.GetDirectory(this.AppDataFullPath); var files = directory.GetFiles(this.Settings.FilenameFormat.FormatWith("*")); var expiresOn = DateTimeOffset.UtcNow - this.Settings.ExpirationWindow; foreach (var file in files) { try { var expired = file.LastWriteTimeUtc < expiresOn; if (!expired) { ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Reading: " + file.FullName); var bytes = this.Settings.AppDataRetryPolicy.ReadAllBytes(file.FullName); var json = Encoding.UTF8.GetString(MachineKey.Unprotect(bytes, this.Settings.GetType().ToString())); var request = CrmJsonConvert.DeserializeObject(json) as OrganizationRequest; if (request != null) { this.Context.Service.ExecuteRequest(request); } } } catch (Exception e) { WebEventSource.Log.GenericWarningException(e); if (this.Settings.PropagateExceptions) { exceptions.Add(e); } } } } if (this.Settings.PropagateExceptions && exceptions.Any()) { throw new AggregateException(exceptions); } }
/// <summary> /// Write this item to a folder. /// </summary> /// <param name="folderPath">The folder path.</param> /// <param name="telemetry">The cache item telemetry.</param> private void Save(string folderPath, CacheItemTelemetry telemetry) { if (telemetry.Request != null) { var request = CrmJsonConvert.SerializeObject(telemetry.Request); var key = request.GetHashCode(); var filename = string.Format(this.Settings.FilenameFormat, key); var fullPath = Path.Combine(folderPath, filename); var bytes = MachineKey.Protect(Encoding.UTF8.GetBytes(request), this.Settings.GetType().ToString()); ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Writing: " + fullPath); this.Settings.AppDataRetryPolicy.WriteAllBytes(fullPath, bytes); } }