private void UploadRequest(HubRequest request, Lib.Caching.DocumentCacheManager cache) { string requestId = request.Source.ID.ToString(); Document[] responseDocuments; if (cache.Enabled) { responseDocuments = cache.GetResponseDocuments().ToArray(); } else { responseDocuments = request.Processor.Response(requestId); } if (responseDocuments != null && responseDocuments.Length > 0) { Log.Info(string.Format("Uploading {4} documents for query: {0} (DataMart:{1}, Network:{2}, RequestID: {3:D})", request.Source.Identifier, request.DataMartName, request.NetworkName, request.Source.ID, responseDocuments.Length)); //since the ID of the document returned from the API is not known when posting, and the order cannot be assumed in the response //loop over each document and post the metadata and the content as a single unit of work per document. for (int i = 0; i < responseDocuments.Length; i++) { string uploadIdentifier = ("[" + Utilities.Crypto.Hash(Guid.NewGuid()) + "]").PadRight(16); Guid[] metaDataPostResponse = DnsServiceManager.PostResponseDocuments(uploadIdentifier, requestId, request.DataMartId, new[] { responseDocuments[i] }, _networkSetting); System.IO.Stream contentStream = null; try { if (cache.Enabled) { contentStream = cache.GetDocumentStream(Guid.Parse(responseDocuments[i].DocumentID)); } else { request.Processor.ResponseDocument(requestId, responseDocuments[i].DocumentID, out contentStream, 60000); } DnsServiceManager.PostResponseDocumentContent(uploadIdentifier, requestId, request.DataMartId, metaDataPostResponse[0], responseDocuments[i].Filename, contentStream, _networkSetting) .LogExceptions(Log.Error) .Catch() .Wait(); } finally { contentStream.CallDispose(); } } } else { Log.Info(string.Format("No documents to upload for query: {0} (DataMart:{1}, Network:{2}, RequestID: {3:D})", request.Source.Identifier, request.DataMartName, request.NetworkName, request.Source.ID, responseDocuments.Length)); } if (request.RoutingStatus == Lpp.Dns.DTO.DataMartClient.Enums.DMCRoutingStatus.Failed) { HubRequestStatus hubRequestStatus = DnsServiceManager.ConvertModelRequestStatus(request.Processor.Status(requestId)); hubRequestStatus.Message = request.Processor.Status(requestId).Message; DnsServiceManager.SetRequestStatus(request, hubRequestStatus, request.Properties, _networkSetting); Log.Info(string.Format("The following query {0} (ID: {3}, DataMart: {4}, Network: {5}) failed to execute on behalf of {1}:\n\n{2}", request.Source.Identifier, _networkSetting.Profile.Username, hubRequestStatus.Message, request.Source.ID, request.DataMartName, request.NetworkName)); } else { Log.Info(string.Format("The following query {0} (ID: {2}, DataMart: {3}, Network: {4}) was executed and results were uploaded automatically on behalf of {1}", request.Source.Identifier, _networkSetting.Profile.Username, request.Source.ID, request.DataMartName, request.NetworkName)); } }
private void UploadRequest(HubRequest request, Lib.Caching.DocumentCacheManager cache) { string requestId = request.Source.ID.ToString(); Document[] responseDocuments; if (cache.Enabled) { responseDocuments = cache.GetResponseDocuments().ToArray(); } else { responseDocuments = request.Processor.Response(requestId); } if (responseDocuments != null && responseDocuments.Length > 0) { Log.Info(string.Format("Uploading {4} documents for query: {0} (DataMart:{1}, Network:{2}, RequestID: {3:D})", request.Source.Identifier, request.DataMartName, request.NetworkName, request.Source.ID, responseDocuments.Length)); if (_networkSetting.SupportsUploadV2) { Log.Debug($"Network settings indicate document upload v2 supported for query: {request.Source.Identifier}, (DataMart: {request.DataMartName}, Network: { request.NetworkName }, RequestID: { request.Source.ID }"); try { Parallel.ForEach(responseDocuments, new ParallelOptions { MaxDegreeOfParallelism = 4 }, (doc) => { string uploadIdentifier = ("[" + Utilities.Crypto.Hash(Guid.NewGuid()) + "]").PadRight(16); System.IO.Stream stream = null; try { if (cache.Enabled) { stream = cache.GetDocumentStream(Guid.Parse(doc.DocumentID)); } else { request.Processor.ResponseDocument(requestId, doc.DocumentID, out stream, doc.Size); } var dto = new DTO.DataMartClient.Criteria.DocumentMetadata { ID = Guid.Parse(doc.DocumentID), DataMartID = request.DataMartId, RequestID = Guid.Parse(requestId), IsViewable = doc.IsViewable, Size = doc.Size, MimeType = doc.MimeType, Kind = doc.Kind, Name = doc.Filename, CurrentChunkIndex = 0 }; DnsServiceManager.PostDocumentChunk(uploadIdentifier, dto, stream, _networkSetting); } finally { stream.Dispose(); } }); } catch (Exception ex) { Log.Error($"An error occurred during concurrent uploading of response documents for query: {request.Source.Identifier}, (DataMart: {request.DataMartName}, Network: { request.NetworkName }, RequestID: { request.Source.ID }", ex); throw; } } else { //since the ID of the document returned from the API is not known when posting, and the order cannot be assumed in the response //loop over each document and post the metadata and the content as a single unit of work per document. Log.Debug($"Network settings indicates document upload v2 IS NOT supported for query: {request.Source.Identifier}, (DataMart: {request.DataMartName}, Network: { request.NetworkName }, RequestID: { request.Source.ID }"); for (int i = 0; i < responseDocuments.Length; i++) { string uploadIdentifier = ("[" + Utilities.Crypto.Hash(Guid.NewGuid()) + "]").PadRight(16); Guid[] metaDataPostResponse = DnsServiceManager.PostResponseDocuments(uploadIdentifier, requestId, request.DataMartId, new[] { responseDocuments[i] }, _networkSetting); System.IO.Stream contentStream = null; try { if (cache.Enabled) { contentStream = cache.GetDocumentStream(Guid.Parse(responseDocuments[i].DocumentID)); } else { request.Processor.ResponseDocument(requestId, responseDocuments[i].DocumentID, out contentStream, 60000); } DnsServiceManager.PostResponseDocumentContent(uploadIdentifier, requestId, request.DataMartId, metaDataPostResponse[0], responseDocuments[i].Filename, contentStream, _networkSetting) .LogExceptions(Log.Error) .Catch() .Wait(); } finally { contentStream.CallDispose(); } } } } else { Log.Info(string.Format("No documents to upload for query: {0} (DataMart:{1}, Network:{2}, RequestID: {3:D})", request.Source.Identifier, request.DataMartName, request.NetworkName, request.Source.ID, responseDocuments.Length)); } if (request.RoutingStatus == Lpp.Dns.DTO.DataMartClient.Enums.DMCRoutingStatus.Failed) { HubRequestStatus hubRequestStatus = DnsServiceManager.ConvertModelRequestStatus(request.Processor.Status(requestId)); hubRequestStatus.Message = request.Processor.Status(requestId).Message; DnsServiceManager.SetRequestStatus(request, hubRequestStatus, request.Properties, _networkSetting); Log.Info(string.Format("The following query {0} (ID: {3}, DataMart: {4}, Network: {5}) failed to execute on behalf of {1}:\n\n{2}", request.Source.Identifier, _networkSetting.Profile.Username, hubRequestStatus.Message, request.Source.ID, request.DataMartName, request.NetworkName)); } else { Log.Info(string.Format("The following query {0} (ID: {2}, DataMart: {3}, Network: {4}) was executed and results were uploaded automatically on behalf of {1}", request.Source.Identifier, _networkSetting.Profile.Username, request.Source.ID, request.DataMartName, request.NetworkName)); } }