Пример #1
0
        public async Task HandleAsync(JObject jObject, CancellationToken cancellationToken)
        {
            var @event = jObject.ToObject <CustomerCreatedEvent>();

            var isnew       = tracer.CurrentTransaction == null;
            var transaction = isnew
                ? tracer.StartTransaction("CustomerCreatedEventHandler", "event-subscriber")
                              .WithLabel("data", jObject.ToString())
                              .WithLabel("event name", "CustomerCreatedEvent")
                : tracer.CurrentTransaction;

            logger.Information(@event);
            var apiSpan  = transaction.StartSpan($"{apis.Customer}/{@event.CustomerId}", ApiConstants.ActionQuery, subType: ApiConstants.SubtypeHttp);
            var customer = await httpClient.GetAsync <Customer>($"{apis.Customer}/{@event.CustomerId}").ConfigureAwait(false);

            apiSpan.SetLabel("Response", JObject.FromObject(customer).ToString());
            apiSpan?.End();
            logger.Information("customer service {api} {RequestMethod} {data}", apis.Customer, "GET", JObject.FromObject(customer).ToString());

            await repository.NewAsync(customer, cancellationToken);

            if (isnew)
            {
                transaction.End();
            }
        }
        public async Task <IActionResult> NewAsync([FromBody] Order order)
        {
            var isnew       = tracer.CurrentTransaction == null;
            var transaction = isnew
                ? tracer.StartTransaction($"NewOrder", "post")
                              .WithLabel("Order: OrderId", order.Id.ToString())
                              .WithLabel("Order: Product", order.Product)
                              .WithLabel("Order: Quantity", order.Quantity.ToString())
                              .WithLabel("Order: Customer", order.CustomerId.ToString())
                : tracer.CurrentTransaction;


            if (order == default(Order))
            {
                return(BadRequest("Body empty or null"));
            }

            order.Id = Guid.NewGuid();

            var customer = await customerRepository.GetByIdAsync(order.CustomerId, CancellationToken.None);

            if (customer == default)
            {
                return(NotFound("Customer"));
            }

            await repository.NewAsync(order, CancellationToken.None);

            messageProducer.SendMessage(new OrderCreatedEvent
            {
                OrderId  = order.Id,
                Product  = order.Product,
                Quantity = order.Quantity
            });

            var span = transaction.StartSpan($"{apis.Inventory}/{order.Product}", ApiConstants.ActionQuery, subType: ApiConstants.SubtypeHttp);
            var availableQuantity = await httpClient.GetAsync <int>($"{apis.Inventory}/{order.Product}").ConfigureAwait(false);

            span.SetLabel("Response", availableQuantity.ToString());
            span?.End();
            logger.LogInformation("inventory service {api} {RequestMethod} {data}", apis.Inventory, "GET", availableQuantity.ToString());

            if (availableQuantity >= order.Quantity)
            {
                await repository.ProcessOrderAsync(order.Id, CancellationToken.None);

                messageProducer.SendMessage(new OrderProcessedEvent
                {
                    OrderId  = order.Id,
                    Product  = order.Product,
                    Quantity = order.Quantity
                });
            }
            else
            {
                await repository.CancelOrderAsync(order.Id, CancellationToken.None);

                messageProducer.SendMessage(new OrderCancelledEvent
                {
                    OrderId = order.Id,
                    Product = order.Product
                });
            }
            if (isnew)
            {
                transaction.End();
            }
            return(Ok());
        }