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.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. void ExitSignalHandle::Start() {
  52. if (is_running_) {
  53. return;
  54. }
  55. is_running_ = true;
  56. master_exit_requested_ = std::promise<void>();
  57. worker_exit_requested_ = std::promise<void>();
  58. has_exited_.clear();
  59. InitSignalHandle();
  60. }
  61. void ExitSignalHandle::Stop() { HandleSignal(0); }
  62. bool ExitSignalHandle::HasStopped() { return !is_running_; }
  63. void ExitSignalHandle::HandleSignal(int sig) {
  64. auto &instance = Instance();
  65. instance.HandleSignalInner();
  66. }
  67. void ExitSignalHandle::HandleSignalInner() {
  68. if (!has_exited_.test_and_set()) {
  69. master_exit_requested_.set_value();
  70. worker_exit_requested_.set_value();
  71. is_running_ = false;
  72. }
  73. }
  74. } // namespace serving
  75. } // namespace mindspore

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