Skip to content

Commit

Permalink
Make ChannelStatusServer.poll callable from production
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelbej committed Oct 14, 2024
1 parent 507c01b commit f4f6db9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1,035 deletions.
52 changes: 38 additions & 14 deletions lib/ask/runtime/channel_status_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ defmodule Ask.Runtime.ChannelStatusServer do
end

def poll(pid) do
send(pid, :poll)
send(pid, :poll_once)
end

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

def get_channel_status(channel_id) do
Expand All @@ -36,29 +40,31 @@ 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")

try do
Survey.running_channels()
|> Repo.preload(:user)
|> Enum.each(fn c ->
poll_channels(state)

unless c.paused do
previous_status = get_status_from_state(c.id, state)
{:noreply, state}
after
:timer.send_after(@poll_interval, :poll)
end
end

spawn(fn ->
status = ChannelBroker.check_status(c.id)
timestamp = Timex.now()
def handle_info(:poll_once, state) do
log_info("poll forced")

process_channel_status_change(status, previous_status, timestamp, c)
end)
end
end)
try do
poll_channels(state)

{:noreply, state}
after
:timer.send_after(@poll_interval, :poll)
nil
end
end

Expand All @@ -74,6 +80,24 @@ defmodule Ask.Runtime.ChannelStatusServer do
Logger.info("ChannelStatusServer: #{message}")
end

defp poll_channels(state) do
Survey.running_channels()
|> Repo.preload(:user)
|> Enum.each(fn c ->

unless c.paused do
previous_status = get_status_from_state(c.id, state)

spawn(fn ->
status = ChannelBroker.check_status(c.id)
timestamp = Timex.now()

process_channel_status_change(status, previous_status, timestamp, c)
end)
end
end)
end

defp process_channel_status_change({:down, _messages}, %{status: :down}, _timestamp, _channel) do
nil
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ask_web/controllers/channel_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ defmodule AskWeb.ChannelController do
render(conn, "show.json", channel: channel |> Repo.preload([:projects, :user]))
end

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

def unpause(conn, %{"id" => id}) do
def unpause(conn, %{"channel_id" => id}) do
channel_params = %{"paused" => false}
AskWeb.ChannelController.update(conn, %{"id" => id, "channel" => channel_params})
end
Expand Down
Loading

0 comments on commit f4f6db9

Please sign in to comment.