Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • HeartbeatManager: don't sleep while starting jobs
  • Executor: make Start() blocking
  • shutdown properly if an uncaught exception is thrown
File size: 4.8 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 - ConfigManager.Instance.GetUsedCores() : 0,
92                FreeMemory = ConfigManager.GetFreeMemory(),
93                JobProgress = ConfigManager.Instance.GetExecutionTimeOfAllJobs(),
94                AssignJob = true //TODO: check if we want another job
95              };
96
97              SlaveClientCom.Instance.ClientCom.LogMessage("Sending Heartbeat: " + heartBeatData);
98              List<MessageContainer> msgs = wcfService.SendHeartbeat(heartBeatData);
99
100              if (msgs == null) {
101                SlaveClientCom.Instance.ClientCom.LogMessage("Error getting response from Heartbeat");
102                OnExceptionOccured(new Exception("Error getting response from Heartbeat"));
103              } else {
104                SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat Response received (" + msgs.Count + "): ");
105                msgs.ForEach(mc => SlaveClientCom.Instance.ClientCom.LogMessage(mc.Message.ToString()));
106                msgs.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
107              }
108            }
109          }
110        }
111        catch (Exception e) {
112          SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread failed: " + e.ToString());
113          OnExceptionOccured(e);
114        }
115        waitHandle.WaitOne(this.Interval);
116      }
117      waitHandle.Close();
118      SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread stopped");
119    }
120
121
122    #region Eventhandler
123    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
124    private void OnExceptionOccured(Exception e) {
125      var handler = ExceptionOccured;
126      if (handler != null) handler(this, new EventArgs<Exception>(e));
127    }
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.