Skip to content

Instantly share code, notes, and snippets.

@konstantinzolotarev
Created September 6, 2017 21:05
Show Gist options
  • Save konstantinzolotarev/4feb6b9e80c8410cc22b5f8768ccb913 to your computer and use it in GitHub Desktop.
Save konstantinzolotarev/4feb6b9e80c8410cc22b5f8768ccb913 to your computer and use it in GitHub Desktop.
defmodule UserObserver do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
end
def init(_) do
{:ok, users} = load_users()
timer = set_timer()
{:ok, %{users: users, timer: timer}}
end
def handle_info(:reload, state)
# .... Perform actions on users ....
timer = set_timer()
{:noreply, %{state | timer: timer}}
end
defp load_users do
API.load_users()
end
defp set_timer do
Process.send_after(self(), :reload, 10_000)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment