private async Task<bool> registerResourcesToWatchdog(CancellationToken cancellationToken) { try { var listResources = await StateManagerHelper.GetExternalResourcesAsync(this.StateManager); var updateResources = listResources.Where(p => p.IsUpdate).ToList(); List<Task> listTaskRegister = new List<Task>(); for (int i = 0; i < updateResources.Count; i++) { var rs = listResources[i]; listTaskRegister.Add(new Task(async () => { IReliableDictionary<string, ExternalResource> rsDict = await StateManagerHelper.GetExternalResourceDictAsync(this.StateManager); try { await WatchdogHelper.RegisterHealthCheckAsync(rs.RowKey, this.Context.ServiceName, this.Context.PartitionId, rs.Frequency, $"api/health/{rs.RowKey}", cancellationToken); rs.IsUpdate = false; using (ITransaction tx = this.StateManager.CreateTransaction()) { await rsDict.AddOrUpdateAsync(tx, rs.RowKey, rs, (k, v) => { return rs; }, TimeSpan.FromSeconds(5), CancellationToken.None); await tx.CommitAsync().ConfigureAwait(false); } } catch (Exception ex) { ServiceEventSource.Current.Exception($"Resource {rs.RowKey} register failed", ex.Message, ex.StackTrace); } })); } listTaskRegister.ForEach(task => { task.Start(); }); Task.WaitAll(listTaskRegister.ToArray()); return true; } catch (Exception ex) { ServiceEventSource.Current.Exception("Register resources failed", ex.Message, ex.StackTrace); return false; } }
private async Task <bool> RegisterHealCheckActorAsync(CancellationToken cancellationToken) { return(await WatchdogHelper.RegisterHealthCheckAsync("WDActor", this.Context.ServiceName, this.Context.PartitionId, 2, "api/ActorHealth/abc", cancellationToken)); //bool result = false; //HttpClient client = new HttpClient(); //string jsonTemplate = // "{{\"name\":\"WDActor\",\"serviceName\": \"{0}\",\"partition\": \"{1}\",\"frequency\": \"{2}\",\"suffixPath\": \"api/ActorHealth/abc\",\"method\": {{ \"Method\": \"GET\" }}, \"expectedDuration\": \"00:00:00.2000000\",\"maximumDuration\": \"00:00:05\" }}"; //string json = string.Format(jsonTemplate, this.Context.ServiceName, this.Context.PartitionId, TimeSpan.FromMinutes(2)); //// Called from RunAsync, don't let an exception out so the service will start, but log the exception because the service won't work. //try //{ // HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:19081/Watchdog/WatchdogService/api/healthcheck"); // request.Content = new StringContent(json, Encoding.Default, "application/json"); // HttpResponseMessage msg = await client.SendAsync(request); // // Log a success or error message based on the returned status code. // if (HttpStatusCode.OK == msg.StatusCode) // { // ServiceEventSource.Current.Message(string.Format("{0} - {1}", nameof(this.RegisterHealthCheckAsync), Enum.GetName(typeof(HttpStatusCode), msg.StatusCode))); // //ServiceEventSource.Current.Trace(nameof(this.RegisterHealthCheckAsync), Enum.GetName(typeof(HttpStatusCode), msg.StatusCode)); // result = true; // } // else // { // ServiceEventSource.Current.Message(string.Format("Error: {0} - {1}", nameof(this.RegisterHealthCheckAsync), Enum.GetName(typeof(HttpStatusCode), msg.StatusCode))); // //ServiceEventSource.Current.Error(nameof(this.RegisterHealthCheckAsync), Enum.GetName(typeof(HttpStatusCode), msg.StatusCode)); // //ServiceEventSource.Current.Trace(nameof(this.RegisterHealthCheckAsync), json ?? "<null JSON>"); // } //} //catch (Exception ex) //{ // ServiceEventSource.Current.Message(string.Format("Exception: {0} ", ex.Message)); // //ServiceEventSource.Current.Error($"Exception: {ex.Message} at {ex.StackTrace}."); //} //return result; }
private async Task <bool> RegisterHealthCheckAsync(CancellationToken cancellationToken) { return(await WatchdogHelper.RegisterHealthCheckAsync("WDStateless", this.Context.ServiceName, this.Context.PartitionId, 2, "api/values", cancellationToken)); }