main.cpp 3.6 KB
Newer Older
Y
yangrui07 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
#include <signal.h>
Y
yangrui07 已提交
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
#include <sys/stat.h>

#include <brpc/server.h>
#include <gflags/gflags.h>
#ifdef BCLOUD
#include "base/logging.h"
#else
#include "butil/logging.h"
#endif

#include "cube/control.h"
#include "cube/framework.h"
#include "cube/server.h"

DEFINE_int32(port, 8000, "TCP Port of this server");
DEFINE_int32(dict_split, 1, "data dict split for dictset");
DEFINE_bool(in_mem,
            true,
            "True[load data into memory] False[mmap data in disk]");
DECLARE_string(flagfile);

namespace rec {
namespace mcube {

bool g_signal_quit = false;
static void sigint_handler(int) { g_signal_quit = true; }  // sigint_handler

int run(int argc, char** argv) {
  google::ParseCommandLineFlags(&argc, &argv, true);

// initialize logger instance
#ifdef BCLOUD
  logging::LoggingSettings settings;
  settings.logging_dest = logging::LOG_TO_FILE;

  std::string filename(argv[0]);
  filename = filename.substr(filename.find_last_of('/') + 1);
  settings.log_file =
      strdup((std::string("./log/") + filename + ".log").c_str());
  settings.delete_old = logging::DELETE_OLD_LOG_FILE;
  logging::InitLogging(settings);

  logging::ComlogSinkOptions cso;
  cso.process_name = filename;
  cso.enable_wf_device = true;
  logging::ComlogSink::GetInstance()->Setup(&cso);
#else
  if (FLAGS_log_dir == "") {
    FLAGS_log_dir = "./log";
  }

  struct stat st_buf;
  int ret = 0;
  if ((ret = stat(FLAGS_log_dir.c_str(), &st_buf)) != 0) {
    mkdir(FLAGS_log_dir.c_str(), 0777);
    ret = stat(FLAGS_log_dir.c_str(), &st_buf);
    if (ret != 0) {
      LOG(WARNING) << "Log path " << FLAGS_log_dir
                   << " not exist, and create fail";
      return -1;
    }
  }
  google::InitGoogleLogging(strdup(argv[0]));
  FLAGS_logbufsecs = 0;
  FLAGS_logbuflevel = -1;
#endif
  LOG(INFO) << "Succ initialize logger";

  Framework* framework = Framework::instance();
  ret = framework->init(FLAGS_dict_split, FLAGS_in_mem);
  if (ret != 0) {
    LOG(ERROR) << "init predict framework failed";
    return ret;
  }

  Server cube;
  Control cntl;

  brpc::Server server;
  server.set_version("Cube Service");
  brpc::ServerOptions option;

  if (server.AddService(&cube, brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
    LOG(ERROR) << "Failed to add predict service";
    return -1;
  }

  if (server.AddService(&cntl, brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
    LOG(ERROR) << "Failed to add predict service";
    return -1;
  }

  if (server.Start(FLAGS_port, &option) != 0) {
    LOG(ERROR) << "Fail to start service";
    return -1;
  }
  LOG(INFO) << "cube service start";

  signal(SIGINT, sigint_handler);
  while (!g_signal_quit) {
    sleep(1);
  }

  return 0;
}

}  // namespace mcube
}  // namespace rec

int main(int argc, char** argv) {
  if (google::SetCommandLineOption("bvar_dump", "true").empty()) {
    LOG(ERROR) << "Failed to dump bvar file";
    return -1;
  }
  google::SetCommandLineOption("flagfile", "conf/gflags.conf");
  return ::rec::mcube::run(argc, argv);
}

/* vim: set ts=4 sw=4 sts=4 tw=100 */