private async Task SaveIncidentAsync(int componentId, IWatcherCheckResult result, bool notify = false) { var date = _configuration.DateTimeProvider().Date; var componentStatus = result.IsValid ? ComponentStatus.Operational : ComponentStatus.MajorOutage; var incidentStatus = result.IsValid ? IncidentStatus.Fixed : IncidentStatus.Identified; var name = $"{result.WatcherName} check is {(result.IsValid ? "valid" : "invalid")}"; var incidents = await _cachetService.GetIncidentsAsync(componentId); var incident = incidents.FirstOrDefault(x => x.ComponentId == componentId && x.CreatedAt?.Date == date); var message = result.Description; if (incident == null) { incident = await _cachetService.CreateIncidentAsync(name, message, incidentStatus, notify : notify, componentId : componentId, componentStatus : componentStatus); } else { incident = await _cachetService.UpdateIncidentAsync(incident.Id, name, message, incidentStatus, notify : notify, componentId : componentId, componentStatus : componentStatus); } }
protected WardenCheckResult(IWatcherCheckResult watcherCheckResult, DateTime startedAt, DateTime completedAt, Exception exception = null) { if (watcherCheckResult == null) throw new ArgumentNullException(nameof(watcherCheckResult), "Watcher check result can not be null."); WatcherCheckResult = watcherCheckResult; StartedAt = startedAt; CompletedAt = completedAt; Exception = exception; }
protected WardenCheckResult(IWatcherCheckResult watcherCheckResult, DateTime startedAt, DateTime completedAt, Exception exception = null) { if (watcherCheckResult == null) { throw new ArgumentNullException(nameof(watcherCheckResult), "Watcher check result can not be null."); } WatcherCheckResult = watcherCheckResult; StartedAt = startedAt; CompletedAt = completedAt; Exception = exception; }
private async Task SaveWatcherCheckResultAsync(IWatcherCheckResult result, long wardenCheckResultId) { if (result == null) { return; } var watcherCheckResultCommand = "insert into WatcherCheckResults values " + "(@wardenCheckResult_Id, @watcherName, @watcherType, @description, @isValid)"; var watcherCheckResultParameters = new Dictionary <string, object> { ["wardenCheckResult_Id"] = wardenCheckResultId, ["watcherName"] = result.WatcherName, ["watcherType"] = result.WatcherType.ToString().Split('.').Last(), ["description"] = result.Description, ["isValid"] = result.IsValid }; await ExecuteAsync(watcherCheckResultCommand, watcherCheckResultParameters); }
private async Task SaveIncidentAsync(int componentId, IWatcherCheckResult result, bool notify = false, bool saveValidIncident = false, bool updateIfStatusIsTheSame = false) { var date = _configuration.DateTimeProvider().Date; var componentStatus = result.IsValid ? ComponentStatus.Operational : ComponentStatus.MajorOutage; var incidentStatus = result.IsValid ? IncidentStatus.Fixed : IncidentStatus.Identified; var incidents = await _cachetService.GetIncidentsAsync(componentId); var existingIncidentStatus = incidents.FirstOrDefault(x => x.ComponentId == componentId)?.Status; //If there's neither failure nor previous incident reported, do not report a valid service check. if (!saveValidIncident && result.IsValid && existingIncidentStatus == null) { return; } if (!updateIfStatusIsTheSame && existingIncidentStatus == incidentStatus) { return; } var name = $"{result.WatcherName} check is {(result.IsValid ? "valid" : "invalid")}"; var incident = incidents.FirstOrDefault(x => x.ComponentId == componentId && x.CreatedAt?.Date == date); var message = result.Description; if (incident == null) { incident = await _cachetService.CreateIncidentAsync(name, message, incidentStatus, notify : notify, componentId : componentId, componentStatus : componentStatus); } else { incident = await _cachetService.UpdateIncidentAsync(incident.Id, name, message, incidentStatus, notify : notify, componentId : componentId, componentStatus : componentStatus); } }
private async Task ExecuteWatcherCheckAsync(IWatcher watcher, IWatcherCheckResult watcherCheckResult, IWardenCheckResult wardenCheckResult, WatcherConfiguration watcherConfiguration, IList <WatcherExecutionResult> results) { if (watcherCheckResult.IsValid) { var result = wardenCheckResult; results.Add(new WatcherExecutionResult(watcher, WatcherResultState.Success, GetPreviousWatcherState(watcher), result)); await UpdateWatcherResultStateAndExecuteHooksPossibleAsync(watcher, WatcherResultState.Success, () => InvokeOnFirstSuccessHooksAsync(watcherConfiguration, result), executeIfLatestStateIsNotSet : false); await InvokeOnSuccessHooksAsync(watcherConfiguration, wardenCheckResult); } else { var result = wardenCheckResult; results.Add(new WatcherExecutionResult(watcher, WatcherResultState.Failure, GetPreviousWatcherState(watcher), result)); await UpdateWatcherResultStateAndExecuteHooksPossibleAsync(watcher, WatcherResultState.Failure, () => InvokeOnFirstFailureHooksAsync(watcherConfiguration, result)); await InvokeOnFailureHooksAsync(watcherConfiguration, wardenCheckResult); } }
private async Task<IList<WatcherExecutionResult>> ExecuteWatcherCheckAsync(IWatcher watcher, IWatcherCheckResult watcherCheckResult, IWardenCheckResult wardenCheckResult, WatcherConfiguration watcherConfiguration) { var results = new List<WatcherExecutionResult>(); if (watcherCheckResult.IsValid) { var result = wardenCheckResult; results.Add(new WatcherExecutionResult(watcher, WatcherResultState.Success, GetPreviousWatcherState(watcher), result)); await UpdateWatcherResultStateAndExecuteHooksPossibleAsync(watcher, WatcherResultState.Success, () => InvokeOnFirstSuccessHooksAsync(watcherConfiguration, result), executeIfLatestStateIsNotSet: false); await InvokeOnSuccessHooksAsync(watcherConfiguration, wardenCheckResult); } else { var result = wardenCheckResult; results.Add(new WatcherExecutionResult(watcher, WatcherResultState.Failure, GetPreviousWatcherState(watcher), result)); await UpdateWatcherResultStateAndExecuteHooksPossibleAsync(watcher, WatcherResultState.Failure, () => InvokeOnFirstFailureHooksAsync(watcherConfiguration, result)); await InvokeOnFailureHooksAsync(watcherConfiguration, wardenCheckResult); } return results; }
/// <summary> /// Factory method for creating a new instance of IWardenCheckResult. /// </summary> /// <returns>Instance of IWardenCheckResult.</returns> public static IWardenCheckResult Create(IWatcherCheckResult watcherCheckResult, DateTime startedAt, DateTime completedAt, Exception exception = null) => new WardenCheckResult(watcherCheckResult, startedAt, completedAt, exception);