Exemplo n.º 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();
            }
        }
Exemplo n.º 2
0
        public async Task HandleAsync(JObject jObject, CancellationToken cancellationToken)
        {
            var @event = jObject.ToObject <OrderProcessedEvent>();

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

                : tracer.CurrentTransaction;

            logger.Information(@event);
            var currentQuantity = await repository.GetAvailableQuantityAsync(@event.Product, cancellationToken);

            await repository.UpdateAsync(@event.Product, @event.Quantity, cancellationToken);

            messageProducer.SendMessage(new InventoryUpdatedEvent
            {
                Product  = @event.Product,
                Quantity = currentQuantity - @event.Quantity
            });

            if (isnew)
            {
                transaction.End();
            }
        }