main.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "global.h"
  2. #include "data_manager.h"
  3. #include "data_conf.h"
  4. #include "dtc_global.h"
  5. #include "config.h"
  6. #include "proc_title.h"
  7. #include "dbconfig.h"
  8. #include "log.h"
  9. #include <sys/types.h>
  10. #include <dirent.h>
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <stdlib.h>
  14. #include <iostream>
  15. #include <thread>
  16. const char data_project_name[] = "data_lifecycle_manager";
  17. int scan_file(const char* path, std::vector<std::string>& config_vec){
  18. DIR *dir = opendir(path);
  19. if(dir == NULL){
  20. log4cplus_error("opendir error.");
  21. return DTC_CODE_LOAD_CONFIG_ERR;
  22. }
  23. struct dirent *dirent;
  24. while(dirent = readdir(dir)){
  25. string dir_name = dirent->d_name;
  26. string match_str="dtc-conf";
  27. if(dir_name == "." || dir_name == ".." || dir_name == "dtc-conf-0.yaml"){
  28. continue;
  29. } else if(dir_name.size() > match_str.size() && dir_name.substr(0, match_str.size()) == match_str){
  30. config_vec.push_back("../conf/" + dir_name);
  31. }
  32. }
  33. return 0;
  34. }
  35. void thread_func(const std::string& config_path){
  36. DataConf* p_data_conf = new DataConf();
  37. if(p_data_conf->LoadConfig(config_path) != 0){
  38. log4cplus_error("load_config error.");
  39. return;
  40. }
  41. ConfigParam config_param;
  42. if(p_data_conf->ParseConfig(config_path, config_param) != 0){
  43. log4cplus_error("parse_config error.");
  44. return;
  45. }
  46. DataManager* p_data_manager = new DataManager(config_param);
  47. if(0 != p_data_manager->ConnectAgent()){
  48. log4cplus_error("ConnectAgent error.");
  49. return;
  50. }
  51. if(0 != p_data_manager->ConnectFullDB()){
  52. log4cplus_error("ConnectFullDB error.");
  53. return;
  54. }
  55. if(0 != p_data_manager->CreateTable()){
  56. log4cplus_error("CreateTable error.");
  57. }
  58. p_data_manager->DoProcess();
  59. if(NULL != p_data_manager){
  60. delete p_data_manager;
  61. }
  62. if(NULL != p_data_conf){
  63. delete p_data_conf;
  64. }
  65. }
  66. int main(int argc, char *argv[]){
  67. init_proc_title(argc, argv);
  68. set_proc_title("agent-data-lifecycle");
  69. init_log4cplus();
  70. log4cplus_info("%s v%s: starting....", data_project_name, version);
  71. if(init_daemon() < 0){
  72. log4cplus_error("init_daemon error.");
  73. return DTC_CODE_INIT_DAEMON_ERR;
  74. }
  75. std::vector<std::string> config_vec;
  76. int ret = scan_file("../conf", config_vec);
  77. if(0 != ret){
  78. log4cplus_error("scan_file error.");
  79. return DTC_CODE_LOAD_CONFIG_ERR;
  80. }
  81. std::vector<std::thread> thread_vec;
  82. for(auto config_path : config_vec){
  83. thread_vec.push_back(std::thread(thread_func, config_path));
  84. }
  85. for (auto vit = thread_vec.begin(); vit != thread_vec.end(); vit++) {
  86. if (vit->joinable()) {
  87. vit->join();
  88. }
  89. }
  90. /*if(NULL != p_data_manager){
  91. delete p_data_manager;
  92. }
  93. */
  94. log4cplus_info("%s v%s: stopped", data_project_name, version);
  95. //Logger::shutdown();
  96. //daemon_cleanup();
  97. //DaemonCrashed(1);
  98. // log4cplus::deinitialize();
  99. return 0;
  100. }