ORE Studio 0.0.4
Loading...
Searching...
No Matches
health_monitor.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_DATABASE_HEALTH_MONITOR_HPP
21#define ORES_DATABASE_HEALTH_MONITOR_HPP
22
23#include <mutex>
24#include <atomic>
25#include <chrono>
26#include <string>
27#include <functional>
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"
34
35namespace ores::database {
36
59class health_monitor final {
60private:
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>;
64
65 [[nodiscard]] static logger_type& lg() {
66 static logger_type instance(
67 boost::log::keywords::channel = "ores.database.health_monitor");
68 return instance;
69 }
70
71public:
79 std::function<void(bool available, const std::string& error_message)>;
80
87 explicit health_monitor(database_options options,
88 std::chrono::seconds poll_interval = std::chrono::seconds(5));
89
90 ~health_monitor() = default;
91
92 health_monitor(const health_monitor&) = delete;
93 health_monitor& operator=(const health_monitor&) = delete;
95 health_monitor& operator=(health_monitor&&) = delete;
96
103
109 [[nodiscard]] bool is_available() const;
110
116 [[nodiscard]] std::string last_error() const;
117
126 boost::asio::awaitable<void> run(boost::asio::io_context& io_context);
127
133 void stop();
134
142 bool check_health();
143
144private:
145 database_options options_;
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_;
151 status_change_callback status_callback_;
152 boost::asio::cancellation_signal stop_signal_;
153};
154
155}
156
157#endif
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