Exemplo n.º 1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await foreach (var order in _channel.ReadAllAsync())
            {
                using var scope = _serviceProvider.CreateScope();
                var context = scope.ServiceProvider.GetRequiredService <ShoppingDataContext>();

                var savedOrder = await context.CurbsideOrders.FindAsync(order.OrderId);

                var items = savedOrder.Items.Split(',').Count();
                for (var t = 0; t < items; t++)
                {
                    await Task.Delay(300);

                    if (order.ClientId != null)
                    {
                        await _hub.Clients.Client(order.ClientId).SendAsync("ItemProcessed", new { message = $"Processed item {t + 1}" });
                    }
                }
                savedOrder.Status = CurbsideOrderStatus.Approved;
                await context.SaveChangesAsync();

                if (order.ClientId != null)
                {
                    await _hub.Clients.Client(order.ClientId).SendAsync("OrderProcessed", savedOrder);
                }
            }
        }
Exemplo n.º 2
0
        protected async override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await foreach (var order in TheChannel.ReadAllAsync())
            {
                Logger.LogInformation("Got an Order to Process" + order.OrderId);
                using var scope = ServiceProvider.CreateScope();

                var context = scope.ServiceProvider.GetRequiredService <ShoppingDataContext>();

                var savedOrder = await context.CurbsideOrders.SingleAsync(o => o.Id == order.OrderId);

                var items = savedOrder.Items.Split(',').Count();

                for (var t = 0; t < items; t++)
                {
                    Logger.LogInformation("Processing an Item");
                    if (order.ClientId != null)
                    {
                        await CurbsideHub.Clients.Client(order.ClientId).SendAsync("OrderItemProcessed",
                                                                                   new { id = order.OrderId, itemId = t + 1 }
                                                                                   );
                    }
                    await Task.Delay(1000);
                }
                savedOrder.Status = CurbsideOrderStatus.Processed;
                await context.SaveChangesAsync();

                // Tell the client that made this request through a socket that it has been processed.
                if (order.ClientId != null)
                {
                    await CurbsideHub.Clients.Client(
                        order.ClientId).SendAsync("OrderProcessed", Mapper.Map <CurbsideOrder>(savedOrder));
                }
            }
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await foreach (var order in _channel.ReadAllAsync())
            {
                _logger.LogInformation($"got an order for {order.OrderId}");
                // look it up
                // process it
                // get the estimated pickup time
                // write it to the DB
                using var scope = _serviceProvider.CreateScope();
                var context = scope.ServiceProvider.GetRequiredService <ShoppingDataContext>();

                var savedOrder = await context.CurbsideOrders.SingleOrDefaultAsync(c => c.Id == order.OrderId);

                var items = savedOrder.Items.Split(',');
                foreach (var item in items)
                {
                    await Task.Delay(1000);                     // It's important!

                    _logger.LogInformation($"Processed item {item} for order {order.OrderId}");
                }

                var pickupGenerator = scope.ServiceProvider.GetRequiredService <IGenerateCurbsidePickupTimes>();
                savedOrder.PickupReadyAt = await pickupGenerator.GetPickupDate(savedOrder);

                await context.SaveChangesAsync();
            }
        }
Exemplo n.º 4
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await foreach (var order in _channel.ReadAllAsync())
            {
                using var scope = _serviceProvider.CreateScope();
                var context = scope.ServiceProvider.GetRequiredService <ShoppingDataContext>();

                var savedOrder = await context.CurbsideOrders.SingleOrDefaultAsync(c => c.Id == order.OrderId);

                var items = savedOrder.Items.Split(',').Count();
                for (var t = 0; t < items; t++)
                {
                    await Task.Delay(300);
                }
                savedOrder.Status = CurbsideOrderStatus.Approved;
                await context.SaveChangesAsync();
            }
        }
        protected async override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            // TODO: Talk About what you might want to do here. In a minute.
            await foreach (var order in _channel.ReadAllAsync())
            {
                using var scope = _serviceProvider.CreateScope();
                var context = scope.ServiceProvider.GetRequiredService <ShoppingDataContext>();

                var savedOrder = await context.CurbsideOrders.SingleOrDefaultAsync(o => o.Id == order.ReservationId);

                if (savedOrder == null)
                {
                    continue;
                }
                else
                {
                    var numberOfItems = savedOrder.Items.Split(',').Count();
                    for (var t = 0; t < numberOfItems; t++)
                    {
                        await Task.Delay(1000); // fake some long-running work.

                        if (order.ConnectionId != null)
                        {
                            await _hub.Clients.Client(order.ConnectionId).SendAsync("ItemProcessed",
                                                                                    new
                            {
                                message = $"Processed Item { t + 1} of your order",
                                orderId = order.ReservationId
                            });
                        }
                    }
                    savedOrder.Status     = CurbsideOrderStatus.Approved;
                    savedOrder.PickupDate = DateTime.Now.AddHours(numberOfItems);
                    if (order.ConnectionId != null)
                    {
                        await _hub.Clients.Client(order.ConnectionId).SendAsync("OrderProcessed", savedOrder);
                    }
                    await context.SaveChangesAsync();
                }
            }
        }