| 682 | } |
| 683 | |
| 684 | int stop(const fs::path & pid_file, bool force) |
| 685 | { |
| 686 | UInt64 pid = isRunning(pid_file); |
| 687 | |
| 688 | if (!pid) |
| 689 | return 0; |
| 690 | |
| 691 | int signal = force ? SIGKILL : SIGTERM; |
| 692 | const char * signal_name = force ? "kill" : "terminate"; |
| 693 | |
| 694 | if (0 == kill(pid, signal)) |
| 695 | fmt::print("Sent {} signal to process with pid {}.\n", signal_name, pid); |
| 696 | else |
| 697 | throwFromErrno(fmt::format("Cannot send {} signal", signal_name), ErrorCodes::SYSTEM_ERROR); |
| 698 | |
| 699 | size_t try_num = 0; |
| 700 | constexpr size_t num_tries = 60; |
| 701 | for (; try_num < num_tries; ++try_num) |
| 702 | { |
| 703 | fmt::print("Waiting for server to stop\n"); |
| 704 | if (!isRunning(pid_file)) |
| 705 | { |
| 706 | fmt::print("Server stopped\n"); |
| 707 | break; |
| 708 | } |
| 709 | sleepForSeconds(1); |
| 710 | } |
| 711 | |
| 712 | if (try_num == num_tries) |
| 713 | { |
| 714 | fmt::print("Will terminate forcefully.\n", pid); |
| 715 | if (0 == kill(pid, 9)) |
| 716 | fmt::print("Sent kill signal.\n", pid); |
| 717 | else |
| 718 | throwFromErrno("Cannot send kill signal", ErrorCodes::SYSTEM_ERROR); |
| 719 | |
| 720 | /// Wait for the process (100 seconds). |
| 721 | constexpr size_t num_kill_check_tries = 1000; |
| 722 | constexpr size_t kill_check_delay_ms = 100; |
| 723 | for (size_t i = 0; i < num_kill_check_tries; ++i) |
| 724 | { |
| 725 | fmt::print("Waiting for server to be killed\n"); |
| 726 | if (!isRunning(pid_file)) |
| 727 | { |
| 728 | fmt::print("Server exited\n"); |
| 729 | break; |
| 730 | } |
| 731 | sleepForMilliseconds(kill_check_delay_ms); |
| 732 | } |
| 733 | |
| 734 | if (isRunning(pid_file)) |
| 735 | { |
| 736 | throw Exception(ErrorCodes::CANNOT_KILL, |
| 737 | "The server process still exists after %zu ms", |
| 738 | num_kill_check_tries, kill_check_delay_ms); |
| 739 | } |
| 740 | } |
| 741 |
no test coverage detected