20#ifndef ORES_DATABASE_SERVICE_POSTGRES_LISTENER_SERVICE_HPP
21#define ORES_DATABASE_SERVICE_POSTGRES_LISTENER_SERVICE_HPP
30#include <condition_variable>
31#include <sqlgen/postgres.hpp>
32#include "ores.logging/make_logger.hpp"
33#include "ores.database/domain/context.hpp"
49 [[nodiscard]]
static auto& lg() {
51 static auto instance = make_logger(
52 "ores.database.service.postgres_listener_service");
67 std::function<void(
const std::string& channel,
const std::string& payload)>;
117 void subscribe(
const std::string& channel_name);
125 void notify(
const std::string& channel_name,
const std::string& payload);
136 bool wait_until_ready(std::chrono::milliseconds timeout = std::chrono::seconds(5));
144 bool open_connection();
152 void issue_pending_listens();
168 void handle_notification(
const sqlgen::postgres::Notification& notification);
174 mutable std::mutex mutex_;
175 std::optional<rfl::Ref<sqlgen::postgres::Connection>> connection_;
176 std::vector<std::string> subscribed_channels_;
178 std::thread listener_thread_;
179 std::atomic<bool> running_;
181 std::condition_variable ready_cv_;
Database service layer.
Definition ores.database.service.hpp:28
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Context for the operations on a postgres database.
Definition context.hpp:47
Manages a dedicated PostgreSQL connection to listen for NOTIFY events.
Definition postgres_listener_service.hpp:47
std::function< void(const std::string &channel, const std::string &payload)> notification_callback_t
Type alias for the notification callback function.
Definition postgres_listener_service.hpp:67
bool wait_until_ready(std::chrono::milliseconds timeout=std::chrono::seconds(5))
Waits until the listener is ready to receive notifications.
Definition postgres_listener_service.cpp:121
void start()
Starts the listener thread and begins listening for notifications.
Definition postgres_listener_service.cpp:86
void stop()
Stops the listener thread and waits for it to join.
Definition postgres_listener_service.cpp:102
void notify(const std::string &channel_name, const std::string &payload)
Sends a NOTIFY on a PostgreSQL channel.
Definition postgres_listener_service.cpp:156
~postgres_listener_service()
Destroys the postgres_listener_service.
Definition postgres_listener_service.cpp:40
void subscribe(const std::string &channel_name)
Subscribes to a PostgreSQL NOTIFY channel.
Definition postgres_listener_service.cpp:126