/// <summary> /// Hold the flights waiting untill it's there time to shine. /// </summary> /// <param name="flight">Flight to delay.</param> /// <exception cref="ArgumentNullException">Flight is null.</exception> /// <exception cref="KeyNotFoundException">Flight is aimed to a non existant control tower.</exception> private async void SendFlightToControlTowerAtTime(Flight flight) { if (flight is null) { throw new ArgumentNullException(nameof(flight), "Flight must not be null, we are not Malaysia airlines!"); } string controlTowerName = flight.Direction == FlightDirection.Landing ? flight.To : flight.From; IControlTowerService controlTowerService = stationTreeBuilder[controlTowerName] ?? throw new KeyNotFoundException("Control tower does not exist"); TimeSpan delayUntillFlight = flight.PlannedTime - DateTime.Now; notifier.NotifyFutureFlightAdded(flight); if (delayUntillFlight > TimeSpan.Zero) { await Task.Delay(delayUntillFlight); } logger.LogInformation($"Flight completed waiting and is now moving to control tower {controlTowerName}"); ILogger <IFlightService> flightLogger = loggerFactory.CreateLogger <IFlightService>(); controlTowerService.FlightArrived(new FlightService(flight, flightLogger)); }
public async Task HandleNewFlightArrivedAsync(Flight flight) { try { string controlTowerName = flight.Direction == FlightDirection.Landing ? flight.To : flight.From; IControlTowerService controlTowerService = stationTreeBuilder[controlTowerName] ?? throw new KeyNotFoundException("Control tower does not exist"); flight.ControlTowerId = controlTowerService.ControlTower.Id; Flight dbFlight = await flightRepository.AddAsync(flight); SendFlightToControlTowerAtTime(flight); } catch (DbUpdateException e) { logger.LogCritical(e, "A flight has been lost due to DB updating errors!", flight); throw; } catch (Exception e) { logger.LogCritical(e, "A flight has been lost due to unknown error!", flight); throw; } }