ORE Studio 0.0.4
Loading...
Searching...
No Matches
postgres_listener_service.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_EVENTING_SERVICE_POSTGRES_LISTENER_SERVICE_HPP
21#define ORES_EVENTING_SERVICE_POSTGRES_LISTENER_SERVICE_HPP
22
23#include <mutex>
24#include <atomic>
25#include <string>
26#include <thread>
27#include <vector>
28#include <optional>
29#include <functional>
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"
34
35namespace ores::eventing::service {
36
49private:
50 [[nodiscard]] static auto& lg() {
51 using namespace ores::utility::log;
52 static auto instance = make_logger(
53 "ores.eventing.service.postgres_listener_service");
54 return instance;
55 }
56
57public:
65 using notification_callback_t = std::function<void(const domain::entity_change_event&)>;
66
79
86
87 // Deleted copy constructor and assignment operator to prevent copies.
89 postgres_listener_service& operator=(const postgres_listener_service&) = delete;
90
97 void start();
98
102 void stop();
103
115 void subscribe(const std::string& channel_name);
116
123 void notify(const std::string& channel_name, const std::string& payload);
124
125private:
131 bool open_connection();
132
139 void issue_pending_listens();
140
146 void listen_loop();
147
155 void handle_notification(const sqlgen::postgres::Notification& notification);
156
157private:
159 notification_callback_t notification_callback_;
160
161 mutable std::mutex mutex_;
162 std::optional<rfl::Ref<sqlgen::postgres::Connection>> connection_;
163 std::vector<std::string> subscribed_channels_;
164
165 std::thread listener_thread_;
166 std::atomic<bool> running_;
167};
168
169}
170
171#endif
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