annotator/example/include/process/watchdog.hpp
Alexander Kobjolke 04b878f078 Initial version
2022-03-01 22:13:15 +01:00

89 lines
2.5 KiB
C++

#ifndef PROCESS_PROCESS_EXT_INCLUDE_WATCHDOG_HPP
#define PROCESS_PROCESS_EXT_INCLUDE_WATCHDOG_HPP
#include <chrono>
#include <atomic>
#include <thread>
#include <map>
#include <process/process.hpp> // note: Public include from libprocess
#include "process_status.hpp" // note: Though in different folder this is also a public
// include from libprocess. Generated from IDL
#include "process_statistic.hpp"
#include "process/process_collection.hpp"
namespace process::controller {
class WatchdogCheckerBase {
public:
virtual void check() = 0;
};
template <typename T>
class WatchdogChecker : public WatchdogCheckerBase {
// Add assert to satisfy A14-1-1
static_assert(std::is_class<T>::value);
public:
WatchdogChecker(std::shared_ptr<T> cb)
: m_callback(std::move(cb))
{
if (m_callback.get() == nullptr) {
throw std::runtime_error("invalid callback");
}
}
protected:
std::shared_ptr<T> m_callback;
};
/**
@brief Periodically updates all provided processes.
@note
- Callback is used to keep class Watchdog free of specific dependencies and user types (No linkage to ASAP)
- Watchdog may be moved into libprocess as well as ProcessCollection
- API of Watchdog itself is not thread safe
*/
class Watchdog {
public:
/**
@brief Constructs the Watchdog.
@param collection List of all processes which shall be monitored
@param interval_ms All processes are checked periodically using the given interval
@param cb Callback object which will be invoked whenever a status change has been detected
*/
explicit Watchdog(const std::chrono::milliseconds interval_ms);
Watchdog(const Watchdog& other) = delete;
Watchdog(Watchdog&& other) = delete;
Watchdog& operator=(const Watchdog& other) & = delete;
Watchdog& operator=(Watchdog&& other) & = delete;
/** This class is not intended to be derived */
virtual ~Watchdog();
/**/
virtual void add_checker(std::shared_ptr<WatchdogCheckerBase> checker);
/** Watchdog starts processing */
virtual void start();
/** Stops the watchdog worker thread */
virtual void stop();
/** @return true if watchdog has been started and monitors the collection periodically */
virtual bool is_running() const;
protected:
virtual void loop();
std::chrono::milliseconds m_interval_ms;
std::atomic<bool> m_stop_loop;
std::thread m_thread;
std::vector<std::shared_ptr<WatchdogCheckerBase>> m_checker;
};
}
#endif