summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/stop_command.ex
blob: becb75a0b572295b6ba3faf54b32e22eec5413ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
##
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates.  All rights reserved.

defmodule RabbitMQ.CLI.Ctl.Commands.StopCommand do
  @behaviour RabbitMQ.CLI.CommandBehaviour
  alias RabbitMQ.CLI.Core.OsPid

  def merge_defaults(args, opts) do
    {args, Map.merge(%{idempotent: false}, opts)}
  end

  def switches(), do: [idempotent: :boolean]

  def validate([], _), do: :ok
  def validate([_pidfile_path], _), do: :ok
  def validate([_ | _] = args, _) when length(args) > 1, do: {:validation_failure, :too_many_args}

  def run([], %{node: node_name, idempotent: true}) do
    case :rabbit_misc.rpc_call(node_name, :rabbit, :stop_and_halt, []) do
      {:badrpc, :nodedown} -> {:ok, "Node #{node_name} is no longer running"}
      any -> any
    end
  end

  def run([], %{node: node_name, idempotent: false}) do
    :rabbit_misc.rpc_call(node_name, :rabbit, :stop_and_halt, [])
  end

  def run([pidfile_path], %{node: node_name}) do
    ret = OsPid.read_pid_from_file(pidfile_path, true)
    :rabbit_misc.rpc_call(node_name, :rabbit, :stop_and_halt, [])

    case ret do
      {:error, details} ->
        {:error, "could not read pid from file #{pidfile_path}. Error: #{details}"}

      {:error, :could_not_read_pid_from_file, {:contents, s}} ->
        {:error, "could not read pid from file #{pidfile_path}. File contents: #{s}"}

      {:error, :could_not_read_pid_from_file, details} ->
        {:error, "could not read pid from file #{pidfile_path}. Error: #{details}"}

      pid ->
        OsPid.wait_for_os_process_death(pid)
        {:ok, "process #{pid} (take from pid file #{pidfile_path}) is no longer running"}
    end
  end

  use RabbitMQ.CLI.DefaultOutput

  def usage, do: "stop [--idempotent] [<pidfile>]"

  def usage_additional() do
    [
      ["<pidfile>", "node PID file path to monitor. To avoid using a PID file, use 'rabbitmqctl shutdown'"],
      ["--idempotent", "return success if target node is not running (cannot be contacted)"]
    ]
  end

  def description(), do: "Stops RabbitMQ and its runtime (Erlang VM). Requires a local node pid file path to monitor progress."

  def help_section(), do: :node_management

  def banner([pidfile_path], %{node: node_name}) do
    "Stopping and halting node #{node_name} (will monitor pid file #{pidfile_path}) ..."
  end

  def banner(_, %{node: node_name}), do: "Stopping and halting node #{node_name} ..."
end