oneshot
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
defmodule Rsh26pool.RoomCloser do
|
||||
@moduledoc """
|
||||
Periodically closes rooms whose `closes_at` deadline has passed (the "auto
|
||||
end" feature). Closing broadcasts to any connected LiveViews so open pages
|
||||
transition to the results/grouping view without a manual refresh.
|
||||
"""
|
||||
use GenServer
|
||||
|
||||
require Logger
|
||||
|
||||
alias Rsh26pool.Voting
|
||||
|
||||
@interval :timer.seconds(10)
|
||||
|
||||
def start_link(opts) do
|
||||
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(_opts) do
|
||||
schedule()
|
||||
{:ok, %{}}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info(:sweep, state) do
|
||||
sweep()
|
||||
schedule()
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
defp sweep do
|
||||
for room <- Voting.list_rooms_to_auto_close() do
|
||||
case Voting.close_room(room) do
|
||||
{:ok, _} ->
|
||||
:ok
|
||||
|
||||
other ->
|
||||
Logger.warning("RoomCloser failed to close room #{room.id}: #{inspect(other)}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp schedule, do: Process.send_after(self(), :sweep, @interval)
|
||||
end
|
||||
Reference in New Issue
Block a user