Esempio n. 1
0
 private void SavePublishTaskInfo(PublishDocumentTask publishDocumentTask)
 {
     publishDocumentTask.State     = PublishState.Canceled;
     publishDocumentTask.UpdatedAt = DateTime.Now;
     _logger.LogDebug($"id={publishDocumentTask.DocumentId} PublishDocumentTask.State= {publishDocumentTask.State}");
     PublishDocumentTaskRepository.Save(publishDocumentTask);
 }
 private void SavePublishTaskInfo(PublishDocumentTask publishDocumentTask)
 {
     publishDocumentTask.IsDelivered = true;
     publishDocumentTask.UpdatedAt   = DateTime.Now;
     _logger.LogDebug($"Delivered task id={publishDocumentTask.DocumentId}");
     PublishDocumentTaskRepository.Save(publishDocumentTask);
 }
Esempio n. 3
0
        protected override async Task <bool> OnMessageReceivedAsync(DocumentPublishEventMessage message,
                                                                    CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            try
            {
                var publishDocumentTask = await PublishDocumentTaskRepository.GetAsync(message.Id);

                if (publishDocumentTask == null)
                {
                    publishDocumentTask = new PublishDocumentTask(message);
                    PublishDocumentTaskRepository.Save(publishDocumentTask);
                    await NotifyService.SendNotificationAsync(publishDocumentTask);
                }

                if (!CheckIsNeedToProcessMessage(publishDocumentTask))
                {
                    return(true);
                }

                var documentPublicationInfo = await ProcessMessage(message, cancellationToken);

                SavePublishTaskInfo(publishDocumentTask, documentPublicationInfo);

                await NotifyService.SendNotificationAsync(publishDocumentTask);

                if (documentPublicationInfo.ResultType == PublicationResultType.Processing)
                {
                    var updateMessage = CreatePublishUpdateMessage(message, documentPublicationInfo.RefId);
                    EventBus.PublishMessage(updateMessage);

                    return(false);
                }

                var documentPublishResultEventMessage = new DocumentPublishResultEventMessage
                {
                    UserId     = message.UserId,
                    Id         = message.Id,
                    CreatedAt  = message.CreatedAt,
                    LoadId     = documentPublicationInfo.LoadId,
                    ResultType = documentPublicationInfo.ResultType
                };

                EventBus.PublishMessage(documentPublishResultEventMessage);

                return(true);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Возникла ошибка при обработке сообщения");
                return(false);
            }
        }
        public Task SendNotificationAsync(PublishDocumentTask message)
        {
            var msg = JsonConvert.SerializeObject(message, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                StringEscapeHandling  = StringEscapeHandling.EscapeHtml,
            });

            return(_hub.Clients.All.SendAsync("OnPublicationStateChanged", msg));
        }
 public DocumentPublishEventMessage(PublishDocumentTask publishDocumentTask)
 {
     Id = publishDocumentTask.Id;
     AdditionalPublicationData = (AdditionalPublicationData)JsonConvert.DeserializeObject(publishDocumentTask.Payload, new JsonSerializerSettings
     {
         TypeNameHandling = TypeNameHandling.Auto
     });
     DocumentId       = publishDocumentTask.DocumentId;
     DocumentRevision = publishDocumentTask.DocumentRevision;
     DocumentType     = publishDocumentTask.DocumentType;
     OrganizationId   = publishDocumentTask.OrganizationId;
     UserId           = publishDocumentTask.UserId;
 }
Esempio n. 6
0
        /// <summary>
        /// Проверить, нужно ли обрабатывать сообщение
        /// </summary>
        /// <param name="publishDocumentTask">Задача</param>
        /// <returns>True, если сообщение еще не было обработано</returns>
        private static bool CheckIsNeedToProcessMessage(PublishDocumentTask publishDocumentTask)
        {
            if (publishDocumentTask.IsFinished)
            {
                return(false);
            }

            // Processing.. no actions needed, just return false.
            if (publishDocumentTask.State == PublishState.Processing)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 7
0
        public void Save(PublishDocumentTask publishDocumentTask)
        {
            var task = _context.Find <PublishDocumentTask>(publishDocumentTask.Id);

            if (task == null)
            {
                _context.Add(publishDocumentTask);
            }
            else
            {
                _context.Update(publishDocumentTask);
            }

            _context.SaveChanges();
        }
Esempio n. 8
0
        private void SavePublishTaskInfo(PublishDocumentTask publishDocumentTask,
                                         DocumentPublicationInfo documentPublicationInfo)
        {
            var publishDocumentTaskAttempt = new PublishDocumentTaskAttempt
            {
                PublishDocumentTaskId = publishDocumentTask.Id,
                Response         = documentPublicationInfo.Response,
                Request          = documentPublicationInfo.Request,
                HasEisExceptions = documentPublicationInfo.HasInnerExceptions,
                Result           = documentPublicationInfo.ResultType
            };

            publishDocumentTask.State            = ConvertToPublishState(documentPublicationInfo.ResultType);
            publishDocumentTask.HasEisExceptions = publishDocumentTask.HasEisExceptions ||
                                                   publishDocumentTaskAttempt.HasEisExceptions;
            publishDocumentTask.LoadId    = documentPublicationInfo.LoadId;
            publishDocumentTask.RefId     = documentPublicationInfo.RefId;
            publishDocumentTask.UpdatedAt = DateTime.Now;
            publishDocumentTask.PublishDocumentTaskAttempts.Add(publishDocumentTaskAttempt);
            PublishDocumentTaskRepository.Save(publishDocumentTask);
            _logger.LogDebug(
                $"id={publishDocumentTask.DocumentId} PublishDocumentTask.State= {publishDocumentTask.State} attempt result= {publishDocumentTaskAttempt.Result}");

            PublishState ConvertToPublishState(PublicationResultType resultType)
            {
                switch (resultType)
                {
                case PublicationResultType.Success:
                    return(PublishState.Published);

                case PublicationResultType.Failed:
                    return(PublishState.Failed);

                case PublicationResultType.Processing:
                    return(PublishState.Processing);

                case PublicationResultType.XmlValidationError:
                    return(PublishState.XmlValidationError);

                default:
                    throw new ArgumentOutOfRangeException(nameof(resultType));
                }
            }
        }
        /// <summary>
        /// Обработчик сообщения
        /// </summary>
        /// <param name="publishDocumentTask">Задача</param>
        /// <param name="message">Сообщение</param>
        /// <param name="cancellationToken">Токен отмены</param>
        /// <returns>must return http response message</returns>
        private async Task <DocumentPublicationInfo> ProcessMessage(PublishDocumentTask publishDocumentTask,
                                                                    DocumentPublishUpdateEventMessage message,
                                                                    CancellationToken cancellationToken)
        {
            /***
             *
             * Perform update document state logic from another system
             *
             * */
            await Task.Delay(0, cancellationToken);

            int?loadId = null;
            var result = (PublicationResultType) new Random().Next(0, 3);

            if (result == PublicationResultType.Success)
            {
                loadId = new Random().Next(1, int.MaxValue);
            }

            return(new DocumentPublicationInfo(message.RefId, result, loadId, "document update request",
                                               "document update response"));
        }