-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvor.cpp
235 lines (202 loc) · 5.42 KB
/
vor.cpp
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
234
235
#include <boost/shared_ptr.hpp>
#include <cstring>
#include <curl/curl.h>
#include <iostream>
#include <signal.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include "data_structures.h"
#include "load.h"
#include "program_options.h"
#include "server.h"
#include "stats.h"
// Reload the new users and users online data.
void reload_data_fast(int);
// Reload all the data.
void reload_data_slow(int);
// Deal with dead child
void dead_child(int);
// Child is dying, dump the stats to shared memory so they can
// be loaded by the replacement.
void dump_stats(int);
// Parent is shutting down, so clear the stats shared memory.
void shutdown(int);
// Handle timed refreshes of data.
void parent();
// Handle data loading and searching.
void child();
char *program_name;
pid_t parent_pid;
int main(int argc, char *argv[]) {
program_name = argv[0];
parent_pid = getpid();
boost::shared_ptr<ProgramOptions> new_program_options(
new ProgramOptions(argc, argv));
program_options.swap(new_program_options);
boost::shared_ptr<Stats> new_stats(
new Stats(data, parent_pid));
global_stats.swap(new_stats);
int fork_pid = fork();
if (fork_pid < 0) {
if (program_options->verbose() >= 0) {
std::cout << "Fork error" << std::endl;
}
exit(1);
} else if (fork_pid > 0) {
child_pids.push_back(fork_pid);
parent();
} else {
strncpy(program_name, "vor-child", strlen(program_name));
child();
}
return 0;
}
void parent() {
// Set up a timer to handle data reloading
sigset_t new_set;
sigemptyset(&new_set);
sigaddset(&new_set, SIGHUP);
sigaddset(&new_set, SIGTERM);
sigprocmask(SIG_UNBLOCK, &new_set, NULL);
signal(SIGHUP, reload_data_slow);
signal(SIGCHLD, dead_child);
signal(SIGTERM, shutdown);
signal(SIGINT, shutdown);
while (true) {
sleep(60); // forever, or at least until we get SIGTERM
}
}
void child() {
signal(SIGHUP, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
signal(SIGTERM, dump_stats);
signal(SIGINT, dump_stats);
curl_global_init(CURL_GLOBAL_ALL);
if (program_options->verbose() >= 1) {
std::cout << "Loading data..." << std::endl;
}
if (Load::load_all_data(data)) {
time_t ltime;
ltime = time(<ime);
localtime_r(<ime, &last_data_loaded);
last_data_loaded.tm_yday--;
if (program_options->verbose() >= 1) {
std::cout << "Data loaded" << std::endl;
}
}
// Set up a timer to handle data reloading
sigset_t new_set;
sigemptyset(&new_set);
sigaddset(&new_set, SIGALRM);
sigprocmask(SIG_UNBLOCK, &new_set, NULL);
struct itimerval tout_val;
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = program_options->reload_frequency();
tout_val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tout_val, 0);
signal(SIGALRM, reload_data_fast);
// Start up our own server
try {
Server server(data);
if (program_options->verbose() >= 0) {
std::cout << "Server ready for connections" << std::endl;
}
server.threaded_accept();
server.wait_on_threads();
} catch (char const *e) {
if (program_options->verbose() >= 0) {
std::cout << "SERVER FAILED: ";
std::cout << e << std::endl;
}
}
curl_global_cleanup();
}
void reload_data_fast(int) {
// Mask the SIGALRM signal
sigset_t new_set;
sigemptyset(&new_set);
sigaddset(&new_set, SIGALRM);
sigprocmask(SIG_BLOCK, &new_set, NULL);
// Should we do a daily run?
struct tm newtime;
time_t ltime;
ltime = time(<ime);
localtime_r(<ime, &newtime);
if (((newtime.tm_yday > last_data_loaded.tm_yday) ||
(newtime.tm_yday == 0)) &&
(newtime.tm_hour == program_options->reload_hour()) ) {
if (program_options->verbose() >= 1) {
std::cout << "Reloading all data" << std::endl;
}
// Send a HUP to the parent to force it to spawn a new
// child. The child will load data, kill us, and take over.
kill(parent_pid, SIGHUP);
} else {
if (program_options->verbose() >= 1) {
std::cout << "Reloading fast data" << std::endl;
}
Load::reload_online_and_new(data);
if (program_options->verbose() >= 1) {
std::cout << "Data loaded" << std::endl;
}
}
// Set up a timer again
sigprocmask(SIG_UNBLOCK, &new_set, NULL);
struct itimerval tout_val;
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = program_options->reload_frequency();
tout_val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tout_val, 0);
}
void reload_data_slow(int) {
int fork_pid = fork();
if (fork_pid < 0) {
if (program_options->verbose() >= 0) {
std::cout << "Fork error" << std::endl;
}
exit(1);
} else if (fork_pid == 0) {
strncpy(program_name, "vor-child", strlen(program_name));
child();
} else {
child_pids.push_back(fork_pid);
}
}
void dead_child(int) {
pid_t pid = wait(NULL);
child_pids.erase(std::remove(child_pids.begin(), child_pids.end(), pid));
if (child_pids.empty()) {
int fork_pid = fork();
if (fork_pid < 0) {
if (program_options->verbose() >= 0) {
std::cout << "Fork error" << std::endl;
}
exit(1);
} else if (fork_pid == 0) {
strncpy(program_name, "vor-child", strlen(program_name));
child();
} else {
child_pids.push_back(fork_pid);
}
}
}
void dump_stats(int) {
global_stats->save_stats();
exit(0);
}
void shutdown(int) {
// Stop trying to respawn children
signal(SIGCHLD, SIG_IGN);
// Kill all children
for (std::vector<pid_t>::const_iterator it = child_pids.begin();
it != child_pids.end();
++it) {
kill(*it, SIGTERM);
}
sleep(1);
global_stats->clear_stats();
exit(0);
}