예제 #1
0
        /// <inheritdoc/>
        public async Task <SessionWorkflowState> ProcessSessionMessageAsync(IMessageSession session, Message message)
        {
            var state = await _stateManager.GetWorkflowStateAsync(session);

            if (message.UserProperties.TryGetValue("ResetWorkflowState", out var shouldReset) && Convert.ToBoolean(shouldReset))
            {
                _logger.LogInformation($"[{nameof(ContractEventSessionManager)}.{nameof(ProcessSessionMessageAsync)}] - Reset workflow state message [{message.MessageId}] received, resetting session [{session.SessionId}].");
                return(await _stateManager.ResetWorkflowStateAsync(session));
            }

            if (state.IsFaulted && state.FailedMessageId != message.MessageId)
            {
                _logger.LogWarning($"[{nameof(ContractEventSessionManager)}.{nameof(ProcessSessionMessageAsync)}] - Moving this message [{message.MessageId}] to DLQ, beacause previous message [{state.FailedMessageId}] failed, hence dropping all messages in session [{session.SessionId}].");
                state.PostponedMessages.Add(message.MessageId);
                await session.DeadLetterAsync(message.SystemProperties.LockToken, $"Previous message failed, hence dropping all messages in session", $"Previous message {state.FailedMessageId} failed, hence dropping all messages in session {session.SessionId}");

                await _stateManager.SetWorkflowStateAsync(session, state);
            }
            else
            {
                if (state.IsFaulted && state.FailedMessageId == message.MessageId)
                {
                    state = await _stateManager.ResetWorkflowStateAsync(session);
                }

                try
                {
                    var contractEvent = JsonConvert.DeserializeObject <ContractEvent>(Encoding.UTF8.GetString(message.Body));
                    _processLog.Initialise(message, contractEvent);

                    await _contractService.ProcessMessage(contractEvent);
                }
                catch (NotImplementedException notImplemented)
                {
                    await SaveFailedState(session, message, state, notImplemented, "Message contains not implemented input");

                    await session.DeadLetterAsync(message.SystemProperties.LockToken, $"Message contains not imeplemented input", $"Invalid message {state.FailedMessageId} in session {session.SessionId} reason - {notImplemented.Message}.");
                }
                catch (ContractEventExpectationFailedException badMessage)
                {
                    await SaveFailedState(session, message, state, badMessage, "Message contains invalid input");

                    await session.DeadLetterAsync(message.SystemProperties.LockToken, $"Message contains invalid input", $"Invalid message {state.FailedMessageId} in session {session.SessionId} reason - {badMessage.Message}.");
                }
                catch (Exception ex)
                {
                    if (message.SystemProperties.DeliveryCount >= _functionSettings.MaximumDeliveryCount)
                    {
                        await SaveFailedState(session, message, state, ex, "Message delivery count exceeded");
                    }

                    throw;
                }
            }

            return(state);
        }
예제 #2
0
        /// <inheritdoc/>
        public async Task ProcessMessage(ContractEvent contractEvent)
        {
            _logger.LogInformation($"[{nameof(ProcessMessage)}] Processing message for contract event : {contractEvent.BookmarkId}");

            var eventType = contractEvent.GetContractEventType();

            switch (eventType)
            {
            case ContractEventType.Create:
                var existingContract = await _contractsDataService.TryGetContractAsync(contractEvent.ContractNumber, contractEvent.ContractVersion);

                if (existingContract is null)
                {
                    await _contractCreationService.CreateAsync(contractEvent);
                }
                else
                {
                    _logger.LogWarning($"[{nameof(ContractEventProcessor)}] - Ignoring contract event with id [{contractEvent.BookmarkId}] because a contract with contract number [{contractEvent.ContractNumber}], version [{contractEvent.ContractVersion}] and Id [{existingContract.Id}] already exists.");
                }

                break;

            case ContractEventType.Approve:
                var approveContract = await _contractsDataService.TryGetContractAsync(contractEvent.ContractNumber, contractEvent.ContractVersion);

                if (approveContract is null)
                {
                    _logger.LogWarning($"[{nameof(ContractEventProcessor)}] - Ignoring contract event with id [{contractEvent.BookmarkId}] because unable to find a contract with contract number [{contractEvent.ContractNumber}], version [{contractEvent.ContractVersion}].");
                }
                else
                {
                    await _contractApprovalService.ApproveAsync(contractEvent, approveContract);
                }

                break;

            case ContractEventType.Withdraw:
                await _contractWithdrawService.WithdrawAsync(contractEvent);

                break;

            default:
                throw new NotImplementedException($"[{nameof(ContractService)}] - [{nameof(ProcessMessage)}] does not have an implementation for event type [{eventType}].");
            }
        }
 private byte[] HandleFileNotFoundExceptionWithTestPdf <TException>(TException ex)
     where TException : Exception
 {
     if (_spConfig.ShouldErrorPdfNotFound)
     {
         throw ex;
     }
     else
     {
         using var stream = typeof(SharePointClientService).Assembly.GetManifestResourceStream(TestContractPdfFileName);
         if (stream is null)
         {
             throw new MissingManifestResourceException($"Failed to locate test contract PDF file ({TestContractPdfFileName}) in current assembly.");
         }
         else
         {
             _logger.LogWarning($"[{nameof(HandleFileNotFoundExceptionWithTestPdf)}] - The contract pdf file have been replaced by the test contract pdf file.");
             return(stream.ToByteArray());
         }
     }
 }