Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • some Admin UI bugfixes

Slave:

  • fixed bug when Pause is called immediately after Calculate
  • send exceptions when something goes wrong in Pause or Stop
File size: 4.9 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    public TimeSpan Interval { get; set; }
36    private Thread heartBeatThread;
37    private AutoResetEvent waitHandle;
38    private WcfService wcfService;
39    private bool threadStopped;
40
41    public HeartbeatManager() {
42      Interval = Settings.Default.HeartbeatInterval;
43    }
44
45    /// <summary>
46    /// Starts the Heartbeat signal.
47    /// </summary>
48    public void StartHeartbeat() {
49      this.waitHandle = new AutoResetEvent(true);
50      wcfService = WcfService.Instance;
51      threadStopped = false;
52      heartBeatThread = new Thread(RunHeartBeatThread);
53      heartBeatThread.Start();
54    }
55
56    /// <summary>
57    /// Stop the heartbeat
58    /// </summary>
59    public void StopHeartBeat() {
60      threadStopped = true;
61      waitHandle.Set();
62      heartBeatThread.Join();
63    }
64
65    /// <summary>
66    /// use this method to singalize there is work to do (to avoid the waiting period if its clear that actions are required)
67    /// </summary>
68    public void AwakeHeartBeatThread() {
69      if (!threadStopped)
70        waitHandle.Set();
71    }
72
73    private void RunHeartBeatThread() {
74      while (!threadStopped) {
75        SlaveClientCom.Instance.ClientCom.StatusChanged(ConfigManager.Instance.GetStatusForClientConsole());
76
77        try {
78          lock (locker) {
79            if (wcfService.ConnState != NetworkEnum.WcfConnState.Connected) {
80              // login happens automatically upon successfull connection
81              wcfService.Connect(ConfigManager.Instance.GetClientInfo());
82              SlaveStatusInfo.LoginTime = DateTime.Now;
83            }
84            if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) {
85              Slave info = ConfigManager.Instance.GetClientInfo();
86
87              Heartbeat heartBeatData = new Heartbeat {
88                SlaveId = info.Id,
89                FreeCores = info.Cores.HasValue ? info.Cores.Value - SlaveStatusInfo.UsedCores : 0,
90                FreeMemory = ConfigManager.GetFreeMemory(),
91                CpuUtilization = ConfigManager.Instance.GetCpuUtilization(),
92                JobProgress = ConfigManager.Instance.GetExecutionTimeOfAllJobs(),
93                AssignJob = !ConfigManager.Instance.Asleep
94              };
95
96              SlaveClientCom.Instance.ClientCom.LogMessage("Send HB: " + heartBeatData);
97              List<MessageContainer> msgs = wcfService.SendHeartbeat(heartBeatData);
98
99              if (msgs == null) {
100                SlaveClientCom.Instance.ClientCom.LogMessage("Error getting response from HB");
101                OnExceptionOccured(new Exception("Error getting response from HB"));
102              } else {
103                SlaveClientCom.Instance.ClientCom.LogMessage("HB Response received (" + msgs.Count + "): ");
104                msgs.ForEach(mc => SlaveClientCom.Instance.ClientCom.LogMessage(mc.Message.ToString()));
105                msgs.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
106              }
107            }
108          }
109        }
110        catch (Exception e) {
111          SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread failed: " + e.ToString());
112          OnExceptionOccured(e);
113        }
114        waitHandle.WaitOne(this.Interval);
115      }
116      waitHandle.Close();
117      SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread stopped");
118    }
119
120    #region Eventhandler
121    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
122    private void OnExceptionOccured(Exception e) {
123      var handler = ExceptionOccured;
124      if (handler != null) handler(this, new EventArgs<Exception>(e));
125    }
126    #endregion
127  }
128}
Note: See TracBrowser for help on using the repository browser.