1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | |
16 | * \author Dianzhang.Chen |
17 | * \date Dec 2020 |
18 | * \brief Main function |
19 | */ |
20 | |
21 | #include <signal.h> |
22 | #include <string.h> |
23 | #include <iostream> |
24 | #include <ailego/debug/bug_report.h> |
25 | #include <ailego/utility/process_helper.h> |
26 | #include <gflags/gflags.h> |
27 | #include "repository/repository_common/config.h" |
28 | #include "repository/repository_common/version.h" |
29 | #include "mysql_repository.h" |
30 | |
31 | DEFINE_string(config, "" , "Read configuration from this file" ); |
32 | DEFINE_string(pidfile, "" , "Write the pid into this file" ); |
33 | DEFINE_bool(daemon, false, "Run this app in daemon mode" ); |
34 | |
35 | static bool ValidateConfig(const char *flagname, const std::string &config) { |
36 | return !config.empty(); |
37 | } |
38 | |
39 | DEFINE_validator(config, ValidateConfig); |
40 | |
41 | static inline std::string GetVersion() { |
42 | return std::string("Version: 0.0.1" ); |
43 | } |
44 | |
45 | static inline std::string PrintUsage() { |
46 | std::string usage = |
47 | "Usage: \n" |
48 | " mysql_repository [options] \n\n" |
49 | "Options: \n" |
50 | " --config Read configuration from this file.\n" |
51 | " --daemon Run this app in daemon mode.\n" |
52 | " --pidfile Write the pid into this file.\n" ; |
53 | return usage; |
54 | } |
55 | |
56 | void ShutdownHandler(int sig) { |
57 | LOG_INFO("Receive stop signal: %d" , sig); |
58 | auto &module = proxima::be::repository::MysqlRepository::Instance(); |
59 | module.stop(); |
60 | module.cleanup(); |
61 | } |
62 | |
63 | static inline void SetupSignals() { |
64 | ailego::ProcessHelper::IgnoreSignal(SIGHUP); |
65 | ailego::ProcessHelper::IgnoreSignal(SIGPIPE); |
66 | ailego::ProcessHelper::RegisterSignal(SIGINT, ShutdownHandler); |
67 | ailego::ProcessHelper::RegisterSignal(SIGTERM, ShutdownHandler); |
68 | ailego::ProcessHelper::RegisterSignal(SIGUSR1, ShutdownHandler); |
69 | ailego::ProcessHelper::RegisterSignal(SIGUSR2, ShutdownHandler); |
70 | } |
71 | |
72 | int main(int argc, char *argv[]) { |
73 | // Parse arguments |
74 | for (int i = 1; i < argc; ++i) { |
75 | const char *arg = argv[i]; |
76 | |
77 | if (!strcmp(arg, "-help" ) || !strcmp(arg, "--help" ) || !strcmp(arg, "-h" )) { |
78 | std::cout << PrintUsage() << std::endl; |
79 | exit(0); |
80 | } else if (!strcmp(arg, "-version" ) || !strcmp(arg, "--version" ) || |
81 | !strcmp(arg, "-v" )) { |
82 | std::cout << proxima::be::repository::Version::Details() << std::endl; |
83 | exit(0); |
84 | } |
85 | } |
86 | |
87 | gflags::SetVersionString(GetVersion()); |
88 | gflags::ParseCommandLineNonHelpFlags(&argc, &argv, false); |
89 | |
90 | // Load config |
91 | proxima::be::repository::Config &config = |
92 | proxima::be::repository::Config::Instance(); |
93 | int ret = config.load_repository_config(FLAGS_config); |
94 | if (ret != 0) { |
95 | std::cerr << "Mysql repository load config failed." << std::endl; |
96 | exit(1); |
97 | } |
98 | |
99 | if (!config.validate_repository_config()) { |
100 | std::cerr << "Mysql repository valid config error." << std::endl; |
101 | exit(1); |
102 | } |
103 | |
104 | // Initialize bug report |
105 | ailego::BugReport::Bootstrap(argc, argv, config.get_log_dir().c_str()); |
106 | |
107 | // Start module |
108 | auto &module = proxima::be::repository::MysqlRepository::Instance(); |
109 | |
110 | ret = module.init(FLAGS_daemon, FLAGS_pidfile); |
111 | if (ret != 0) { |
112 | std::cerr << "Mysql repository init failed." << std::endl; |
113 | exit(1); |
114 | } |
115 | |
116 | ret = module.start(); |
117 | if (ret != 0) { |
118 | std::cerr << "Mysql repository start failed." << std::endl; |
119 | module.stop(); |
120 | module.cleanup(); |
121 | exit(1); |
122 | } else { |
123 | std::cout << "Mysql repository start successfully." << std::endl; |
124 | } |
125 | |
126 | // Handle signals |
127 | SetupSignals(); |
128 | |
129 | // Wait for signals |
130 | pause(); |
131 | |
132 | // Stop and cleanup module |
133 | module.stop(); |
134 | module.cleanup(); |
135 | return 0; |
136 | } |
137 | |