AgentWatchdog.hpp 851 Bytes
Newer Older
1
2
3
4
5
6
7
8
#pragma once

#include "Agent.hpp"
#include "utils/Timer.hpp"

namespace cta { namespace objectstore {
class AgentWatchdog {
public:
9
10
  AgentWatchdog(const std::string & name, Backend & os): m_agent(name, os), 
    m_heartbeatCounter(readHeartbeat()), m_timeout(5.0) {}
11
12
13
  
  bool checkAlive() {
    uint64_t newHeartBeatCount = readHeartbeat();
14
    if (newHeartBeatCount == m_heartbeatCounter && m_timer.secs() > m_timeout)
15
16
17
18
19
      return false;
    m_heartbeatCounter = newHeartBeatCount;
    m_timer.reset();
    return true; 
  }
20
21
22
23
24
  
  void setTimeout(double timeout) {
    m_timeout = timeout;
  }
  
25
26
private:
  cta::utils::Timer m_timer;
27
  Agent m_agent;
28
  uint64_t m_heartbeatCounter;
29
  double m_timeout;
30
31
32
33
34
35
36
37
38
  
  uint64_t readHeartbeat() {
    ScopedSharedLock lock(m_agent);
    m_agent.fetch();
    return m_agent.getHeartbeatCount();
  }
};
    
}}