Skip to content

Instantly share code, notes, and snippets.

@jeremytregunna
Last active November 17, 2018 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremytregunna/624195c96869530ca2bfdf10431012aa to your computer and use it in GitHub Desktop.
Save jeremytregunna/624195c96869530ca2bfdf10431012aa to your computer and use it in GitHub Desktop.
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