#ifndef PROCESS_PROCESS_EXT_INCLUDE_WATCHDOG_HPP #define PROCESS_PROCESS_EXT_INCLUDE_WATCHDOG_HPP #include #include #include #include #include // 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 class WatchdogChecker : public WatchdogCheckerBase { // Add assert to satisfy A14-1-1 static_assert(std::is_class::value); public: WatchdogChecker(std::shared_ptr cb) : m_callback(std::move(cb)) { if (m_callback.get() == nullptr) { throw std::runtime_error("invalid callback"); } } protected: std::shared_ptr 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 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 m_stop_loop; std::thread m_thread; std::vector> m_checker; }; } #endif