protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var consumerHelper = new ConsumerWrapper("libraryreservations", _config); _logger.LogInformation("The Service is running and waiting for reservations"); while (!stoppingToken.IsCancellationRequested) { var order = consumerHelper.ReadMessage <ReservationMessage>(); _logger.LogInformation($"Got a reservation for {order.For} for the items {order.Items}"); var numberOfItems = order.Items.Split(',').Count(); await Task.Delay(1000 *numberOfItems); if (numberOfItems % 2 == 0) { await _httpService.MarkReservationAccepted(order); _logger.LogInformation("\tApproved that order."); } else { await _httpService.MarkReservationRejected(order); _logger.LogWarning("\tRejected that one!"); } } }
public async override Task <bool> Process(string message) { // read the message - // 1. Deserialize the JSON into a class. Logger.LogInformation($"Got the JSON from the Message Queue: {message}"); var request = JsonSerializer.Deserialize <ReservationModel>(message); // 2. Apply business rules to processing the reservation if (request.Books.Split(",").Length > 3) { // - Any reservation > 3 books gets cancelled. Otherwise, approved. return(await HttpService.MarkReservationCancelled(request)); } else { // 3. Post to the appropriate route on our API. return(await HttpService.MarkReservationAccepted(request)); } return(true); //if you did some error handling, and you had an error, return false. It will leave it on the Queue. }
public async override Task <bool> Process(string message) { //1. Deserialize the message (JSON document) var request = JsonSerializer.Deserialize <Reservation>(message); Logger.LogInformation($"Got a reservation for {request.For}"); //2. Decide if it is approved or denied var count = request.Books.Split(',').Length; if (count % 2 == 0) { return(await Service.MarkReservationAccepted(request)); } else { return(await Service.MarkReservationDenied(request)); } // return(true); }
public override async Task <bool> Process(string message) { // 1. deserialize from the json into a .net object var request = JsonSerializer.Deserialize <ReservationMessage>(message); // 2. maybe log it so we can see it. Logger.LogInformation($"Got a reservation for {request.For}"); // 3. do the business stuff. process the reservation. // (reservations with an even nuber of books get approved, otherwise, they are denied) var isOk = request.Books.Split(',').Count() % 2 == 0; // 4. tell the api about it. if (isOk) { return(await Service.MarkReservationAccepted(request)); } else { return(await Service.MarkReservationDenied(request)); } }