Last active
November 17, 2018 16:42
-
-
Save jeremytregunna/624195c96869530ca2bfdf10431012aa 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 Exuma do | |
def doing_work(pid) do | |
IO.puts("Some work, like a DB fetch") | |
results = [1, 2, 42] | |
send(pid, {:update_state, results}) | |
end | |
end | |
defmodule Exuma.StateHolderThingy do | |
use GenServer | |
def start_link do | |
GenServer.start_link(__MODULE__, :ok) | |
end | |
def init(:ok) do | |
Process.send_after(self(), :tick, 100) | |
{:ok, nil} | |
end | |
def handle_info(:tick, state) do | |
Process.send_after(self(), :tick, 300_000) | |
schedule_work() | |
{:noreply, state} | |
end | |
def handle_info({:update_state, results}, _state) do | |
{:noreply, results)} | |
end | |
def schedule_work() do | |
spawn &(Exuma.doing_work(self())) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment