6 #ifndef __DDS__MonitoringThread__ 7 #define __DDS__MonitoringThread__ 24 typedef std::function<bool()> callbackFunction_t;
26 typedef std::pair<callbackFunction_t, std::chrono::seconds> callbackValue_t;
48 void start(
double _idleTime,
const std::function<
void(
void)>& _idleCallback)
51 static const std::chrono::seconds INTERVAL_STEP(1);
52 static const std::chrono::seconds WAITING_TIME(20);
57 [
this, &_idleCallback, _idleTime]()
61 std::chrono::seconds secInterval(0);
68 std::lock_guard<std::mutex> lock(m_registeredCallbackFunctionsMutex);
71 m_registeredCallbackFunctions.erase(
72 remove_if(m_registeredCallbackFunctions.begin(),
73 m_registeredCallbackFunctions.end(),
74 [&](callbackValue_t& i)
80 const int nCurInterval =
81 std::chrono::duration<int>(secInterval).count();
82 const int nInterval = std::chrono::duration<int>(i.second).count();
83 if (nCurInterval != 0 && nCurInterval >= nInterval &&
84 0 == (nCurInterval % nInterval))
87 <<
"MONITORING: calling callback at interval of " 88 << std::chrono::duration<int>(i.second).count();
93 m_registeredCallbackFunctions.end());
95 catch (std::exception& _e)
104 std::chrono::seconds idleTime;
107 std::lock_guard<std::mutex> lock(m_mutex);
108 std::chrono::steady_clock::time_point currentTime = std::chrono::steady_clock::now();
110 std::chrono::duration_cast<std::chrono::seconds>(currentTime - m_startIdleTime);
113 if (idleTime.count() > _idleTime)
117 <<
" sec. Call idle callback and wait " 118 << std::chrono::duration<int>(WAITING_TIME).count() <<
"s";
125 catch (std::exception& _e)
128 <<
"MonitoringThread exception on custom idle function: " << _e.what();
134 std::this_thread::sleep_for(WAITING_TIME);
139 std::this_thread::sleep_for(WAITING_TIME);
146 std::this_thread::sleep_for(INTERVAL_STEP);
147 secInterval += INTERVAL_STEP;
150 catch (std::exception& _e)
160 std::lock_guard<std::mutex> lock(m_mutex);
161 m_startIdleTime = std::chrono::steady_clock::now();
166 std::lock_guard<std::mutex> lock(m_registeredCallbackFunctionsMutex);
167 m_registeredCallbackFunctions.push_back(make_pair(_handler, _interval));
173 pid_t pidToKill(::getpid());
179 kill(pidToKill, SIGTERM);
183 const size_t max_iter = 30;
184 while (iter <= max_iter)
199 kill(pidToKill, SIGKILL);
205 std::chrono::steady_clock::time_point m_startIdleTime;
207 std::function<void(
void)> m_idleCallback;
208 std::vector<callbackValue_t> m_registeredCallbackFunctions;
210 std::mutex m_registeredCallbackFunctionsMutex;
void start(double _idleTime, const std::function< void(void)> &_idleCallback)
Main function user has to run to start monitoring thread.
Definition: MonitoringThread.h:48
static CMonitoringThread & instance()
Return singleton instance.
Definition: MonitoringThread.h:38
Definition: MonitoringThread.h:22
bool IsProcessRunning(pid_t _PID)
The function checks, whether the process which corresponds to the given _PID can be found.
Definition: Process.h:51
#define LOG(severity)
Definition: Logger.h:34
Miscellaneous functions and helpers are located here.
Definition: AgentConnectionManager.h:13
void registerCallbackFunction(callbackFunction_t _handler, const std::chrono::seconds &_interval)
Definition: MonitoringThread.h:164
void updateIdle()
Definition: MonitoringThread.h:158