/// <summary> /// Execute query only and do not upload results /// </summary> /// <param name="qdm">Query to be processed</param> private void ProcessRequest(HubRequest request) { Log.Info(string.Format("BackgroundProcess: Processing request {0} (RequestID: {3}, DataMartId: {1}, NetworkId: {2})", request.Source.Identifier, request.DataMartId, request.NetworkId, request.Source.ID)); try { var key = MakeKey(request); if (RequestStatuses.ContainsKey(key)) { RequestStatuses[key] = ProcessingStatus.InProcessing; } string requestId = ProcessRequest(request, request.Processor); RequestStatus.StatusCode statusCode = request.Processor.Status(requestId).Code; if (statusCode == RequestStatus.StatusCode.Error) { 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 {3} (ID: {0}) was executed on behalf of {1}:\n\n{2}", request.Source.ID, _networkSetting.Profile.Username, "", request.Source.Identifier)); } catch (Exception ex) { string message = string.Format("BackgroundProcess: An error occurred when processing request {0} (RequestID: {3}, DataMartId: {1}, NetworkId: {2}", request.Source.Identifier, request.DataMartId, request.NetworkId, request.Source.ID); Log.Error(message, ex); DnsServiceManager.SetRequestStatus(request, new HubRequestStatus(Lpp.Dns.DTO.DataMartClient.Enums.DMCRoutingStatus.Failed, message), request.Properties, _networkSetting); } Log.Info(string.Format("BackgroundProcess: Finished processing request {0} (RequestID: {3}, DataMartId: {1}, NetworkId: {2})", request.Source.Identifier, request.DataMartId, request.NetworkId, request.Source.ID)); }
/// <summary> /// Execute query only and do not upload results /// </summary> /// <param name="qdm">Query to be processed</param> private void ProcessRequest(HubRequest request) { NetWorkSetting networkSetting = Configuration.Instance.GetNetworkSetting(request.NetworkId); _log.Info(String.Format("BackgroundProcess: Processing request {0} (RequestID: {3}, DataMartId: {1}, NetworkId: {2})", request.Source.Identifier, request.DataMartId, networkSetting.NetworkId, request.Source.ID)); try { string requestId = ProcessRequest(request, request.Processor, networkSetting); RequestStatus.StatusCode statusCode = request.Processor.Status(requestId).Code; 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); } else { DnsServiceManager.SetRequestStatus(request, DnsServiceManager.ConvertModelRequestStatus(request.Processor.Status(requestId)), request.Properties, networkSetting); } _log.Info(String.Format("The following query {3} (ID: {0}) was executed on behalf of {1}:\n\n{2}", request.Source.ID, networkSetting.Profile.Username, "", request.Source.Identifier)); } catch (Exception ex) { string message = string.Format("BackgroundProcess: An error occurred when processing request {0} (RequestID: {3}, DataMartId: {1}, NetworkId: {2}", request.Source.Identifier, request.DataMartId, networkSetting.NetworkId, request.Source.ID); _log.Error(message, ex); DnsServiceManager.SetRequestStatus(request, new HubRequestStatus(Lpp.Dns.DTO.DataMartClient.Enums.DMCRoutingStatus.Failed, message), request.Properties, networkSetting); } _log.Info(String.Format("BackgroundProcess: Finished processing request {0} (RequestID: {3}, DataMartId: {1}, NetworkId: {2})", request.Source.Identifier, request.DataMartId, networkSetting.NetworkId, request.Source.ID)); }
private void StartProcessingRequest(HubRequest request, IModelProcessor processor, DataMartDescription datamartDescription, DomainManger.DomainManager domainManager, Lib.Caching.DocumentCacheManager cache) { Action process = () => { processor.SetRequestProperties(request.Source.ID.ToString(), request.Properties); ProcessRequest(request); }; Action <Task> continuation = (completed) => { HubRequestStatus hubRequestStatus = null; var statusCode = request.Processor.Status(request.Source.ID.ToString()).Code; if (cache.Enabled) { Document[] responseDocuments = processor.Response(request.Source.ID.ToString()); cache.Add(responseDocuments.Select(doc => { System.IO.Stream data; processor.ResponseDocument(request.Source.ID.ToString(), doc.DocumentID, out data, doc.Size); Guid documentID; if (!Guid.TryParse(doc.DocumentID, out documentID)) { documentID = Utilities.DatabaseEx.NewGuid(); doc.DocumentID = documentID.ToString(); } return(new DocumentWithStream(documentID, doc, data)); })); } if (datamartDescription.ProcessQueriesAndNotUpload && (statusCode == RequestStatus.StatusCode.Complete || statusCode == RequestStatus.StatusCode.CompleteWithMessage)) { RequestStatuses.TryAdd(MakeKey(request), ProcessingStatus.PendingUpload); } else if (datamartDescription.ProcessQueriesAndUploadAutomatically && (statusCode == RequestStatus.StatusCode.Complete || statusCode == RequestStatus.StatusCode.CompleteWithMessage)) { // Post process requests that are automatically uploaded processor.PostProcess(request.Source.ID.ToString()); if (cache.Enabled) { cache.ClearCache(); Document[] responseDocuments = processor.Response(request.Source.ID.ToString()); cache.Add(responseDocuments.Select(doc => { System.IO.Stream data; processor.ResponseDocument(request.Source.ID.ToString(), doc.DocumentID, out data, doc.Size); Guid documentID; if (!Guid.TryParse(doc.DocumentID, out documentID)) { documentID = Utilities.DatabaseEx.NewGuid(); doc.DocumentID = documentID.ToString(); } return(new DocumentWithStream(documentID, doc, data)); })); } // Increment counter System.Threading.Interlocked.Increment(ref _queriesProcessedCount); statusCode = request.Processor.Status(request.Source.ID.ToString()).Code; if (statusCode == RequestStatus.StatusCode.Error) { hubRequestStatus = DnsServiceManager.ConvertModelRequestStatus(request.Processor.Status(request.Source.ID.ToString())); hubRequestStatus.Message = request.Processor.Status(request.Source.ID.ToString()).Message; } else if (statusCode == RequestStatus.StatusCode.Complete || statusCode == RequestStatus.StatusCode.CompleteWithMessage) { SystemTray.UpdateNotifyText(_queriesProcessedCount, request.DataMartName, request.NetworkId); try { UploadRequest(request, cache); statusCode = request.Processor.Status(request.Source.ID.ToString()).Code; hubRequestStatus = DnsServiceManager.ConvertModelRequestStatus(request.Processor.Status(request.Source.ID.ToString())); hubRequestStatus.Message = request.Processor.Status(request.Source.ID.ToString()).Message; } catch (Exception ex) { string message = string.Format("An error occurred while attempting unattended processing of the following query {0} (ID: {1}, DataMart: {2}, Network: {3})", request.Source.Identifier, request.Source.ID, request.DataMartName, request.NetworkName); Log.Error(message, ex); hubRequestStatus = new HubRequestStatus(Lpp.Dns.DTO.DataMartClient.Enums.DMCRoutingStatus.Failed, message); } } else { statusCode = request.Processor.Status(request.Source.ID.ToString()).Code; hubRequestStatus = DnsServiceManager.ConvertModelRequestStatus(request.Processor.Status(request.Source.ID.ToString())); hubRequestStatus.Message = request.Processor.Status(request.Source.ID.ToString()).Message; } DnsServiceManager.SetRequestStatus(request, hubRequestStatus, request.Properties, _networkSetting); Log.Info(string.Format("BackgroundProcess: Finished Processing / Uploading results for query {0} (RequestID: {3}, DataMart: {1}, Network: {2})", request.Source.Identifier, request.DataMartName, request.NetworkName, request.Source.ID)); if (statusCode == RequestStatus.StatusCode.Error || statusCode == RequestStatus.StatusCode.Complete || statusCode == RequestStatus.StatusCode.CompleteWithMessage || statusCode == RequestStatus.StatusCode.AwaitingResponseApproval) { //mark auto-processing complete in the status cache if the processing resulted in either an error or a completed status var key = MakeKey(request); if (RequestStatuses.ContainsKey(key)) { RequestStatuses[key] = ProcessingStatus.Complete; } } } }; RunTask(MakeKey(request), domainManager, process, continuation); }
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) { NetWorkSetting networkSetting = Configuration.Instance.GetNetworkSetting(request.NetworkId); _log.Info(String.Format("BackgroundProcess: Processing query {0} (RequestId: {3}, DataMartId: {1}, NetworkId: {2})", request.Source.Identifier, request.DataMartId, networkSetting.NetworkId, request.Source.ID)); try { string requestId = request.Source.ID.ToString(); Lpp.Dns.DataMart.Model.Document[] responseDocuments = request.Processor.Response(requestId); Guid[] documentIds = DnsServiceManager.PostResponseDocuments(requestId, request.DataMartId, responseDocuments, networkSetting); _log.Info("Number of portal document ids returned by PostResponseDocuments: " + documentIds.Length); for (int i = 0; i < documentIds.Length; i++) { _log.Info("About to post content for portal document id: " + documentIds[i] + " corresponding to response document id: " + responseDocuments[i].DocumentID); Stream contentStream = null; try { request.Processor.ResponseDocument(requestId, responseDocuments[i].DocumentID, out contentStream, 60000); DnsServiceManager.PostResponseDocumentContent(documentIds[i], contentStream, networkSetting) .LogExceptions(_log.Error) .Catch() .SubscribeOn(Scheduler.Default) .LastOrDefault(); } finally { contentStream.CallDispose(); } } 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}) failed to execute on behalf of {1}:\n\n{2}", request.Source.Identifier, networkSetting.Profile.Username, hubRequestStatus.Message, request.Source.ID)); } else { //MantisID:6331:Zero out low-cell counts if they exist. //string strReason = request.Processor.Status(requestId).Code == RequestStatus.StatusCode.CompleteWithMessage ? request.Processor.Status(requestId).Message : string.Empty; //DnsServiceManager.SetRequestStatus(request, new HubRequestStatus(Lpp.Dns.DTO.DataMartClient.Enums.DMCRoutingStatus.Completed), request.Properties, networkSetting); //if (string.IsNullOrEmpty(strReason)) //{ //DnsServiceManager.SetRequestStatus(request, new HubRequestStatus(HubRequestStatus.StatusCode.Completed), request.Properties, networkSetting); _log.Info(String.Format("The following query {0} (ID: {2}) was executed and results were uploaded automatically on behalf of {1}", request.Source.Identifier, networkSetting.Profile.Username, request.Source.ID)); //} //else //{ // //DnsServiceManager.SetRequestStatus(request, new HubRequestStatus(HubRequestStatus.StatusCode.Completed, strReason), request.Properties, networkSetting); // _log.Info(String.Format("The following query {0} (ID: {3}) was executed and results were uploaded automatically on behalf of {1}:\n\n{2}", // request.Source.Identifier, networkSetting.Profile.Username, strReason, request.Source.ID)); //} } } catch (Exception ex) { string message = string.Format("An error occurred while attempting unattended processing of the following query {0} (ID: {1})", request.Source.Identifier, request.Source.ID); _log.Error(message, ex); DnsServiceManager.SetRequestStatus(request, new HubRequestStatus(Lpp.Dns.DTO.DataMartClient.Enums.DMCRoutingStatus.Failed, message), request.Properties, networkSetting); } _log.Info(String.Format("BackgroundProcess: Finished Processing / Uploading results for query {0} (RequestID: {3}, DataMartId: {1}, NetworkId: {2})", request.Source.Identifier, request.DataMartId, networkSetting.NetworkId, request.Source.ID)); }
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)); } }