Skip to content

Commit

Permalink
Add Jackalope.Session connection_status/0
Browse files Browse the repository at this point in the history
Jackalope provides connection status to the implementation of Jackalope.Handler when it changes. However, some situations may require the implementation (or another process) to query the current connection status (i.e. the implementation may be a process that crashed and lost state and needs to rebuild it without waiting for Jackalope connection status to change for the needed event)
  • Loading branch information
LivInAbsurdism authored and jfcloutier committed Nov 23, 2022
1 parent 5743c7d commit 537b023
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/jackalope.ex
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ defmodule Jackalope do
]
defdelegate publish(topic, payload, opts \\ []), to: Jackalope.Session

@doc """
Get the current MQTT connection status
"""
@spec connection_status() :: :offline | :online
defdelegate connection_status(), to: Jackalope.Session

# TODO Get rid of this stuff
defp connection_options(opts) do
server =
Expand Down
12 changes: 12 additions & 0 deletions lib/jackalope/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ defmodule Jackalope.Session do
GenServer.cast(__MODULE__, {:report_connection_status, status})
end

@spec connection_status() :: :online | :offline
def connection_status() do
GenServer.call(__MODULE__, :connection_status)
catch
_, _ -> :offline
end

## MQTT-ing
@doc false
@spec publish(String.t(), any(), keyword) :: :ok | {:error, :invalid_qos | :invalid_ttl}
Expand Down Expand Up @@ -92,6 +99,11 @@ defmodule Jackalope.Session do
{:ok, initial_state, {:continue, :consume_work_list}}
end

@impl GenServer
def handle_call(:connection_status, _from, state) do
{:reply, state.connection_status, state}
end

@impl GenServer
# Connection status changes
def handle_cast({:report_connection_status, :up}, state) do
Expand Down

0 comments on commit 537b023

Please sign in to comment.