Exemplo n.º 1
0
        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));
            }
        }
Exemplo n.º 2
0
        public void UploadMultipleSmallFiles()
        {
            //Guid requestID = new Guid("63f074ee-cf4c-4838-ae27-aa4700d7ca87");
            //Guid dataMartID = new Guid("84C9440A-0B1F-4EC0-AF75-A85500CC8F16");
            //string sourceFolderPath = @"C:\work\DR-UAT\DP2\v_psu_01_346\msoc";
            string sourceFolderPath = @"C:\work\DR-UAT\DP2\v_psu_01_353\TEST";

            Guid requestID  = new Guid("2D35C33F-11D2-47E3-A2D8-AA4700BCCBBA");
            Guid dataMartID = new Guid("6D55D9BA-9787-40DC-B9B7-A49F00C83946");

            List <DTO.DataMartClient.Criteria.DocumentMetadata> documents = new List <DTO.DataMartClient.Criteria.DocumentMetadata>();

            foreach (var file in System.IO.Directory.GetFiles(sourceFolderPath))
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(file);
                Guid documentID       = Lpp.Utilities.DatabaseEx.NewGuid();
                documents.Add(
                    new DTO.DataMartClient.Criteria.DocumentMetadata
                {
                    CurrentChunkIndex = 0,
                    DataMartID        = dataMartID,
                    ID         = documentID,
                    IsViewable = false,
                    Kind       = string.Empty,
                    MimeType   = Lpp.Utilities.FileEx.GetMimeTypeByExtension(fi.Name),
                    Name       = fi.Name,
                    RequestID  = requestID
                }
                    );

                logger.Debug($"Adding document: {fi.Name} (ID: {documentID})");
            }

            //documents = documents.Skip(12).Take(4).ToList();

            Parallel.ForEach(documents,
                             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);
                    //}

                    stream = new System.IO.FileStream(System.IO.Path.Combine(sourceFolderPath, doc.Name), System.IO.FileMode.Open);

                    var dto = new DTO.DataMartClient.Criteria.DocumentMetadata
                    {
                        ID                = doc.ID,
                        DataMartID        = dataMartID,
                        RequestID         = requestID,
                        IsViewable        = doc.IsViewable,
                        Size              = doc.Size,
                        MimeType          = doc.MimeType,
                        Kind              = doc.Kind,
                        Name              = doc.Name,
                        CurrentChunkIndex = 0
                    };

                    DnsServiceManager.PostDocumentChunk(uploadIdentifier, dto, stream, _networkSetting);
                }
                finally
                {
                    stream.Dispose();
                }
            });
        }