Exemple #1
0
 public void StopService()
 {
     lock (this)
     {
         m_eServiceStatus = FlareServiceStatus.StopRequested;
     }
 }
Exemple #2
0
        public void Run()
        {
            LogToConsole(@"Flare main loop: start");

            lock (this)
            {
                m_eServiceStatus = FlareServiceStatus.Started;
            }

            try
            {
                m_pqWaitingTargets.Clear();
                m_qTargetsReadyForTesting.Clear();

                ReadTargetsFromDatabase();

                MySqlConnection con = DatabaseConnectionProvider.GetMySqlConnection();

                m_lstAllContacts = Contact.GetAllContacts(con);
                m_dictSystemConfigurationEntries = SystemConfigurationEntry.GetDictionary(con);

                if (m_pqWaitingTargets.IsEmpty())
                {
                    LogToConsole(@"Aborting: The priority queue is empty; there are no servers to monitor.");
                    return;
                }

                // The main loop begins here.
                TimeSpan tsOneSecond  = new TimeSpan(0, 0, 1);
                TimeSpan tsLoopPeriod = new TimeSpan(0, 0, 1);
                bool     bLimitNumberOfMainLoopIterations = (NumberOfMainLoopIterations > 0);

                LogToConsole(@"Starting the main loop...");

                for (int nMainLoopIterationNumber = 0;
                     !bLimitNumberOfMainLoopIterations || nMainLoopIterationNumber < NumberOfMainLoopIterations;
                     ++nMainLoopIterationNumber)
                {
                    DateTime dtNow = DateTime.UtcNow;

                    LogToConsole(string.Format(@"It is now {0}.", dtNow.ToLongTimeString()));

                    if (m_pqWaitingTargets.IsEmpty())
                    {
                        LogToConsole(@"The priority queue is empty; sleeping for one second.");
                        System.Threading.Thread.Sleep(tsOneSecond);
                        continue;
                    }

                    Target targetHead = m_pqWaitingTargets.Peek();

                    LogToConsole(string.Format(@"The target at the head of the queue ('{0}') is due to be tested at {1}.",
                                               targetHead.Name, targetHead.DateTimeOfNextMonitor.ToLongTimeString()));

                    if (dtNow < targetHead.DateTimeOfNextMonitor)
                    {
                        TimeSpan tsDifference      = targetHead.DateTimeOfNextMonitor - dtNow;
                        TimeSpan tsTimeSpanToSleep = (tsDifference < tsLoopPeriod) ? tsDifference : tsLoopPeriod;

                        LogToConsole(string.Format(@"It is not yet time to test the target at the head of the queue.  Sleeping for {0} seconds and {1} milliseconds...",
                                                   tsTimeSpanToSleep.Seconds, tsTimeSpanToSleep.Milliseconds));

                        System.Threading.Thread.Sleep(tsTimeSpanToSleep);
                        continue;
                    }

                    lock (m_qTargetsReadyForTesting)
                    {
                        m_qTargetsReadyForTesting.Enqueue(m_pqWaitingTargets.Dequeue());
                    }

#if MULTITHREAD_TARGET_TESTING
                    // Note: There is also a delegate named ParameterizedThreadStart; perhaps it may be useful
                    // in allowing us to pass a TargetInfo object as a parameter, thus avoiding the m_qTargetsReadyForTesting queue.
                    Thread thread = new Thread(new ThreadStart(ThreadMain_TestTarget));

                    thread.Start();
#else
                    ThreadMain_TestTarget();
#endif

                    lock (this)
                    {
                        if (m_eServiceStatus == FlareServiceStatus.StopRequested)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogToConsole(string.Format(@"{0} caught: {1}", ex.GetType().FullName, ex.Message));
            }

            lock (this)
            {
                m_eServiceStatus = FlareServiceStatus.Stopped;
            }

            LogToConsole(@"Flare main loop: end");
        }