Ejemplo n.º 1
0
        private void Heartbeat()
        {
            int pingFailCount = 0;

            logger.Info("HeartBeat Thread Started.");

            //heart-beat thread handles its own errors.
            try
            {
                HeartbeatInfo info = new HeartbeatInfo();
                info.Interval = _HeartbeatInterval;

                // init for cpu usage
                TimeSpan oldTime      = Process.GetCurrentProcess().TotalProcessorTime;
                DateTime timeMeasured = DateTime.Now;
                TimeSpan newTime      = new TimeSpan(0);

                //TODO: be language neutral here. how??
                // init for cpu avail
                PerformanceCounter cpuCounter = new PerformanceCounter();
                cpuCounter.ReadOnly     = true;
                cpuCounter.CategoryName = "Processor";
                cpuCounter.CounterName  = "% Processor Time";
                cpuCounter.InstanceName = "_Total";

                while (!_stop)
                {
                    // calculate usage
                    newTime = Process.GetCurrentProcess().TotalProcessorTime;
                    TimeSpan absUsage = newTime - oldTime;
                    float    flUsage  = ((float)absUsage.Ticks / (DateTime.Now - timeMeasured).Ticks) * 100;
                    info.PercentUsedCpuPower = (int)flUsage > 100 ? 100 : (int)flUsage;
                    info.PercentUsedCpuPower = (int)flUsage < 0 ? 0 : (int)flUsage;
                    timeMeasured             = DateTime.Now;
                    oldTime = newTime;

                    try
                    {
                        // calculate avail
                        info.PercentAvailCpuPower = 100 - (int)cpuCounter.NextValue() + info.PercentUsedCpuPower;
                        info.PercentAvailCpuPower = info.PercentAvailCpuPower > 100 ? 100 : info.PercentAvailCpuPower;
                        info.PercentAvailCpuPower = info.PercentAvailCpuPower < 0 ? 0 : info.PercentAvailCpuPower;
                    }
                    catch (Exception e)
                    {
                        logger.Debug("HeartBeat thread error: " + e.Message + Environment.NewLine + " Heartbeat continuing after error...");
                    }

                    //have a seperate try...block since we try 3 times before giving up
                    try
                    {
                        //send a heartbeat to the manager.
                        _executor.Manager.Executor_Heartbeat(_executor.Credentials, _executor.Id, info);
                        pingFailCount = 0;
                    }
                    catch (Exception e)
                    {
                        if (e is SocketException || e is System.Runtime.Remoting.RemotingException)
                        {
                            pingFailCount++;
                            //we disconnect the executor if the manager cant be pinged three times
                            if (pingFailCount >= 3)
                            {
                                logger.Error("Failed to contact manager " + pingFailCount + " times...", e);

                                //the disconnect here should be started off on a seperate thread because:
                                //disconnect itself waits for HeartBeatThread to stop. If the method call
                                //to disconnect from HeartBeat wont return immediately, then there is a deadlock
                                //with disconnect waiting for the HeartBeatThread to stop and the HeartBeatThread waiting
                                //for the call to disconnect to return.

                                //raise the event to indicate that the Executor has got disconnected.
                                _executor.OnGotDisconnected();
                            }
                        }
                        else if (e is InvalidExecutorException)
                        {
                            //raise a disconnected event.
                            //so that we will reconnect if needed.
                            logger.Error("Invalid Executor exception : ", e);
                            _executor.OnGotDisconnected();
                        }
                        else
                        {
                            logger.Debug("Error during heartbeat. Continuing after error...", e);
                        }
                    }

                    Thread.Sleep(_HeartbeatInterval * 1000);
                }
            }
            catch (ThreadAbortException)
            {
                Thread.ResetAbort();
                logger.Info("HeartBeat thread aborted.");
            }
            catch (Exception e)
            {
                logger.Error("HeartBeat Exception. Heartbeat thread stopping...", e);
            }

            logger.Info("HeartBeat Thread Exited.");
        }
Ejemplo n.º 2
0
        private void NonDedicatedMonitor()
        {
            bool gotDisconnected = false;

            logger.Info("NonDedicatedMonitor Thread Started.");
            try
            {
                _ExecutingNonDedicated = true;
                while (!gotDisconnected && !_stop)
                {
                    try
                    {
                        //_ReadyToExecute.WaitOne();

                        //get the next thread-id from the manager - only if we can execute something here.
                        //in non-dedicated mode...the executor pulls threads instead of the manager giving it threads to execute.
                        ThreadIdentifier ti = _executor.Manager.Executor_GetNextScheduledThreadIdentifier(_executor.Credentials, _executor.Id);

                        if (ti == null)
                        {
                            //if there is no thread to execute, sleep for a random time and ask again later
                            Random rnd = new Random();
                            Thread.Sleep(rnd.Next(_EmptyThreadInterval, _EmptyThreadInterval * 2));
                        }
                        else
                        {
                            logger.Debug("Non-dedicated mode: Calling own method to execute thread");
                            _executor.Manager_ExecuteThread(ti);
                        }
                    }
                    catch (SocketException se)
                    {
                        gotDisconnected = true;
                        logger.Error("Got disconnected. Non-dedicated mode.", se);
                    }
                    catch (System.Runtime.Remoting.RemotingException re)
                    {
                        gotDisconnected = true;
                        logger.Error("Got disconnected. Non-dedicated mode.", re);
                    }
                }

                // got disconnected
                _ExecutingNonDedicated = false;

                //_executor.OnNonDedicatedExecuting

                logger.Debug("Non-dedicated executor: Unremoting self");
                _executor.DisconnectNDE();

                //raise the event for non-dedicated mode only.
                _executor.OnGotDisconnected();
            }
            catch (ThreadAbortException)
            {
                _ExecutingNonDedicated = false;
                logger.Warn("ThreadAbortException. Non-dedicated executor");
                Thread.ResetAbort();
            }
            catch (Exception e)
            {
                logger.Error("Error in non-dedicated monitor: " + e.Message, e);
            }

            logger.Info("NonDedicatedMonitor Thread Exited.");
        }