20#ifndef ORES_EVENTING_SERVICE_POSTGRES_LISTENER_SERVICE_HPP
21#define ORES_EVENTING_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"
34#include "ores.eventing/domain/entity_change_event.hpp"
51 [[nodiscard]]
static auto& lg() {
53 static auto instance = make_logger(
54 "ores.eventing.service.postgres_listener_service");
116 void subscribe(
const std::string& channel_name);
124 void notify(
const std::string& channel_name,
const std::string& payload);
135 bool wait_until_ready(std::chrono::milliseconds timeout = std::chrono::seconds(5));
143 bool open_connection();
151 void issue_pending_listens();
167 void handle_notification(
const sqlgen::postgres::Notification& notification);
173 mutable std::mutex mutex_;
174 std::optional<rfl::Ref<sqlgen::postgres::Connection>> connection_;
175 std::vector<std::string> subscribed_channels_;
177 std::thread listener_thread_;
178 std::atomic<bool> running_;
180 std::condition_variable ready_cv_;
Event bus and related services.
Definition event_bus.hpp:33
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Context for the operations on a postgres database.
Definition context.hpp:30
Represents a low-level notification about a change to an entity at the repository level.
Definition entity_change_event.hpp:33
Manages a dedicated PostgreSQL connection to listen for NOTIFY events.
Definition postgres_listener_service.hpp:49
std::function< void(const domain::entity_change_event &)> notification_callback_t
Type alias for the notification callback function.
Definition postgres_listener_service.hpp:66
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:123
void start()
Starts the listener thread and begins listening for notifications.
Definition postgres_listener_service.cpp:88
void stop()
Stops the listener thread and waits for it to join.
Definition postgres_listener_service.cpp:104
void notify(const std::string &channel_name, const std::string &payload)
Sends a NOTIFY on a PostgreSQL channel.
Definition postgres_listener_service.cpp:158
~postgres_listener_service()
Destroys the postgres_listener_service.
Definition postgres_listener_service.cpp:42
void subscribe(const std::string &channel_name)
Subscribes to a PostgreSQL NOTIFY channel.
Definition postgres_listener_service.cpp:128