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 @ 6371

Last change on this file since 6371 was 6371, checked in by ascheibe, 13 years ago

#1233

  • code cleanups for slave review
  • added switch between privileged and unprivileged sandbox
  • removed childjob management because it's not used
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.Common;
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    public TimeSpan Interval { get; set; }
35    private Thread heartBeatThread;
36    private AutoResetEvent waitHandle;
37    private WcfService wcfService;
38    private bool threadStopped;
39
40    public HeartbeatManager() {
41      Interval = new TimeSpan(0, 0, 10);
42    }
43
44    public HeartbeatManager(TimeSpan interval) {
45      Interval = interval;
46    }
47
48    /// <summary>
49    /// Starts the Heartbeat signal.
50    /// </summary>
51    public void StartHeartbeat() {
52      this.waitHandle = new AutoResetEvent(true);
53      wcfService = WcfService.Instance;
54      threadStopped = false;
55      heartBeatThread = new Thread(RunHeartBeatThread);
56      heartBeatThread.Start();
57    }
58
59    /// <summary>
60    /// Stop the heartbeat
61    /// </summary>
62    public void StopHeartBeat() {
63      threadStopped = true;
64      waitHandle.Set();
65      heartBeatThread.Join();
66    }
67
68    /// <summary>
69    /// use this method to singalize there is work to do (to avoid the waiting period if its clear that actions are required)
70    /// </summary>
71    public void AwakeHeartBeatThread() {
72      if (!threadStopped)
73        waitHandle.Set();
74    }
75
76    private void RunHeartBeatThread() {
77      while (!threadStopped) {
78        SlaveClientCom.Instance.ClientCom.StatusChanged(ConfigManager.Instance.GetStatusForClientConsole());
79
80        try {
81          lock (locker) {
82            if (wcfService.ConnState != NetworkEnum.WcfConnState.Connected) {
83              // login happens automatically upon successfull connection
84              wcfService.Connect(ConfigManager.Instance.GetClientInfo());
85              SlaveStatusInfo.LoginTime = DateTime.Now;
86            }
87            if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) {
88              Slave info = ConfigManager.Instance.GetClientInfo();
89
90              Heartbeat heartBeatData = new Heartbeat {
91                SlaveId = info.Id,
92                FreeCores = info.Cores.HasValue ? info.Cores.Value - SlaveStatusInfo.UsedCores : 0,
93                FreeMemory = ConfigManager.GetFreeMemory(),
94                CpuUtilization = ConfigManager.Instance.GetCpuUtilization(),
95                JobProgress = ConfigManager.Instance.GetExecutionTimeOfAllJobs(),
96                AssignJob = !ConfigManager.Instance.Asleep
97              };
98
99              SlaveClientCom.Instance.ClientCom.LogMessage("Send HB: " + heartBeatData);
100              List<MessageContainer> msgs = wcfService.SendHeartbeat(heartBeatData);
101
102              if (msgs == null) {
103                SlaveClientCom.Instance.ClientCom.LogMessage("Error getting response from HB");
104                OnExceptionOccured(new Exception("Error getting response from HB"));
105              } else {
106                SlaveClientCom.Instance.ClientCom.LogMessage("HB Response received (" + msgs.Count + "): ");
107                msgs.ForEach(mc => SlaveClientCom.Instance.ClientCom.LogMessage(mc.Message.ToString()));
108                msgs.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
109              }
110            }
111          }
112        }
113        catch (Exception e) {
114          SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread failed: " + e.ToString());
115          OnExceptionOccured(e);
116        }
117        waitHandle.WaitOne(this.Interval);
118      }
119      waitHandle.Close();
120      SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread stopped");
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.