Hello,
I have a GenServer module that takes care of sending the time left for the start of the upcoming games via Phoenix Presence. My code works, but given that I started coding in Elixir a couple of days ago I’d like to know whether there is something that I could do better (I’m used to Node.js and everything is very different). Thanks in advance for any advice!
defmodule Hydnix.GameScheduler do
require Logger
use GenServer, restart: :transient
use Timex
alias Hydnix.Core
alias HydnixWeb.Presence
alias Timex.Duration
def start_link(args) do
GenServer.start_link(__MODULE__, args)
end
def init(args) do
pid = Keyword.get(args, :pid)
Core.next_games()
|> Enum.with_index()
|> Enum.each(fn {e, _idx} ->
{:ok, _} = Presence.track(Keyword.get(args, :pid), "game:#{e.id}", "time_left_to_start", %{
value: time_left_to_start(e.starts_at),
pid: self()
})
:timer.apply_interval(:timer.seconds(1), Hydnix.GameScheduler, :update, [{pid, e}])
end)
{:ok, args}
end
def update(args) do
{pid, e} = args
{:ok, _} = Presence.update(pid, "game:#{e.id}", "time_left_to_start", %{
value: time_left_to_start(e.starts_at),
pid: self()
})
end
def time_left_to_start(starts_at) do
Timex.diff(starts_at, DateTime.utc_now, :seconds)
|> Duration.from_seconds
|> Duration.to_string
end
end