anchor.diff:  %% @end
   %%
   shutdown() ->
  -  io:format("~n              Consul Cluster Deregistering: "),
  -  case deregister() of
  -    ok ->
  -      io:format("Node deregistered~n");
  -    Other ->
  -      io:format("Error deregistering: ~p~n", [Other])
  -  end,
  +  case unregister() of
  +    ok             -> info("Unregistered");
  +    {error, Error} -> err("Error unregistering: ~p", [Error])
  +  end.
  +
  +
  +%% @private
  +%% @spec join_cluster(term()) -> ok
  +%% @doc Have the current node join a cluster using the specified discovery node
  +%% @end
  +%%
  +join_cluster([]) ->
  +  debug("No nodes in existing cluster"),
  +  ok;
  +join_cluster(Nodes) ->
  +  debug("Joining existing cluster: ~p", [Nodes]),
  +  application:stop(rabbit),
  +  mnesia:stop(),
  +  rabbit_mnesia:reset(),
  +  rabbit_mnesia:join_cluster(lists:nth(1, Nodes), disc),
  +  mnesia:start(),
  +  rabbit:start(),
  +  info("Cluster joined"),
     ok.
   
   
   %% @private
  -%% @spec extract_nodes() -> list()
  +%% @spec maybe_register() -> ok|error
  +%% @doc Register with Consul and join the cluster if needed
  +%% @end
  +%%
  +maybe_register() ->
  +  Nodes = cluster_nodes(),
  +  case lists:member(node(), Nodes) of
  +    true ->
  +      debug("Node is already registered"),
  +      ok;
  +    false ->
  +      case register() of
  +        ok ->
  +          join_cluster(Nodes);
  +        {error, 400}   ->
  +          err("Permission denied when registering node with Consul"),
  +          error;
  +        {error, Error} ->
  +          err("Error registering: ~p", [Error]),
  +          error
  +    end
  +  end.
  +
  +
  +%% @private
  +%% @spec register() -> mixed
  +%% @doc Register with Consul as providing rabbitmq service
  +%% @end
  +%%
  +register() ->
  +  info("Registering node with Consul"),
  +  case autocluster_consul_client:post([agent, service, register], registration_body()) of
  +    ok -> ok;
  +    Error -> Error
  +  end.
  +
  +
  +%% @private
  +%% @spec unregister() -> mixed
  +%% @doc Deregister the rabbitmq service for this node from Consul
  +%% @end
  +%%
  +unregister() ->
  +  info("Unregistering node with Consul"),
  +  case autocluster_consul_client:get([agent, service, deregister, autocluster_consul_config:service()], []) of
  +    {ok, _} -> ok;
  +    Error -> Error
  +  end.
  +
  +
  +%% @private
  +%% @spec cluster_nodes() -> list()
   %% @doc Fetch the list of cluster nodes from Consul, returning them as a list of
   %%      atoms.
   %% @end
  
positive.code: %% @end
  %%
  init() ->
    io:format("~n              Consul Cluster Initializing: "),
    case register() of
      ok ->
        io:format("Node registered~n");
      {error, 400} ->
        io:format("permission denied when registering node with Consul, did you provide a ACL token?~n"),
        rabbit:stop_and_halt();
      {error, Error} ->
        io:format("Error registering: ~p~n", [Error]),
        rabbit:stop_and_halt()
    end,
    send_check_pass(),
    Interval = autocluster_consul_config:service_ttl() * 1000,
    {ok, _} = timer:apply_interval(Interval, ?MODULE, send_check_pass, []),
    Nodes = cluster_nodes(),
    case lists:member(node(), Nodes) of
      true -> ok;
      false -> join_cluster(Nodes)
    end.
  
  
  
negative.code: %%
  ttl_string(Value) ->
    list_to_atom(integer_to_list(Value) ++ "s").
  
  
  %% @private
  %% @spec log(Module, Function, Message, Args) -> ok
  %% @doc Ensure all logged lines share the same format
  %% @end
  %%
  log(Module, Fun, Message, Args) ->
    Module:Fun(string:join(["autocluster_consul: ", Message, "~n"], ""), Args).
  
  %% Logging methods
  
  debug(Message) -> log(rabbit_log, debug, Message, []).
  debug(Message, Args) -> log(rabbit_log, debug, Message, Args).
  info(Message) -> log(rabbit_log, info, Message, []).
  info(Message, Args) -> log(rabbit_log, info, Message, Args).
  err(Message) -> log(rabbit_log, error, Message, []).
  err(Message, Args) -> log(rabbit_log, error, Message, Args).
  warning(Message) -> log(rabbit_log, error, Message, []).
  warning(Message, Args) -> log(rabbit_log, error, Message, Args).
  