1## This Source Code Form is subject to the terms of the Mozilla Public
2## License, v. 2.0. If a copy of the MPL was not distributed with this
3## file, You can obtain one at https://mozilla.org/MPL/2.0/.
4##
5## Copyright (c) 2007-2021 VMware, Inc. or its affiliates.  All rights reserved.
6
7defmodule RabbitMQ.CLI.Diagnostics.Commands.CheckRunningCommand do
8  @moduledoc """
9  Exits with a non-zero code if the RabbitMQ app on the target node is not running.
10
11  This command is meant to be used in health checks.
12  """
13
14  @behaviour RabbitMQ.CLI.CommandBehaviour
15
16  use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout
17  use RabbitMQ.CLI.Core.MergesNoDefaults
18  use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments
19
20  def run([], %{node: node_name, timeout: timeout}) do
21    # Note: we use is_booted/1 over is_running/1 to avoid
22    # returning a positive result when the node is still booting
23    :rabbit_misc.rpc_call(node_name, :rabbit, :is_booted, [node_name], timeout)
24  end
25
26  def output(true, %{node: node_name} = _options) do
27    {:ok, "RabbitMQ on node #{node_name} is fully booted and running"}
28  end
29
30  def output(false, %{node: node_name} = _options) do
31    {:error,
32     "RabbitMQ on node #{node_name} is not running or has not fully booted yet (check with is_booting)"}
33  end
34
35  use RabbitMQ.CLI.DefaultOutput
36
37  def help_section(), do: :observability_and_health_checks
38
39  def description(), do: "Health check that exits with a non-zero code if the RabbitMQ app on the target node is not running"
40
41  def usage, do: "check_running"
42
43  def banner([], %{node: node_name}) do
44    "Checking if RabbitMQ is running on node #{node_name} ..."
45  end
46end
47