Skip to content

Commit

Permalink
chore(electric): Clean up the response returned from /api/status (#412)
Browse files Browse the repository at this point in the history
We should review the usage of connectors in the code base and refactor
those bits of code now that we have a single connector. In this PR I'm
only addressing the issue raised on Linear because it made sense to me
to make the Status API return a human-readable explanation of the
server's status.
  • Loading branch information
alco authored Sep 11, 2023
1 parent e70ac22 commit 74e99d1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
25 changes: 10 additions & 15 deletions components/electric/lib/electric/plug/status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,20 @@ defmodule Electric.Plug.Status do
use Plug.Router
import Plug.Conn

plug(:match)
plug(:dispatch)
plug :match
plug :dispatch

get "/" do
origins =
PostgresConnector.connectors()
|> Enum.map(fn origin ->
case PostgresConnectorMng.status(origin) do
:ready -> {origin, true}
:migration -> {origin, :migration}
_ -> {origin, false}
end
end)
[origin] = PostgresConnector.connectors()

data = %{
connectors: Map.new(origins)
}
msg =
if :ready == PostgresConnectorMng.status(origin) do
"Connection to Postgres is up!"
else
"Initializing connection to Postgres..."
end

send_resp(conn, 200, Jason.encode!(data))
send_resp(conn, 200, msg)
end

match _ do
Expand Down
13 changes: 6 additions & 7 deletions components/electric/lib/electric/postgres/postgres_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Electric.Replication.PostgresConnectorMng do
subscription: String.t(),
electric_connection: %{host: String.t(), port: pos_integer, dbname: String.t()}
},
state: :reinit | :init | :subscribe | :ready | :migration
state: :init | :subscribe | :ready
}
end

Expand All @@ -44,9 +44,9 @@ defmodule Electric.Replication.PostgresConnectorMng do
Electric.name(__MODULE__, origin)
end

@spec status(Connectors.origin()) :: :init | :subscribe | :ready | :migration
@spec status(Connectors.origin()) :: :init | :subscribe | :ready
def status(origin) do
GenServer.call(name(origin), {:status})
GenServer.call(name(origin), :status)
end

@impl GenServer
Expand All @@ -68,8 +68,7 @@ defmodule Electric.Replication.PostgresConnectorMng do
end

@impl GenServer
def handle_continue(init, %State{origin: origin} = state)
when init == :init or init == :reinit do
def handle_continue(:init, %State{origin: origin} = state) do
case initialize_postgres(state) do
{:ok, state1} ->
:ok = PostgresConnector.start_children(state.config)
Expand All @@ -79,7 +78,7 @@ defmodule Electric.Replication.PostgresConnectorMng do

error ->
Logger.error("initialization for postgresql failed with reason: #{inspect(error)}")
{:noreply, schedule_retry(init, state)}
{:noreply, schedule_retry(:init, state)}
end
end

Expand All @@ -94,7 +93,7 @@ defmodule Electric.Replication.PostgresConnectorMng do
end

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

Expand Down
4 changes: 2 additions & 2 deletions e2e/tests/01.01_simple_startup.lux
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[newshell electric_curl]
!curl http://localhost:5133/api/status
?{"connectors":{"postgres_1":true}}
??Connection to Postgres is up!

[cleanup]
[invoke teardown]
[invoke teardown]
2 changes: 1 addition & 1 deletion examples/beer-stars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Another service is an Electric sync service running replication on port `5133`:

```shell
$ curl http://localhost:5133/api/status
{"connectors":{"postgres_1":true}}
Connection to Postgres is up!
```

Finally, the app backend that talks to GitHub's GraphQL API runs on port `40001`:
Expand Down

0 comments on commit 74e99d1

Please sign in to comment.