Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/HeartbeatManager.cs @ 6269

Last change on this file since 6269 was 6269, checked in by cneumuel, 13 years ago

#1233

  • added CpuUtilization to heartbeats
File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Common;
26
27namespace HeuristicLab.Clients.Hive.SlaveCore {
28  /// <summary>
29  /// Heartbeat class. It sends every x ms a heartbeat to the server and receives a Message
30  /// </summary>
31  public class HeartbeatManager {
32    private static object locker = new object();
33    public TimeSpan Interval { get; set; }
34    private Thread heartBeatThread;
35    private AutoResetEvent waitHandle;
36    private WcfService wcfService;
37    private bool threadStopped;
38
39    public HeartbeatManager() {
40      Interval = new TimeSpan(0, 0, 10);
41    }
42
43    public HeartbeatManager(TimeSpan interval) {
44      Interval = interval;
45    }
46
47    /// <summary>
48    /// Starts the Heartbeat signal.
49    /// </summary>
50    public void StartHeartbeat() {
51      this.waitHandle = new AutoResetEvent(true);
52      wcfService = WcfService.Instance;
53      threadStopped = false;
54      heartBeatThread = new Thread(RunHeartBeatThread);
55      heartBeatThread.Start();
56    }
57
58    /// <summary>
59    /// Stop the heartbeat
60    /// </summary>
61    public void StopHeartBeat() {
62      threadStopped = true;
63      waitHandle.Set();
64      heartBeatThread.Join();
65    }
66
67    /// <summary>
68    /// use this method to singalize there is work to do (to avoid the waiting period if its clear that actions are required)
69    /// </summary>
70    public void AwakeHeartBeatThread() {
71      if (!threadStopped)
72        waitHandle.Set();
73    }
74
75    private void RunHeartBeatThread() {
76      while (!threadStopped) {
77        SlaveClientCom.Instance.ClientCom.StatusChanged(ConfigManager.Instance.GetStatusForClientConsole());
78
79        try {
80          lock (locker) {
81            if (wcfService.ConnState != NetworkEnum.WcfConnState.Connected) {
82              // login happens automatically upon successfull connection
83              wcfService.Connect(ConfigManager.Instance.GetClientInfo());
84              SlaveStatusInfo.LoginTime = DateTime.Now;
85            }
86            if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) {
87              Slave info = ConfigManager.Instance.GetClientInfo();
88             
89              Heartbeat heartBeatData = new Heartbeat {
90                SlaveId = info.Id,
91                FreeCores = info.Cores.HasValue ? info.Cores.Value - SlaveStatusInfo.UsedCores : 0,
92                FreeMemory = ConfigManager.GetFreeMemory(),
93                CpuUtilization = ConfigManager.Instance.GetCpuUtilization(),
94                JobProgress = ConfigManager.Instance.GetExecutionTimeOfAllJobs(),
95                AssignJob = !ConfigManager.Instance.Asleep
96              };
97
98              SlaveClientCom.Instance.ClientCom.LogMessage("Sending Heartbeat: " + heartBeatData);
99              List<MessageContainer> msgs = wcfService.SendHeartbeat(heartBeatData);
100
101              if (msgs == null) {
102                SlaveClientCom.Instance.ClientCom.LogMessage("Error getting response from Heartbeat");
103                OnExceptionOccured(new Exception("Error getting response from Heartbeat"));
104              } else {
105                SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat Response received (" + msgs.Count + "): ");
106                msgs.ForEach(mc => SlaveClientCom.Instance.ClientCom.LogMessage(mc.Message.ToString()));
107                msgs.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
108              }
109            }
110          }
111        }
112        catch (Exception e) {
113          SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread failed: " + e.ToString());
114          OnExceptionOccured(e);
115        }
116        waitHandle.WaitOne(this.Interval);
117      }
118      waitHandle.Close();
119      SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread stopped");
120    }
121
122
123    #region Eventhandler
124    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
125    private void OnExceptionOccured(Exception e) {
126      var handler = ExceptionOccured;
127      if (handler != null) handler(this, new EventArgs<Exception>(e));
128    }
129    #endregion
130  }
131}
Note: See TracBrowser for help on using the repository browser.