Skip to content

Commit

Permalink
Apply fixes from PR
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelbej committed Oct 30, 2024
1 parent 7f5b6af commit 1f79bc8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
20 changes: 10 additions & 10 deletions lib/ask/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ defmodule Ask.Channel do
%{channel | status: status}
end

def get_status(%{paused: true}) do
%{status: "paused"}
end

def get_status(channel) do
unless channel.paused do
channel.id
|> ChannelStatusServer.get_channel_status()
|> case do
:up -> %{status: "up"}
:unknown -> %{status: "unknown"}
down_or_error -> down_or_error
end
else
%{status: "paused"}
channel.id
|> ChannelStatusServer.get_channel_status()
|> case do
:up -> %{status: "up"}
:unknown -> %{status: "unknown"}
down_or_error -> down_or_error
end
end

Expand Down
8 changes: 0 additions & 8 deletions lib/ask/runtime/channel_status_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ defmodule Ask.Runtime.ChannelStatusServer do
send(pid, :poll_once)
end

def wait(pid) do
GenServer.call(pid, :get)
end

def get_channel_status(channel_id) do
GenServer.call(@server_ref, {:get_channel_status, channel_id})
end
Expand All @@ -40,10 +36,6 @@ defmodule Ask.Runtime.ChannelStatusServer do
{:reply, get_status_from_state(channel_id, state), state}
end

def handle_call(:get, _, state) do
{:reply, state, state}
end

def handle_info(:poll, state) do
log_info("polling")

Expand Down
38 changes: 28 additions & 10 deletions lib/ask_web/controllers/channel_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,36 @@ defmodule AskWeb.ChannelController do
end

def pause(conn, %{"channel_id" => id}) do
channel_params = %{"paused" => true}
result = AskWeb.ChannelController.update(conn, %{"id" => id, "channel" => channel_params})
ChannelStatusServer.poll(ChannelStatusServer.server_ref())
ChannelStatusServer.wait(ChannelStatusServer.server_ref())
result
pause_channel(conn, id, true)
end

def unpause(conn, %{"channel_id" => id}) do
channel_params = %{"paused" => false}
result = AskWeb.ChannelController.update(conn, %{"id" => id, "channel" => channel_params})
ChannelStatusServer.poll(ChannelStatusServer.server_ref())
ChannelStatusServer.wait(ChannelStatusServer.server_ref())
result
pause_channel(conn, id, false)
end

defp pause_channel(conn, id, paused) do
channel_params = %{"paused" => paused}

Channel
|> Repo.get!(id)
|> authorize_channel(conn)
|> Repo.preload([:projects, :user])
|> Channel.changeset(channel_params)
|> Repo.update()
|> case do
{:ok, channel} ->
ChannelBroker.on_channel_settings_change(channel.id, channel.settings)
ChannelStatusServer.poll(ChannelStatusServer.server_ref())

render(conn, "show.json", channel: channel |> Repo.preload(:projects))

{:error, changeset} ->
Logger.warn("Error when pausing channel: #{id}")

conn
|> put_status(:unprocessable_entity)
|> put_view(AskWeb.ChangesetView)
|> render("error.json", changeset: changeset)
end
end
end

0 comments on commit 1f79bc8

Please sign in to comment.