GetDataSourceForMessage() public static method

Factory method to return a datasource based on a given data message
public static GetDataSourceForMessage ( ClientDataMessage dataMessage ) : IDataSource
dataMessage ClientDataMessage ClientDataMessage object
return IDataSource
Example #1
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);
            }
        }
Example #2
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;
                    }
                }
            });
        }
Example #3
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);
        }