Line data Source code
1 : #include <signal.h>
2 : #include <thrift/protocol/TBinaryProtocol.h>
3 : #include <thrift/server/TThreadedServer.h>
4 : #include <thrift/transport/TBufferTransports.h>
5 : #include <thrift/transport/TServerSocket.h>
6 :
7 : #include "../utils.h"
8 : #include "../utils_memcached.h"
9 : #include "../utils_mongodb.h"
10 : #include "../utils_thrift.h"
11 : #include "UserMentionHandler.h"
12 : #include "nlohmann/json.hpp"
13 :
14 : extern "C" void __gcov_flush();
15 :
16 : using apache::thrift::protocol::TBinaryProtocolFactory;
17 : using apache::thrift::server::TThreadedServer;
18 : using apache::thrift::transport::TFramedTransportFactory;
19 : using apache::thrift::transport::TServerSocket;
20 : using namespace social_network;
21 :
22 : static memcached_pool_st* memcached_client_pool;
23 : static mongoc_client_pool_t* mongodb_client_pool;
24 :
25 0 : void sigintHandler(int sig) {
26 0 : if (memcached_client_pool != nullptr) {
27 0 : memcached_pool_destroy(memcached_client_pool);
28 : }
29 0 : if (mongodb_client_pool != nullptr) {
30 0 : mongoc_client_pool_destroy(mongodb_client_pool);
31 : }
32 0 : exit(EXIT_SUCCESS);
33 : }
34 :
35 1 : void handle_sigusr1(int signum) {
36 1 : __gcov_flush();
37 0 : }
38 :
39 1 : int main(int argc, char* argv[]) {
40 1 : signal(SIGINT, sigintHandler);
41 1 : signal(SIGUSR1, handle_sigusr1);
42 1 : init_logger();
43 1 : SetUpTracer("config/jaeger-config.yml", "user-mention-service");
44 :
45 1 : json config_json;
46 1 : if (load_config_file("config/service-config.json", &config_json) != 0) {
47 0 : exit(EXIT_FAILURE);
48 : }
49 :
50 1 : int port = config_json["user-mention-service"]["port"];
51 :
52 1 : int mongodb_conns = config_json["user-mongodb"]["connections"];
53 1 : int mongodb_timeout = config_json["user-mongodb"]["timeout_ms"];
54 :
55 1 : int memcached_conns = config_json["user-memcached"]["connections"];
56 1 : int memcached_timeout = config_json["user-memcached"]["timeout_ms"];
57 :
58 : memcached_client_pool =
59 1 : init_memcached_client_pool(config_json, "user", 32, memcached_conns);
60 : mongodb_client_pool =
61 1 : init_mongodb_client_pool(config_json, "user", mongodb_conns);
62 1 : if (memcached_client_pool == nullptr || mongodb_client_pool == nullptr) {
63 0 : return EXIT_FAILURE;
64 : }
65 :
66 1 : std::shared_ptr<TServerSocket> server_socket = get_server_socket(config_json, "0.0.0.0", port);
67 :
68 2 : TThreadedServer server(std::make_shared<UserMentionServiceProcessor>(
69 2 : std::make_shared<UserMentionHandler>(
70 : memcached_client_pool, mongodb_client_pool)),
71 : server_socket,
72 2 : std::make_shared<TFramedTransportFactory>(),
73 6 : std::make_shared<TBinaryProtocolFactory>());
74 :
75 2 : LOG(info) << "Starting the user-mention-service server...";
76 1 : server.serve();
77 3 : }
|