MantisBase v0.4.9
Loading...
Searching...
No Matches
realtime.h
Go to the documentation of this file.
1
13
14#ifndef MANTISBASE_REALTIME_H
15#define MANTISBASE_REALTIME_H
16
17#include <condition_variable>
18#include <functional>
19#include <memory>
20#include <mutex>
21#include <string>
22#include <thread>
23
24#include "mantisbase/mantis.h"
25#include "nlohmann/json.hpp"
26
27#if MB_HAS_POSTGRESQL
28#include <soci/postgresql/soci-postgresql.h>
29#include <libpq-fe.h>
30#endif
31
32namespace soci {
33 class session;
34}
35
36namespace mb {
37 // Forward declarations
38 class MantisBase;
39 class MbEntity;
40 class MbRtDbWorker;
41
42 using json = nlohmann::json;
43
45 using RtCallback = std::function<void(const json &)>;
46
53 public:
55 explicit MbRealtimeDB(const MantisBase &app);
56
62
64 [[nodiscard]] bool init() const;
65
67 void addDbHooks(const std::string &entity_name) const;
68
70 void addDbHooks(const MbEntity &entity) const;
71
73 static void addDbHooks(const MbEntity &entity, const std::shared_ptr<soci::session> &sess);
74
76 void dropDbHooks(const std::string &entity_name) const;
77
79 static void dropDbHooks(const std::string &entity_name, const std::shared_ptr<soci::session> &sess);
80
82 void runWorker(const RtCallback &callback);
83
85 void stopWorker() const;
86
92 void notifyChange() const;
93
94 private:
95#if MB_HAS_POSTGRESQL
96 // Create the notification trigger function
97 static void createNotifyFunction(const MantisBase& app, soci::session &sql);
98#endif
99
100 static std::string buildTriggerObject(const MbEntity &entity, const std::string &action /*"NEW" or "OLD"*/);
101
102 std::unique_ptr<MbRtDbWorker> m_rtDbWorker;
103 };
104
107 public:
109 explicit MbRtDbWorker(const MantisBase &app);
110
112
113 [[nodiscard]] bool isDbRunning() const;
114
115 void addCallback(const RtCallback &cb);
116
117 void stopWorker();
118
120 void notify();
121
122 private:
123 void run();
124
125 void runSQlite();
126
127#if MB_HAS_POSTGRESQL
128 void runPostgreSQL();
129#endif
130
131 bool initSQLite();
132
133#if MB_HAS_POSTGRESQL
134 bool initPSQL();
135#endif
136
137 void pruneChangeLog(int up_to_id); // Delete consumed rows from mb_change_log (SQLite)
138
139 const MantisBase &mApp; // Owning application (injected)
140 int last_id = -1; // Last db ID to be queried, only query newer than this
141 int m_lastPrunedId = 0; // Highest mb_change_log id already pruned
142 std::string last_ts = getCurrentTimestampUTC(); // When last is not set, use timestamp value in UTC
143
144 std::string m_db_type;
145 RtCallback m_callback;
146 std::atomic<bool> m_running;
147 bool m_notified = false; // set by notify(); guarded by mtx
148 std::thread th;
149 std::mutex mtx;
150 std::condition_variable cv;
151 std::unique_ptr<soci::session> sql_ro;
152
153#if MB_HAS_POSTGRESQL
154 std::unique_ptr<PGconn, decltype(&PQfinish)> psql{nullptr, &PQfinish};
155#endif
156 };
157}
158
159#endif //MANTISBASE_REALTIME_H
MantisBase entry point.
Definition mantisbase.h:54
MbAppMixin(const MantisBase &app)
Constrictor for instantiating this interface.
Definition types.cpp:5
Represents a database table/entity with schema and CRUD operations.
Definition entity.h:55
bool init() const
Definition realtime.cpp:14
void addDbHooks(const std::string &entity_name) const
Definition realtime.cpp:60
void dropDbHooks(const std::string &entity_name) const
Definition realtime.cpp:149
MbRealtimeDB(const MantisBase &app)
Definition realtime.cpp:9
void runWorker(const RtCallback &callback)
Definition realtime.cpp:184
void stopWorker() const
Definition realtime.cpp:191
void notifyChange() const
Definition realtime.cpp:197
Definition realtime.h:106
void addCallback(const RtCallback &cb)
Definition realtime.cpp:323
bool isDbRunning() const
Definition realtime.cpp:314
void notify()
Definition realtime.cpp:455
MbRtDbWorker(const MantisBase &app)
Definition realtime.cpp:282
void stopWorker()
Definition realtime.cpp:327
~MbRtDbWorker()
Definition realtime.cpp:310
#define MANTISBASE_API
Definition export.h:31
Convenience header that includes all MantisBase public API headers.
Definition api_keys.h:35
nlohmann::json json
Shorten JSON namespace.
Definition api_keys.h:36
MANTISBASE_API std::string getCurrentTimestampUTC()
Definition utils.h:347
std::function< void(const json &)> RtCallback
Definition realtime.h:45
Definition log_database.h:21