20#ifndef ORES_DATABASE_HEALTH_MONITOR_HPP
21#define ORES_DATABASE_HEALTH_MONITOR_HPP
28#include <boost/asio/awaitable.hpp>
29#include <boost/asio/io_context.hpp>
30#include <boost/asio/cancellation_signal.hpp>
31#include <boost/log/trivial.hpp>
32#include <boost/log/sources/severity_channel_logger.hpp>
33#include "ores.database/domain/database_options.hpp"
35namespace ores::database {
61 using severity_level = boost::log::trivial::severity_level;
62 using logger_type = boost::log::sources::severity_channel_logger_mt<
63 severity_level, std::string>;
65 [[nodiscard]]
static logger_type& lg() {
66 static logger_type instance(
67 boost::log::keywords::channel =
"ores.database.health_monitor");
79 std::function<void(
bool available,
const std::string& error_message)>;
88 std::chrono::seconds poll_interval = std::chrono::seconds(5));
126 boost::asio::awaitable<void>
run(boost::asio::io_context& io_context);
146 std::chrono::seconds poll_interval_;
147 std::atomic<bool> available_{
false};
148 std::atomic<bool> running_{
false};
149 mutable std::mutex mutex_;
150 std::string last_error_;
152 boost::asio::cancellation_signal stop_signal_;
Configuration for database connection.
Definition database_options.hpp:33
Monitors database connectivity and provides health status.
Definition health_monitor.hpp:59
boost::asio::awaitable< void > run(boost::asio::io_context &io_context)
Run the health monitor polling loop.
Definition health_monitor.cpp:153
void set_status_change_callback(status_change_callback callback)
Set the callback to be invoked when database status changes.
Definition health_monitor.cpp:44
std::string last_error() const
Get the last error message if database is unavailable.
Definition health_monitor.cpp:53
void stop()
Stop the health monitor.
Definition health_monitor.cpp:58
bool is_available() const
Check if the database is currently available.
Definition health_monitor.cpp:49
std::function< void(bool available, const std::string &error_message)> status_change_callback
Callback type for status change notifications.
Definition health_monitor.hpp:79
bool check_health()
Perform a single health check.
Definition health_monitor.cpp:64