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 AutoProcess(KeyValuePair <string, HubRequest> input) { var request = input.Value; var datamartDescription = Configuration.Instance.GetDataMartDescription(request.NetworkId, request.DataMartId); if (datamartDescription.ProcessQueriesAndUploadAutomatically == false && datamartDescription.ProcessQueriesAndNotUpload == false) { //just notify, do not process string message = $"New query submitted and awaiting processing in { request.ProjectName } Project: { request.Source.Name } ({request.Source.Identifier})"; SystemTray.DisplayNewQueryNotificationToolTip(message); RequestStatuses.TryAdd(input.Key, ProcessingStatus.CannotRunAndUpload); return; } var modelDescription = Configuration.Instance.GetModelDescription(request.NetworkId, request.DataMartId, request.Source.ModelID); var packageIdentifier = new Lpp.Dns.DTO.DataMartClient.RequestTypeIdentifier { Identifier = request.Source.RequestTypePackageIdentifier, Version = request.Source.AdapterPackageVersion }; if (!System.IO.File.Exists(System.IO.Path.Combine(Configuration.PackagesFolderPath, packageIdentifier.PackageName()))) { DnsServiceManager.DownloadPackage(_networkSetting, packageIdentifier); } var domainManager = new DomainManger.DomainManager(Configuration.PackagesFolderPath); try { domainManager.Load(request.Source.RequestTypePackageIdentifier, request.Source.AdapterPackageVersion); IModelProcessor processor = domainManager.GetProcessor(modelDescription.ProcessorId); ProcessorManager.UpdateProcessorSettings(modelDescription, processor); processor.Settings.Add("NetworkId", request.NetworkId); Lib.Caching.DocumentCacheManager cache = new Lib.Caching.DocumentCacheManager(request.NetworkId, request.DataMartId, request.Source.ID, request.Source.Responses.Where(x => x.DataMartID == request.DataMartId).FirstOrDefault().ResponseID); //need to initialize before checking the capabilities and settings of the processor since they may change based on the type of request being sent. if (processor is IEarlyInitializeModelProcessor) { ((IEarlyInitializeModelProcessor)processor).Initialize(modelDescription.ModelId, request.Documents.Select(d => new DocumentWithStream(d.ID, new Document(d.ID, d.Document.MimeType, d.Document.Name, d.Document.IsViewable, Convert.ToInt32(d.Document.Size), d.Document.Kind), new DocumentChunkStream(d.ID, _networkSetting))).ToArray()); } if (processor != null && processor.ModelMetadata != null && processor.ModelMetadata.Capabilities != null && processor.ModelMetadata.Capabilities.ContainsKey("CanRunAndUpload") && !(bool)processor.ModelMetadata.Capabilities["CanRunAndUpload"]) { //can't be run, don't attempt autoprocessing RequestStatuses.TryAdd(input.Key, ProcessingStatus.CannotRunAndUpload); domainManager.Dispose(); return; } request.Processor = processor; if (cache.HasResponseDocuments == false && (request.RoutingStatus == Lpp.Dns.DTO.DataMartClient.Enums.DMCRoutingStatus.Submitted || request.RoutingStatus == DTO.DataMartClient.Enums.DMCRoutingStatus.Resubmitted)) { if (processor != null) { SystemTray.GenerateNotification(request, request.NetworkId); StartProcessingRequest(request, processor, datamartDescription, domainManager, cache); return; } } else if (request.RoutingStatus == Lpp.Dns.DTO.DataMartClient.Enums.DMCRoutingStatus.AwaitingResponseApproval) { if (datamartDescription.ProcessQueriesAndUploadAutomatically) { // Increment counter System.Threading.Interlocked.Increment(ref _queriesProcessedCount); SystemTray.UpdateNotifyText(_queriesProcessedCount, request.DataMartName, request.NetworkId); StartUploadingRequest(request, domainManager, cache); return; } } else if (cache.HasResponseDocuments) { RequestStatuses.TryAdd(input.Key, ProcessingStatus.PendingUpload); } domainManager.Dispose(); } catch (Exception ex) { Log.Error($"Error autoprocessing Request: { request.Source.Identifier }, DataMartId: { request.DataMartId }, NetworkId: {request.NetworkId}", ex); domainManager.Dispose(); throw; } }