1
0

hmain_test.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "hv.h"
  2. #include "hmain.h"
  3. #include "iniparser.h"
  4. /*
  5. * @build: make examples
  6. * @usage: bin/hmain_test -h
  7. * bin/hmain_test -v
  8. *
  9. * bin/hmain_test -c etc/hmain_test.conf -d
  10. * ps aux | grep hmain_test
  11. *
  12. * bin/hmain_test -s stop
  13. * ps aux | grep hmain_test
  14. *
  15. */
  16. typedef struct conf_ctx_s {
  17. IniParser* parser;
  18. int loglevel;
  19. int worker_processes;
  20. int worker_threads;
  21. int port;
  22. } conf_ctx_t;
  23. conf_ctx_t g_conf_ctx;
  24. inline void conf_ctx_init(conf_ctx_t* ctx) {
  25. ctx->parser = new IniParser;
  26. ctx->loglevel = LOG_LEVEL_DEBUG;
  27. ctx->worker_processes = 0;
  28. ctx->worker_threads = 0;
  29. ctx->port = 0;
  30. }
  31. static void print_version();
  32. static void print_help();
  33. static int parse_confile(const char* confile);
  34. static void worker_fn(void* userdata);
  35. // short options
  36. static const char options[] = "hvc:ts:dp:";
  37. // long options
  38. static const option_t long_options[] = {
  39. {'h', "help", NO_ARGUMENT},
  40. {'v', "version", NO_ARGUMENT},
  41. {'c', "confile", REQUIRED_ARGUMENT},
  42. {'t', "test", NO_ARGUMENT},
  43. {'s', "signal", REQUIRED_ARGUMENT},
  44. {'d', "daemon", NO_ARGUMENT},
  45. {'p', "port", REQUIRED_ARGUMENT}
  46. };
  47. static const char detail_options[] = R"(
  48. -h|--help Print this information
  49. -v|--version Print version
  50. -c|--confile <confile> Set configure file, default etc/{program}.conf
  51. -t|--test Test configure file and exit
  52. -s|--signal <signal> Send <signal> to process,
  53. <signal>=[start,stop,restart,status,reload]
  54. -d|--daemon Daemonize
  55. -p|--port <port> Set listen port
  56. )";
  57. void print_version() {
  58. printf("%s version %s\n", g_main_ctx.program_name, hv_compile_version());
  59. }
  60. void print_help() {
  61. printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
  62. printf("Options:\n%s\n", detail_options);
  63. }
  64. int parse_confile(const char* confile) {
  65. int ret = g_conf_ctx.parser->LoadFromFile(confile);
  66. if (ret != 0) {
  67. printf("Load confile [%s] failed: %d\n", confile, ret);
  68. exit(-40);
  69. }
  70. // logfile
  71. std::string str = g_conf_ctx.parser->GetValue("logfile");
  72. if (!str.empty()) {
  73. strncpy(g_main_ctx.logfile, str.c_str(), sizeof(g_main_ctx.logfile));
  74. }
  75. hlog_set_file(g_main_ctx.logfile);
  76. // loglevel
  77. str = g_conf_ctx.parser->GetValue("loglevel");
  78. if (!str.empty()) {
  79. hlog_set_level_by_str(str.c_str());
  80. }
  81. // log_filesize
  82. str = g_conf_ctx.parser->GetValue("log_filesize");
  83. if (!str.empty()) {
  84. hlog_set_max_filesize_by_str(str.c_str());
  85. }
  86. // log_remain_days
  87. str = g_conf_ctx.parser->GetValue("log_remain_days");
  88. if (!str.empty()) {
  89. hlog_set_remain_days(atoi(str.c_str()));
  90. }
  91. // log_fsync
  92. str = g_conf_ctx.parser->GetValue("log_fsync");
  93. if (!str.empty()) {
  94. logger_enable_fsync(hlog, getboolean(str.c_str()));
  95. }
  96. // first log here
  97. hlogi("%s version: %s", g_main_ctx.program_name, hv_compile_version());
  98. hlog_fsync();
  99. // worker_processes
  100. int worker_processes = 0;
  101. #ifdef DEBUG
  102. // Disable multi-processes mode for debugging
  103. worker_processes = 0;
  104. #else
  105. str = g_conf_ctx.parser->GetValue("worker_processes");
  106. if (str.size() != 0) {
  107. if (strcmp(str.c_str(), "auto") == 0) {
  108. worker_processes = get_ncpu();
  109. hlogd("worker_processes=ncpu=%d", worker_processes);
  110. }
  111. else {
  112. worker_processes = atoi(str.c_str());
  113. }
  114. }
  115. #endif
  116. g_conf_ctx.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  117. // worker_threads
  118. int worker_threads = 0;
  119. str = g_conf_ctx.parser->GetValue("worker_threads");
  120. if (str.size() != 0) {
  121. if (strcmp(str.c_str(), "auto") == 0) {
  122. worker_threads = get_ncpu();
  123. hlogd("worker_threads=ncpu=%d", worker_threads);
  124. }
  125. else {
  126. worker_threads = atoi(str.c_str());
  127. }
  128. }
  129. g_conf_ctx.worker_threads = LIMIT(0, worker_threads, 64);
  130. // port
  131. int port = 0;
  132. const char* szPort = get_arg("p");
  133. if (szPort) {
  134. port = atoi(szPort);
  135. }
  136. if (port == 0) {
  137. port = g_conf_ctx.parser->Get<int>("port");
  138. }
  139. if (port == 0) {
  140. printf("Please config listen port!\n");
  141. exit(-10);
  142. }
  143. g_conf_ctx.port = port;
  144. hlogi("parse_confile('%s') OK", confile);
  145. return 0;
  146. }
  147. static void on_reload(void* userdata) {
  148. hlogi("reload confile [%s]", g_main_ctx.confile);
  149. parse_confile(g_main_ctx.confile);
  150. }
  151. int main(int argc, char** argv) {
  152. // g_main_ctx
  153. main_ctx_init(argc, argv);
  154. if (argc == 1) {
  155. print_help();
  156. exit(10);
  157. }
  158. // int ret = parse_opt(argc, argv, options);
  159. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  160. if (ret != 0) {
  161. print_help();
  162. exit(ret);
  163. }
  164. /*
  165. printf("---------------arg------------------------------\n");
  166. printf("%s\n", g_main_ctx.cmdline);
  167. for (auto& pair : g_main_ctx.arg_kv) {
  168. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  169. }
  170. for (auto& item : g_main_ctx.arg_list) {
  171. printf("%s\n", item.c_str());
  172. }
  173. printf("================================================\n");
  174. printf("---------------env------------------------------\n");
  175. for (auto& pair : g_main_ctx.env_kv) {
  176. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  177. }
  178. printf("================================================\n");
  179. */
  180. // help
  181. if (get_arg("h")) {
  182. print_help();
  183. exit(0);
  184. }
  185. // version
  186. if (get_arg("v")) {
  187. print_version();
  188. exit(0);
  189. }
  190. // g_conf_ctx
  191. conf_ctx_init(&g_conf_ctx);
  192. const char* confile = get_arg("c");
  193. if (confile) {
  194. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  195. }
  196. parse_confile(g_main_ctx.confile);
  197. // test
  198. if (get_arg("t")) {
  199. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  200. exit(0);
  201. }
  202. // signal
  203. signal_init(on_reload);
  204. const char* signal = get_arg("s");
  205. if (signal) {
  206. signal_handle(signal);
  207. }
  208. #ifdef OS_UNIX
  209. // daemon
  210. if (get_arg("d")) {
  211. // nochdir, noclose
  212. int ret = daemon(1, 1);
  213. if (ret != 0) {
  214. printf("daemon error: %d\n", ret);
  215. exit(-10);
  216. }
  217. }
  218. #endif
  219. // pidfile
  220. create_pidfile();
  221. master_workers_run(worker_fn, (void*)(intptr_t)g_conf_ctx.port, g_conf_ctx.worker_processes, g_conf_ctx.worker_threads);
  222. return 0;
  223. }
  224. void worker_fn(void* userdata) {
  225. long port = (long)(intptr_t)(userdata);
  226. while (1) {
  227. printf("port=%ld pid=%ld tid=%ld\n", port, hv_getpid(), hv_gettid());
  228. hv_delay(60000);
  229. }
  230. }