Пример #1
0
        private async Task postExistingDataMessagesAsync()
        {
            IDataSource <ClientDataMessage> inputLocationDS = DataSourceFactory.GetDataSourceForLocation(Settings.InputDir);

            foreach (ClientDataMessage dataMessage in inputLocationDS.FindAll())
            {
                base.formInstance.logText("File - " + dataMessage.Name + " - found in input location. Initializing data processing sequence...");

                await this.PostMessageAsync(dataMessage);
            }
        }
Пример #2
0
        /// <summary>
        /// Write the DataJobStatusDetail out as a status log for
        /// either the successful of failed processing of a document.
        /// </summary>
        /// <param name="jobStatusDetail">DataJobStatusDetail object</param>
        /// <param name="logFileFullPath">Full path of the log file to write out to</param>
        /// <returns>Empty task</returns>
        private async Task writeStatusLogFileAsync(DataJobStatusDetail jobStatusDetail,
                                                   ClientDataMessage targetDataMessage,
                                                   HttpResponseMessage httpResponse)
        {
            if (targetDataMessage == null)
            {
                return;
            }

            string logData = string.Empty;

            IDataSource <ClientDataMessage> logDataSource =
                DataSourceFactory.GetDataSourceForMessage(targetDataMessage);

            var logFilePath = Path.Combine(Path.GetDirectoryName(targetDataMessage.FullPath),
                                           (Path.GetFileNameWithoutExtension(targetDataMessage.FullPath) +
                                            "_log.txt"));

            if (null != jobStatusDetail)
            {
                logData = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(jobStatusDetail));
            }
            else if (null != httpResponse)
            {
                logData = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(httpResponse));
            }

            var targetLogDataMessage = new ClientDataMessage()
            {
                Name          = Path.GetFileName(logFilePath),
                FullPath      = logFilePath,
                MessageStatus = targetDataMessage.MessageStatus
            };


            using (var logMemoryStream = new MemoryStream(Encoding.Default.GetBytes(logData)))
            {
                logDataSource.Create(logMemoryStream, targetLogDataMessage);
            }
        }
Пример #3
0
        private async Task moveDataToTargetAsync(ClientDataMessage sourceDataMessage,
                                                 ClientDataMessage targetDataMessage)
        {
            Stream sourceStream = null;

            IDataSource <ClientDataMessage> sourceDataSource =
                DataSourceFactory.GetDataSourceForMessage(sourceDataMessage);

            await Task.Run(() =>
            {
                try
                {
                    sourceStream = sourceDataSource.Read(sourceDataMessage);

                    if (sourceStream != null)
                    {
                        IDataSource <ClientDataMessage> targetDataSource =
                            DataSourceFactory.GetDataSourceForMessage(targetDataMessage);

                        targetDataSource.Create(sourceStream, targetDataMessage);

                        sourceStream.Dispose();

                        sourceDataSource.Delete(sourceDataMessage);
                    }
                }
                catch { }
                finally
                {
                    if (sourceStream != null)
                    {
                        sourceStream.Dispose();
                        sourceStream = null;
                    }
                }
            });
        }
Пример #4
0
        private async Task <bool> processInprocessQueue()
        {
            if (!InprocessQueue.IsEmpty)
            {
                ClientDataMessage dataMessage;

                // For each document in the in process queue, read async and
                // submit to the AX7 endpoint to enqueue.
                while (InprocessQueue.TryDequeue(out dataMessage))
                {
                    IDataSource <ClientDataMessage> sourceDataSource =
                        DataSourceFactory.GetDataSourceForMessage(dataMessage);

                    Stream sourceStream = sourceDataSource.Read(dataMessage);

                    if (sourceStream != null)
                    {
                        try
                        {
                            sourceStream.Seek(0, SeekOrigin.Begin);

                            var    httpClientHelper = new HttpClientHelper();
                            string correlationId    = dataMessage.Name;
                            Uri    enqueueUri       = httpClientHelper.GetEnqueueUri();

                            base.formInstance.logText("Enqueueing job:  " + dataMessage.Name + ". File size:  " + sourceStream.Length + " bytes.");

                            // Post Enqueue request
                            var response = await httpClientHelper.SendPostRequestAsync(enqueueUri, sourceStream, correlationId);

                            if (response.IsSuccessStatusCode)
                            {
                                // Log success and add to Enqueued jobs for further processing
                                var messageId = await response.Content.ReadAsStringAsync();

                                // Log enqueue success
                                base.formInstance.logText("File:  " + dataMessage.Name + " -  enqueued successfully.");

                                base.formInstance.logText("Message identifier for: " + dataMessage.Name + " - is: " + messageId);

                                // Queue for futher status processing
                                EnqueuedJobs.TryAdd(messageId, new ClientDataMessage(dataMessage.FullPath, MessageStatus.Enqueued));
                            }
                            else
                            {
                                // Enqueue failed. Move message to error location.

                                base.formInstance.logText("Enqueue failed for file:  " + dataMessage.Name);

                                base.formInstance.logText("Failure response:  Status: " + response.StatusCode + ", Reason: " + response.ReasonPhrase);

                                var targetDataMessage = new ClientDataMessage()
                                {
                                    Name          = dataMessage.Name,
                                    FullPath      = Path.Combine(Settings.ErrorDir, dataMessage.Name),
                                    MessageStatus = MessageStatus.Failed
                                };

                                // Move data to error location
                                await this.moveDataToTargetAsync(dataMessage, targetDataMessage);

                                // Enqueue failure
                                await this.updateStatsAsync(null, StatType.Failure, targetDataMessage, response);
                            }
                        }
                        catch (Exception _ex)
                        {
                            base.formInstance.logText("Failure processing file: " + dataMessage.Name + ".Exception : " + _ex.Message);
                        }
                        finally
                        {
                            if (sourceStream != null)
                            {
                                sourceStream.Close();
                                sourceStream.Dispose();
                            }
                        }
                    }
                }
            }

            return(true);
        }