!2783 fix serving exit signal handle

Merge pull request !2783 from dinghao/master
This commit is contained in:
mindspore-ci-bot 2020-07-01 16:47:53 +08:00 committed by Gitee
commit 188d1fc777
1 changed files with 9 additions and 12 deletions

View File

@ -227,6 +227,7 @@ class MSServiceImpl final : public MSService::Service {
Status Server::BuildAndStart() { Status Server::BuildAndStart() {
// handle exit signal // handle exit signal
signal(SIGINT, HandleSignal); signal(SIGINT, HandleSignal);
signal(SIGTERM, HandleSignal);
Status res; Status res;
auto option_args = Options::Instance().GetArgs(); auto option_args = Options::Instance().GetArgs();
std::string server_address = "0.0.0.0:" + std::to_string(option_args->grpc_port); std::string server_address = "0.0.0.0:" + std::to_string(option_args->grpc_port);
@ -258,21 +259,17 @@ Status Server::BuildAndStart() {
} }
g_ctx = ctx; g_ctx = ctx;
#endif #endif
MSServiceImpl service; MSServiceImpl msService;
grpc::EnableDefaultHealthCheckService(true); grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin(); grpc::reflection::InitProtoReflectionServerBuilderPlugin();
// Set the port is not reuseable // Set the port is not reuseable
auto option = grpc::MakeChannelArgumentOption(GRPC_ARG_ALLOW_REUSEPORT, 0); auto option = grpc::MakeChannelArgumentOption(GRPC_ARG_ALLOW_REUSEPORT, 0);
grpc::ServerBuilder builder; grpc::ServerBuilder serverBuilder;
builder.SetOption(std::move(option)); serverBuilder.SetOption(std::move(option));
builder.SetMaxMessageSize(uint32max); serverBuilder.SetMaxMessageSize(uint32max);
// Listen on the given address without any authentication mechanism. serverBuilder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); serverBuilder.RegisterService(&msService);
// Register "service" as the instance through which we'll communicate with std::unique_ptr<grpc::Server> server(serverBuilder.BuildAndStart());
// clients. In this case it corresponds to an *synchronous* service.
builder.RegisterService(&service);
// Finally assemble the server.
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
if (server == nullptr) { if (server == nullptr) {
MS_LOG(ERROR) << "The serving server create failed"; MS_LOG(ERROR) << "The serving server create failed";
ClearEnv(); ClearEnv();
@ -280,7 +277,7 @@ Status Server::BuildAndStart() {
} }
auto grpc_server_run = [&server]() { server->Wait(); }; auto grpc_server_run = [&server]() { server->Wait(); };
std::thread serving_thread(grpc_server_run); std::thread serving_thread(grpc_server_run);
MS_LOG(INFO) << "Server listening on " << server_address << std::endl; MS_LOG(INFO) << "MS Serving listening on " << server_address;
auto exit_future = exit_requested.get_future(); auto exit_future = exit_requested.get_future();
exit_future.wait(); exit_future.wait();
ClearEnv(); ClearEnv();