Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Clients.Hive.Slave/3.3/Manager/HeartbeatManager.cs @ 16811

Last change on this file since 16811 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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      waitHandle.Close();
73    }
74
75    /// <summary>
76    /// use this method to singalize there is work to do (to avoid the waiting period if its clear that actions are required)
77    /// </summary>
78    public void AwakeHeartBeatThread() {
79      if (!threadStopped)
80        waitHandle.Set();
81    }
82
83    private void RunHeartBeatThread() {
84      while (!threadStopped) {
85        try {
86          SlaveClientCom.Instance.StatusChanged(ConfigManager.Instance.GetStatusForClientConsole());
87        }
88        catch (Exception ex) {
89          EventLogManager.LogMessage("Couldn't sent status information to client ui. Exception is: " + Environment.NewLine + ex.ToString());
90        }
91
92        try {
93          lock (locker) {
94            if (wcfService.ConnState != NetworkEnum.WcfConnState.Connected) {
95              // login happens automatically upon successfull connection
96              wcfService.Connect(ConfigManager.Instance.GetClientInfo());
97              SlaveStatusInfo.LoginTime = DateTime.Now;
98            }
99            if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) {
100              Slave info = ConfigManager.Instance.GetClientInfo();
101
102              Heartbeat heartBeatData = new Heartbeat {
103                SlaveId = info.Id,
104                FreeCores = info.Cores.HasValue ? info.Cores.Value - SlaveStatusInfo.UsedCores : 0,
105                FreeMemory = ConfigManager.Instance.GetFreeMemory(),
106                CpuUtilization = ConfigManager.Instance.GetCpuUtilization(),
107                JobProgress = ConfigManager.Instance.GetExecutionTimeOfAllJobs(),
108                AssignJob = !ConfigManager.Instance.Asleep,
109                HbInterval = (int)interval.TotalSeconds
110              };
111
112              SlaveClientCom.Instance.LogMessage("Send HB: " + heartBeatData);
113              List<MessageContainer> msgs = wcfService.SendHeartbeat(heartBeatData);
114
115              if (msgs == null) {
116                SlaveClientCom.Instance.LogMessage("Error getting response from HB");
117                OnExceptionOccured(new Exception("Error getting response from HB"));
118              } else {
119                SlaveClientCom.Instance.LogMessage("HB Response received (" + msgs.Count + "): ");
120                msgs.ForEach(mc => SlaveClientCom.Instance.LogMessage(mc.Message.ToString()));
121                msgs.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
122              }
123            }
124          }
125        }
126        catch (Exception e) {
127          SlaveClientCom.Instance.LogMessage("Heartbeat thread failed: " + e.ToString());
128          OnExceptionOccured(e);
129        }
130        waitHandle.WaitOne(this.interval);
131      }
132      SlaveClientCom.Instance.LogMessage("Heartbeat thread stopped");
133    }
134
135    #region Eventhandler
136    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
137    private void OnExceptionOccured(Exception e) {
138      var handler = ExceptionOccured;
139      if (handler != null) handler(this, new EventArgs<Exception>(e));
140    }
141    #endregion
142  }
143}
Note: See TracBrowser for help on using the repository browser.