From 1f79bc8043b8f8aae14dfc2dada11b1be4ee0f45 Mon Sep 17 00:00:00 2001 From: Ismael Bejarano Date: Tue, 29 Oct 2024 23:18:34 -0300 Subject: [PATCH] Apply fixes from PR --- lib/ask/channel.ex | 20 +++++----- lib/ask/runtime/channel_status_server.ex | 8 ---- lib/ask_web/controllers/channel_controller.ex | 38 ++++++++++++++----- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/lib/ask/channel.ex b/lib/ask/channel.ex index 646ed7cb4..335f6cc77 100644 --- a/lib/ask/channel.ex +++ b/lib/ask/channel.ex @@ -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 diff --git a/lib/ask/runtime/channel_status_server.ex b/lib/ask/runtime/channel_status_server.ex index d47040c48..895c3b4ed 100644 --- a/lib/ask/runtime/channel_status_server.ex +++ b/lib/ask/runtime/channel_status_server.ex @@ -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 @@ -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") diff --git a/lib/ask_web/controllers/channel_controller.ex b/lib/ask_web/controllers/channel_controller.ex index 80ea187f5..cff426e87 100644 --- a/lib/ask_web/controllers/channel_controller.ex +++ b/lib/ask_web/controllers/channel_controller.ex @@ -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