20#ifndef ORES_EVENTING_SERVICE_POSTGRES_LISTENER_SERVICE_HPP
21#define ORES_EVENTING_SERVICE_POSTGRES_LISTENER_SERVICE_HPP
30#include <sqlgen/postgres.hpp>
31#include "ores.utility/log/make_logger.hpp"
32#include "ores.database/domain/context.hpp"
33#include "ores.eventing/domain/entity_change_event.hpp"
35namespace ores::eventing::service {
50 [[nodiscard]]
static auto& lg() {
52 static auto instance = make_logger(
53 "ores.eventing.service.postgres_listener_service");
115 void subscribe(
const std::string& channel_name);
123 void notify(
const std::string& channel_name,
const std::string& payload);
131 bool open_connection();
139 void issue_pending_listens();
155 void handle_notification(
const sqlgen::postgres::Notification& notification);
161 mutable std::mutex mutex_;
162 std::optional<rfl::Ref<sqlgen::postgres::Connection>> connection_;
163 std::vector<std::string> subscribed_channels_;
165 std::thread listener_thread_;
166 std::atomic<bool> running_;
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
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:32
Manages a dedicated PostgreSQL connection to listen for NOTIFY events.
Definition postgres_listener_service.hpp:48
std::function< void(const domain::entity_change_event &)> notification_callback_t
Type alias for the notification callback function.
Definition postgres_listener_service.hpp:65
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:147
~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:117