Ejemplo n.º 1
0
        /// <summary>
        /// Back-up method that will run each day at a certain time
        /// </summary>
        /// <param name="timeSpan">The time of the day the method will run</param>
        private static void StoreFlightDetailsHistory(object timeSpan)
        {
            //If the format of the invokation time is not correct the method will not run
            if (!TimeSpan.TryParse(timeSpan.ToString(), out TimeSpan ts))
            {
                _logger.Error("Time to backup flights details history is configured in wrong format");
                return;
            }

            //check how many seconds left till backup
            double secondsToGo = (ts - DateTime.Now.TimeOfDay).TotalSeconds;

            if (secondsToGo < 0)//if the seconds number is negative (meaning the tome is passed for today), add 24 hours
            {
                secondsToGo += (24 * 60 * 60);
            }

            Thread.Sleep(new TimeSpan(0, 0, (int)secondsToGo));//Sleeo till invokation time
            _logger.Debug($"Backup will start in {secondsToGo} seconds");

            while (true)//will run each 24 hours while the system us up
            {
                _logger.Info("Starting backup...");

                IFlightDAO flightDAO = new FlightDAOPGSQL();
                ITicketDAO ticketDAO = new TicketDAOPGSQL();
                IFlightsTicketsHistoryDAO flightsTicketsHistoryDAO = new FlightsTicketsHistoryDAOPGSQL();
                var flights_with_tickets = flightDAO.GetFlightsWithTicketsAfterLanding(3 * 60 * 60);//get all flight with tickets that landed 3+ hours ago inside dictionary
                int flights_count        = 0;
                int tickets_count        = 0;

                foreach (Flight flight in flights_with_tickets.Keys)                     //Run over all the keys (flights) in the dictionary
                {
                    flightsTicketsHistoryDAO.Add(flight, FlightStatus.Landed);           //Add the flight to history table

                    foreach (Ticket ticket in flights_with_tickets[flight])              //Run over all the tickets of the flight
                    {
                        if (ticket.Id != 0)                                              //If there is no tickets associated with the flight there will be one ticket with id, we don't want to add this ticket to the history
                        {
                            flightsTicketsHistoryDAO.Add(ticket, TicketStatus.Redeemed); //Add ticket to history table
                            ticketDAO.Remove(ticket);                                    //Remove the ticket from original table
                            tickets_count++;
                        }
                    }

                    flightDAO.Remove(flight);//Remove the flight from original table
                    flights_count++;
                }

                _logger.Info($"Backed up {flights_count} flights and {tickets_count} tickets");
                Thread.Sleep(new TimeSpan(24, 0, 0));
            }
        }
        private FlightCenterSystem()
        {
            Thread thread = new Thread(() =>
            {
                FlightDAOPGSQL flightDAOPGSQL = new FlightDAOPGSQL();
                TicketDAOPGSQL ticketDAOPGSQL = new TicketDAOPGSQL();
                while (true)
                {
                    try
                    {
                        if (DateTime.Now.TimeOfDay == AppConfig.Instance.WakingUpTime.TimeOfDay)
                        {
                            DateTime date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                                                         DateTime.Now.Hour - 3, DateTime.Now.Minute, DateTime.Now.Second);
                            IList <Flight> flights = flightDAOPGSQL.GetOldFlights(date);
                            foreach (Flight flight in flights)
                            {
                                IList <Ticket> tickets = ticketDAOPGSQL.GetTicketsByFlight(flight);
                                foreach (Ticket ticket in tickets)
                                {
                                    ticketDAOPGSQL.Add_To_Tickets_History(ticket);
                                    ticketDAOPGSQL.Remove(ticket);
                                }
                                flightDAOPGSQL.Add_to_flights_history(flight);
                                flightDAOPGSQL.Remove(flight);
                            }
                            log.Info($"Old flights and ticket were transformed to archive at {DateTime.Now.Date}");
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error($"Could not transform old tickets and flights to the archive: {ex.Message}");
                    }
                }
            });

            thread.Start();
        }