Changeset 4264 for branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.Core/3.3/Heartbeat.cs
- Timestamp:
- 08/19/10 13:30:56 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.Core/3.3/Heartbeat.cs
r4254 r4264 33 33 using HeuristicLab.Hive.Slave.Communication.ServerService; 34 34 using HeuristicLab.Tracing; 35 using System.Threading; 35 36 //using BO = HeuristicLab.Hive.Contracts.BusinessObjects; 36 37 … … 43 44 private bool offline; 44 45 45 public double Interval { get; set; } 46 private Timer heartbeatTimer = null; 46 public TimeSpan Interval { get; set; } 47 48 private Thread heartBeatThread; 47 49 48 50 private static object locker = new object(); 49 51 50 52 public Heartbeat() { 51 Interval = 100;53 Interval = new TimeSpan(0,0,10); 52 54 } 53 55 54 public Heartbeat( doubleinterval) {56 public Heartbeat(TimeSpan interval) { 55 57 Interval = interval; 56 58 } … … 58 60 private WcfService wcfService; 59 61 62 private bool abortThreadPending; 63 60 64 /// <summary> 61 65 /// Starts the Heartbeat signal. 62 66 /// </summary> 63 67 public void StartHeartbeat() { 64 heartbeatTimer = new System.Timers.Timer();65 heartbeatTimer.Interval = this.Interval;66 heartbeatTimer.AutoReset = true;67 heartbeatTimer.Elapsed += new ElapsedEventHandler(heartbeatTimer_Elapsed);68 68 wcfService = WcfService.Instance; 69 69 wcfService.ProcessHeartBeatCompleted += new EventHandler<ProcessHeartBeatCompletedEventArgs>(wcfService_ProcessHeartBeatCompleted); 70 heartbeatTimer.Start(); 70 abortThreadPending = false; 71 heartBeatThread = GetHeartBeatThread(); 72 heartBeatThread.Start(); 71 73 } 72 74 73 /// <summary> 74 /// This Method is called every time the timer ticks 75 /// </summary> 76 /// <param name="sender"></param> 77 /// <param name="e"></param> 78 void heartbeatTimer_Elapsed(object sender, ElapsedEventArgs e) { 79 lock (locker) { 80 // check if cwfService is disconnected for any reason (should happen at first heartbeat) 81 // [chn] TODO: Client should always send heartbeats. when calendar disallows he should tell the server he does not want to compute anything 82 if (wcfService.ConnState == NetworkEnum.WcfConnState.Disconnected && 83 (!UptimeManager.Instance.CalendarAvailable || UptimeManager.Instance.IsOnline())) { 84 wcfService.Connect(); 85 } 86 if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) { 87 wcfService.LoginSync(ConfigManager.Instance.GetClientInfo()); 88 } 89 90 ClientDto info = ConfigManager.Instance.GetClientInfo(); 91 92 PerformanceCounter counter = new PerformanceCounter("Memory", "Available Bytes", true); 93 int mb = (int)(counter.NextValue() / 1024 / 1024); 94 95 HeartBeatData heartBeatData = new HeartBeatData { 96 SlaveId = info.Id, 97 FreeCores = info.NrOfCores - ConfigManager.Instance.GetUsedCores(), 98 FreeMemory = mb, 99 JobProgress = ConfigManager.Instance.GetProgressOfAllJobs() 100 }; 101 102 DateTime lastFullHour = DateTime.Parse(DateTime.Now.Hour.ToString() + ":00"); 103 TimeSpan span = DateTime.Now - lastFullHour; 104 if (span.TotalSeconds < (Interval / 1000)) { 105 if (UptimeManager.Instance.IsOnline() && UptimeManager.Instance.CalendarAvailable) { 106 //That's quiet simple: Just reconnect and you're good for new jobs 107 if (wcfService.ConnState != NetworkEnum.WcfConnState.Loggedin) { 108 Logger.Info("Client goes online according to timetable"); 75 private Thread GetHeartBeatThread() { 76 return new Thread(() => { 77 while (!abortThreadPending) { 78 lock (locker) { 79 // check if cwfService is disconnected for any reason (should happen at first heartbeat) 80 // [chn] TODO: Client should always send heartbeats. when calendar disallows he should tell the server he does not want to compute anything 81 if (wcfService.ConnState == NetworkEnum.WcfConnState.Disconnected) { 109 82 wcfService.Connect(); 110 83 } 111 } else { 112 //We have quit a lot of work to do here: snapshot all jobs, submit them back, then disconnect and then pray to god that nothing goes wrong 113 MessageQueue.GetInstance().AddMessage(MessageContainer.MessageType.UptimeLimitDisconnect); 114 } 115 } 116 if (wcfService.ConnState == NetworkEnum.WcfConnState.Failed) { 117 wcfService.Connect(); 118 } else if (wcfService.ConnState == NetworkEnum.WcfConnState.Loggedin) { 119 Logger.Debug("Sending Heartbeat: " + heartBeatData); 120 wcfService.ProcessHeartBeatAsync(heartBeatData); 121 } 122 } 84 85 if (wcfService.ConnState == NetworkEnum.WcfConnState.Loggedin) { 86 87 // client is allowed to calculate stuff 88 ClientDto info = ConfigManager.Instance.GetClientInfo(); 89 90 HeartBeatData heartBeatData = new HeartBeatData { 91 SlaveId = info.Id, 92 FreeCores = info.NrOfCores - ConfigManager.Instance.GetUsedCores(), 93 FreeMemory = GetFreeMemory(), 94 JobProgress = ConfigManager.Instance.GetProgressOfAllJobs(), 95 IsAllowedToCalculate = UptimeManager.Instance.IsOnline() && UptimeManager.Instance.CalendarAvailable 96 }; 97 98 if (!heartBeatData.IsAllowedToCalculate) { 99 // stop all running jobs and send snapshots to server 100 MessageQueue.GetInstance().AddMessage(MessageContainer.MessageType.UptimeLimitDisconnect); 101 } 102 103 Logger.Debug("Sending Heartbeat: " + heartBeatData); 104 wcfService.ProcessHeartBeatAsync(heartBeatData); 105 } 106 107 } // lock 108 Thread.Sleep(Interval); 109 } // while 110 abortThreadPending = false; 111 }); 112 } 113 114 private int GetFreeMemory() { 115 PerformanceCounter counter = new PerformanceCounter("Memory", "Available Bytes", true); 116 int mb = (int)(counter.NextValue() / 1024 / 1024); 117 return mb; 123 118 } 124 119 … … 129 124 130 125 public void StopHeartBeat() { 131 heartbeatTimer.Dispose();126 abortThreadPending = true; 132 127 } 133 128 }
Note: See TracChangeset
for help on using the changeset viewer.