Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment