public virtual void RecieveAsyncExecutionResponse(AsyncExecutionResponse response) // called by the server side to send responses { Args.ThrowIfNull(response, "result"); Args.ThrowIfNull(response.Request, "result.Request"); string cuid = response.Request.Cuid; Action <AsyncExecutionResponse> action = ((r) => { }); if (_pendingRequests.ContainsKey(cuid)) { if (!_pendingRequests.TryRemove(cuid, out action)) { FireEvent(RemovePendingFailed, new AsyncExecutionResponseEventArgs { Response = response }); } } else { Logger.Warning("Received AsyncExecutionResponse with no corresponding request: {0}", response.PropertiesToString()); } Task.Run(() => { SaveResponseData(response); action(response); }); }
private void SaveResponseData(AsyncExecutionResponse response, int retryCount = 5) { Args.ThrowIfNull(response, "response"); Args.ThrowIfNull(response.Request, "response.Request"); response.ResultJson = response.Result?.ToJson() ?? ""; string responseHash = response.ResultJson.Sha256(); AsyncExecutionData executionData = AsyncCallbackRepository.OneAsyncExecutionDataWhere(c => c.RequestCuid == response.Request.Cuid); if (executionData == null) { Logger.Warning("Recieved response without corresponding ASYNCEXECUTIONDATA entry: \r\n{0}", response.PropertiesToString()); } else { executionData.ResponseCuid = response.Cuid; executionData.Responded = new Instant(DateTime.UtcNow); executionData.ResponseHash = responseHash; executionData.Success = true; AsyncCallbackRepository.Save(executionData); } AsyncExecutionRequestData requestData = AsyncCallbackRepository.OneAsyncExecutionRequestDataWhere(c => c.Cuid == response.Request.Cuid); if (requestData == null) { Thread.Sleep(100); if (retryCount > 0) { Logger.Warning("Received response without corresponding ASYNCEXECUTIONREQUESTDATA entry (retry count={0}): \r\n{1}", retryCount, response.PropertiesToString()); SaveResponseData(response, --retryCount); } return; } AsyncExecutionResponseData responseData = AsyncCallbackRepository.OneAsyncExecutionResponseDataWhere(c => c.Cuid == response.Cuid); if (responseData != null) { Logger.Warning("Received response that has already been recorded: {0}", response.PropertiesToString()); } else { responseData = new AsyncExecutionResponseData { RequestId = requestData.Id, ResultJson = response.ResultJson, ResponseHash = responseHash, RequestHash = requestData.RequestHash }; AsyncCallbackRepository.Save(responseData); } }