-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
233 lines (184 loc) · 5.63 KB
/
server.cpp
File metadata and controls
233 lines (184 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//=======================================================================
// Copyright (c) 2013-2020 Baptiste Wicht.
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
// http://opensource.org/licenses/MIT)
//=======================================================================
#include <condition_variable>
#include <set>
#include <thread>
#include "accounts.hpp"
#include "api/server_api.hpp"
#include "assets.hpp"
#include "config.hpp"
#include "currency.hpp"
#include "data.hpp"
#include "debts.hpp"
#include "earnings.hpp"
#include "expenses.hpp"
#include "fortune.hpp"
#include "http.hpp"
#include "incomes.hpp"
#include "liabilities.hpp"
#include "logging.hpp"
#include "objectives.hpp"
#include "pages/server_pages.hpp"
#include "recurring.hpp"
#include "share.hpp"
#include "wishes.hpp"
using namespace budget;
namespace {
httplib::Server* server_ptr = nullptr;
std::atomic<bool> cron = true;
std::mutex lock;
std::condition_variable cv;
void server_signal_handler(int signum) {
LOG_F(INFO, "Received signal ({})", signum);
cron = false;
if (server_ptr) {
server_ptr->stop();
}
cv.notify_all();
}
void install_signal_handler() {
struct sigaction action {};
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
action.sa_handler = server_signal_handler;
sigaction(SIGTERM, &action, nullptr);
sigaction(SIGINT, &action, nullptr);
LOG_F(INFO, "Installed the signal handler");
}
bool start_server() {
// Name the thread
const pthread_t self = pthread_self();
pthread_setname_np(self, "server thread");
loguru::set_thread_name("server thread");
LOG_F(INFO, "Started the server thread");
httplib::Server server;
load_pages(server);
load_api(server);
install_signal_handler();
auto port = get_server_port();
auto listen = get_server_listen();
server_ptr = &server;
// Listen
LOG_F(INFO, "Server is starting to listen on {}:{}", listen, port);
if (!server.listen(listen.c_str(), port)) {
LOG_F(ERROR, "Server failed to start");
return false;
}
LOG_F(INFO, "Server has exited normally");
return true;
}
void start_cron_loop() {
// Name the thread
const pthread_t self = pthread_self();
pthread_setname_np(self, "cron thread");
loguru::set_thread_name("cron thread");
LOG_F(INFO, "cron: Started the cron thread");
size_t hours = 0;
while (cron) {
using namespace std::chrono_literals;
{
std::unique_lock lk(lock);
cv.wait_for(lk, 1h);
if (!cron) {
break;
}
}
++hours;
LOG_F(INFO, "cron: Check for recurrings");
check_for_recurrings();
// We save the cache once per day
if (hours % 24 == 0) {
LOG_F(INFO, "cron: Save the caches");
save_currency_cache();
save_share_price_cache();
}
// Every four hours, we refresh the currency cache
// Only current day rates are refreshed
if (hours % 4 == 0) {
LOG_F(INFO, "cron: Refresh the currency cache");
budget::refresh_currency_cache();
}
// Every hour, we try to prefetch value for new days
LOG_F(INFO, "cron: Prefetch the share cache");
budget::prefetch_share_price_cache();
}
LOG_F(INFO, "cron: Cron thread has exited");
}
void load() {
load_accounts();
load_incomes();
load_expenses();
load_earnings();
load_assets();
load_liabilities();
load_objectives();
load_wishes();
load_fortunes();
load_recurrings();
load_debts();
load_wishes();
}
} // end of anonymous namespace
int main(int argc, char** argv) {
std::locale const global_locale("");
std::locale::global(global_locale);
// Initialize logging for the server
loguru::g_stderr_verbosity = loguru::Verbosity_INFO;
loguru::init(argc, argv);
if (!load_config()) {
LOG_F(ERROR, "Could not config");
return 1;
}
set_server_running();
// Restore the caches
load_currency_cache();
load_share_price_cache();
auto old_data_version = to_number<size_t>(internal_config_value("data_version"));
LOG_F(INFO, "Detected database version {}", old_data_version);
try {
if (!budget::migrate_database(old_data_version)) {
return 1;
}
} catch (const budget_exception& e) {
LOG_F(ERROR, "budget_exception occured in migrate: {}", e.message());
return 1;
} catch (const date_exception& e) {
LOG_F(ERROR, "date_exception occured in migrate: {}", e.message());
return 1;
} catch (const std::exception& e) {
LOG_F(ERROR, "std::exception occured in migrate: {}", e.what());
return 1;
}
// Load all the data
try {
load();
} catch (const budget_exception& e) {
LOG_F(ERROR, "budget_exception occured in load: {}", e.message());
return 1;
} catch (const date_exception& e) {
LOG_F(ERROR, "date_exception occured in load: {}", e.message());
return 1;
} catch (const std::exception& e) {
LOG_F(ERROR, "std::exception occured in load: {}", e.what());
return 1;
}
std::atomic<bool> success = false;
{
std::jthread cron_thread([]() { start_cron_loop(); });
{
std::jthread server_thread([&success]() { success = start_server(); });
}
if (!success) {
cron = false;
}
}
// Save the caches
save_currency_cache();
save_share_price_cache();
save_config();
return 0;
}