Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.3/Manager/HeartbeatManager.cs @ 6893

Last change on this file since 6893 was 6893, checked in by ascheibe, 12 years ago

#1233 server can now control the slave heartbeat interval

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Threading;
25using HeuristicLab.Clients.Hive.SlaveCore.Properties;
26using HeuristicLab.Common;
27
28namespace HeuristicLab.Clients.Hive.SlaveCore {
29  /// <summary>
30  /// Heartbeat Manager sends every x ms a heartbeat to the server and receives a message.
31  /// The message is added to the MessageQueue from where the Core pulls them and decides what to do.
32  /// </summary>
33  public class HeartbeatManager {
34    private static object locker = new object();
35    private TimeSpan interval;
36
37    public TimeSpan Interval {
38      get { return interval; }
39      set {
40        interval = value;
41        Settings.Default.HeartbeatInterval = interval;
42        Settings.Default.Save();
43      }
44    }
45    private Thread heartBeatThread;
46    private AutoResetEvent waitHandle;
47    private WcfService wcfService;
48    private bool threadStopped;
49
50    public HeartbeatManager() {
51      interval = Settings.Default.HeartbeatInterval;
52    }
53
54    /// <summary>
55    /// Starts the Heartbeat signal.
56    /// </summary>
57    public void StartHeartbeat() {
58      this.waitHandle = new AutoResetEvent(true);
59      wcfService = WcfService.Instance;
60      threadStopped = false;
61      heartBeatThread = new Thread(RunHeartBeatThread);
62      heartBeatThread.Start();
63    }
64
65    /// <summary>
66    /// Stop the heartbeat
67    /// </summary>
68    public void StopHeartBeat() {
69      threadStopped = true;
70      waitHandle.Set();
71      heartBeatThread.Join();
72    }
73
74    /// <summary>
75    /// use this method to singalize there is work to do (to avoid the waiting period if its clear that actions are required)
76    /// </summary>
77    public void AwakeHeartBeatThread() {
78      if (!threadStopped)
79        waitHandle.Set();
80    }
81
82    private void RunHeartBeatThread() {
83      while (!threadStopped) {
84        SlaveClientCom.Instance.ClientCom.StatusChanged(ConfigManager.Instance.GetStatusForClientConsole());
85
86        try {
87          lock (locker) {
88            if (wcfService.ConnState != NetworkEnum.WcfConnState.Connected) {
89              // login happens automatically upon successfull connection
90              wcfService.Connect(ConfigManager.Instance.GetClientInfo());
91              SlaveStatusInfo.LoginTime = DateTime.Now;
92            }
93            if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) {
94              Slave info = ConfigManager.Instance.GetClientInfo();
95
96              Heartbeat heartBeatData = new Heartbeat {
97                SlaveId = info.Id,
98                FreeCores = info.Cores.HasValue ? info.Cores.Value - SlaveStatusInfo.UsedCores : 0,
99                FreeMemory = ConfigManager.GetFreeMemory(),
100                CpuUtilization = ConfigManager.Instance.GetCpuUtilization(),
101                JobProgress = ConfigManager.Instance.GetExecutionTimeOfAllJobs(),
102                AssignJob = !ConfigManager.Instance.Asleep,
103                HbInterval = (int)interval.TotalSeconds
104              };
105
106              SlaveClientCom.Instance.ClientCom.LogMessage("Send HB: " + heartBeatData);
107              List<MessageContainer> msgs = wcfService.SendHeartbeat(heartBeatData);
108
109              if (msgs == null) {
110                SlaveClientCom.Instance.ClientCom.LogMessage("Error getting response from HB");
111                OnExceptionOccured(new Exception("Error getting response from HB"));
112              } else {
113                SlaveClientCom.Instance.ClientCom.LogMessage("HB Response received (" + msgs.Count + "): ");
114                msgs.ForEach(mc => SlaveClientCom.Instance.ClientCom.LogMessage(mc.Message.ToString()));
115                msgs.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
116              }
117            }
118          }
119        }
120        catch (Exception e) {
121          SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread failed: " + e.ToString());
122          OnExceptionOccured(e);
123        }
124        waitHandle.WaitOne(this.interval);
125      }
126      waitHandle.Close();
127      SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread stopped");
128    }
129
130    #region Eventhandler
131    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
132    private void OnExceptionOccured(Exception e) {
133      var handler = ExceptionOccured;
134      if (handler != null) handler(this, new EventArgs<Exception>(e));
135    }
136    #endregion
137  }
138}
Note: See TracBrowser for help on using the repository browser.