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_DATABASE_SERVICE_POSTGRES_LISTENER_SERVICE_HPP
21#define ORES_DATABASE_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 <condition_variable>
31#include <sqlgen/postgres.hpp>
32#include "ores.logging/make_logger.hpp"
33#include "ores.database/domain/context.hpp"
34
36
48private:
49 [[nodiscard]] static auto& lg() {
50 using namespace ores::logging;
51 static auto instance = make_logger(
52 "ores.database.service.postgres_listener_service");
53 return instance;
54 }
55
56public:
67 std::function<void(const std::string& channel, const std::string& payload)>;
68
81
88
89 // Deleted copy constructor and assignment operator to prevent copies.
91 postgres_listener_service& operator=(const postgres_listener_service&) = delete;
92
99 void start();
100
104 void stop();
105
117 void subscribe(const std::string& channel_name);
118
125 void notify(const std::string& channel_name, const std::string& payload);
126
136 bool wait_until_ready(std::chrono::milliseconds timeout = std::chrono::seconds(5));
137
138private:
144 bool open_connection();
145
152 void issue_pending_listens();
153
159 void listen_loop();
160
168 void handle_notification(const sqlgen::postgres::Notification& notification);
169
170private:
171 context ctx_;
172 notification_callback_t notification_callback_;
173
174 mutable std::mutex mutex_;
175 std::optional<rfl::Ref<sqlgen::postgres::Connection>> connection_;
176 std::vector<std::string> subscribed_channels_;
177
178 std::thread listener_thread_;
179 std::atomic<bool> running_;
180
181 std::condition_variable ready_cv_;
182 bool ready_{false};
183};
184
185}
186
187#endif
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