Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive.Slave/3.3/Manager/HeartbeatManager.cs @ 11082

Last change on this file since 11082 was 11082, checked in by ascheibe, 10 years ago

#2153

  • added a new method HandleStartStopPauseError in Executor to handle error conditions in the same way
  • added timeouts for semaphores so that failed tasks or tasks with endless loops don't block the slave
  • removed ExceptionOccured events from Executor/SlaveTask/TaskManager and use TaskFailed instead
  • removed another ExcpetionOccured event in HeartbeatManager that was never used
File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
26
27namespace HeuristicLab.Clients.Hive.SlaveCore {
28  /// <summary>
29  /// Heartbeat Manager sends every x ms a heartbeat to the server and receives a message.
30  /// The message is added to the MessageQueue from where the Core pulls them and decides what to do.
31  /// </summary>
32  public class HeartbeatManager {
33    private static object locker = new object();
34    private TimeSpan interval;
35
36    public TimeSpan Interval {
37      get { return interval; }
38      set {
39        interval = value;
40        Settings.Default.HeartbeatInterval = interval;
41        Settings.Default.Save();
42      }
43    }
44    private Thread heartBeatThread;
45    private AutoResetEvent waitHandle;
46    private WcfService wcfService;
47    private bool threadStopped;
48
49    public HeartbeatManager() {
50      interval = Settings.Default.HeartbeatInterval;
51    }
52
53    /// <summary>
54    /// Starts the Heartbeat signal.
55    /// </summary>
56    public void StartHeartbeat() {
57      this.waitHandle = new AutoResetEvent(true);
58      wcfService = WcfService.Instance;
59      threadStopped = false;
60      heartBeatThread = new Thread(RunHeartBeatThread);
61      heartBeatThread.Start();
62    }
63
64    /// <summary>
65    /// Stop the heartbeat
66    /// </summary>
67    public void StopHeartBeat() {
68      threadStopped = true;
69      waitHandle.Set();
70      heartBeatThread.Join();
71      waitHandle.Close();
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        try {
85          SlaveClientCom.Instance.StatusChanged(ConfigManager.Instance.GetStatusForClientConsole());
86        }
87        catch (Exception ex) {
88          EventLogManager.LogMessage("Couldn't sent status information to client ui. Exception is: " + Environment.NewLine + ex.ToString());
89        }
90
91        try {
92          lock (locker) {
93            if (wcfService.ConnState != NetworkEnum.WcfConnState.Connected) {
94              // login happens automatically upon successfull connection
95              wcfService.Connect(ConfigManager.Instance.GetClientInfo());
96              SlaveStatusInfo.LoginTime = DateTime.Now;
97            }
98            if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) {
99              Slave info = ConfigManager.Instance.GetClientInfo();
100
101              Heartbeat heartBeatData = new Heartbeat {
102                SlaveId = info.Id,
103                FreeCores = info.Cores.HasValue ? info.Cores.Value - SlaveStatusInfo.UsedCores : 0,
104                FreeMemory = ConfigManager.Instance.GetFreeMemory(),
105                CpuUtilization = ConfigManager.Instance.GetCpuUtilization(),
106                JobProgress = ConfigManager.Instance.GetExecutionTimeOfAllJobs(),
107                AssignJob = !ConfigManager.Instance.Asleep,
108                HbInterval = (int)interval.TotalSeconds
109              };
110
111              SlaveClientCom.Instance.LogMessage("Send HB: " + heartBeatData);
112              List<MessageContainer> msgs = wcfService.SendHeartbeat(heartBeatData);
113
114              if (msgs == null) {
115                SlaveClientCom.Instance.LogMessage("Error getting response from HB");
116              } else {
117                SlaveClientCom.Instance.LogMessage("HB Response received (" + msgs.Count + "): ");
118                msgs.ForEach(mc => SlaveClientCom.Instance.LogMessage(mc.Message.ToString()));
119                msgs.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
120              }
121            }
122          }
123        }
124        catch (Exception e) {
125          SlaveClientCom.Instance.LogMessage("Heartbeat thread failed: " + e.ToString());
126        }
127        waitHandle.WaitOne(this.interval);
128      }
129      SlaveClientCom.Instance.LogMessage("Heartbeat thread stopped");
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.