You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

exit_handle.cc 2.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  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. #include "common/exit_handle.h"
  17. #include <signal.h>
  18. #include <utility>
  19. namespace mindspore {
  20. namespace serving {
  21. ExitSignalHandle &ExitSignalHandle::Instance() {
  22. static ExitSignalHandle instance;
  23. return instance;
  24. }
  25. void ExitSignalHandle::InitSignalHandle() {
  26. if (!has_inited_.test_and_set()) {
  27. signal(SIGINT, HandleSignal);
  28. signal(SIGTERM, HandleSignal);
  29. }
  30. }
  31. // waiting ctrl+c or stop message to exit,
  32. // if no server is running or server has exited, there is no need to wait
  33. void ExitSignalHandle::MasterWait() {
  34. if (!is_running_) {
  35. MSI_LOG_INFO << "Exit Handle has not started or has exited";
  36. return;
  37. }
  38. auto exit_future = master_exit_requested_.get_future();
  39. exit_future.wait();
  40. }
  41. // waiting ctrl+c or stop message to exit,
  42. // if no server is running or server has exited, there is no need to wait
  43. void ExitSignalHandle::WorkerWait() {
  44. if (!is_running_) {
  45. MSI_LOG_INFO << "Exit Handle has not started or has exited";
  46. return;
  47. }
  48. auto exit_future = worker_exit_requested_.get_future();
  49. exit_future.wait();
  50. }
  51. // waiting ctrl+c or stop message to exit,
  52. // if no server is running or server has exited, there is no need to wait
  53. void ExitSignalHandle::AgentWait() {
  54. if (!is_running_) {
  55. MSI_LOG_INFO << "Exit Handle has not started or has exited";
  56. return;
  57. }
  58. auto exit_future = agent_exit_requested_.get_future();
  59. exit_future.wait();
  60. }
  61. void ExitSignalHandle::Start() {
  62. if (is_running_) {
  63. return;
  64. }
  65. is_running_ = true;
  66. master_exit_requested_ = std::promise<void>();
  67. worker_exit_requested_ = std::promise<void>();
  68. agent_exit_requested_ = std::promise<void>();
  69. has_exited_.clear();
  70. InitSignalHandle();
  71. }
  72. void ExitSignalHandle::Stop() { HandleSignal(0); }
  73. bool ExitSignalHandle::HasStopped() { return !is_running_; }
  74. void ExitSignalHandle::HandleSignal(int sig) {
  75. auto &instance = Instance();
  76. instance.HandleSignalInner();
  77. }
  78. void ExitSignalHandle::HandleSignalInner() {
  79. if (!has_exited_.test_and_set()) {
  80. master_exit_requested_.set_value();
  81. worker_exit_requested_.set_value();
  82. agent_exit_requested_.set_value();
  83. is_running_ = false;
  84. }
  85. }
  86. } // namespace serving
  87. } // namespace mindspore

A lightweight and high-performance service module that helps MindSpore developers efficiently deploy online inference services in the production environment.