public async Task <bool> PostResult(ExperimentResult result) { Log.Debug($"ExperimentClient: Posting results for experiment #{result.ExperimentId}..."); try { var jsonBody = JsonConvert.SerializeObject(result, new StringEnumConverter()); var httpContent = new StringContent(jsonBody, Encoding.UTF8, "application/json"); var httpResponse = await _client.PostAsync(API_POST_RESULT, httpContent); if (!httpResponse.IsSuccessStatusCode) { Log.Warning( $"ExperimentClient: Server returned error code after posting: " + $"{(int)httpResponse.StatusCode} ({httpResponse.ReasonPhrase}); Content: {await httpResponse.Content.ReadAsStringAsync()}"); } else { return(true); } } catch (HttpRequestException ex) { Log.Error("ExperimentClient: Post failed due to network issues: " + ex.Message); } catch (Exception ex) { Log.Error("ExperimentClient: Post failed: " + ex.Message); } return(false); }
private async void ReportResult(ExperimentRuntimeResult runtimeResult) { _experimentTimeLimit?.Stop(); try { ExperimentResult result; if (_activeExperiment != null) { result = new ExperimentResult() { Environment = CurrentEnvironment(), ExperimentId = _activeExperiment.Id, Result = runtimeResult.Result, ResultCode = runtimeResult.ResultCode, ResultCodeString = runtimeResult.ResultCodeString ?? (runtimeResult.ResultCode == 0 ? "PASS" : "FAIL") }; } else { Log.Warning( "ExperimentRuntime.ReportResult: ActiveExperiment is null. Cannot send report and exclude"); return; } Log.Debug($"ExperimentRuntime: Experiment finished with result code {runtimeResult.ResultCode}"); if (_activeExperimentHook != null) { _activeExperimentHook.Finished -= ReportResult; ScriptManager.Instance.UnregisterHook(_activeExperimentHook); Log.Debug("ExperimentRuntime: Experiment unhooked"); } else { Log.Warning("ExperimentRuntime: Experiment not unhooked; hook reference is already null"); } await _client.PostResult(result); ExcludeExperiment(_activeExperiment.Id); } catch (Exception ex) { Log.Error("ExperimentManager: Error while posting results: " + ex); SentrySdk.CaptureException(ex); } NextExperiment(); }