Guest User

Untitled

a guest
Nov 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. defmodule Exuma do
  2. def doing_work(pid) do
  3. IO.puts("Some work, like a DB fetch")
  4. results = [1, 2, 42]
  5. send(pid, {:update_state, results})
  6. end
  7. end
  8.  
  9. defmodule Exuma.StateHolderThingy do
  10. use GenServer
  11.  
  12. def start_link do
  13. GenServer.start_link(__MODULE__, :ok)
  14. end
  15.  
  16. def init(:ok) do
  17. Process.send_after(self(), :tick, 100)
  18. {:ok, nil}
  19. end
  20.  
  21. def handle_info(:tick, state) do
  22. Process.send_after(self(), :tick, 300_000)
  23. schedule_work()
  24. {:noreply, state}
  25. end
  26.  
  27. def handle_info({:update_state, results}, _state) do
  28. {:noreply, results)}
  29. end
  30.  
  31. def schedule_work() do
  32. spawn &(Exuma.doing_work(self()))
  33. end
  34. end
Add Comment
Please, Sign In to add comment