Created
September 6, 2017 21:05
-
-
Save konstantinzolotarev/4feb6b9e80c8410cc22b5f8768ccb913 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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