Created
September 22, 2018 19:59
-
-
Save ayrat555/f232a3fdda6067784aa7f2ff740a2284 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 QuotesBot.Polling.Server do | |
use GenServer | |
alias QuotesBot.{Polling.Logic, Config} | |
def start_link do | |
GenServer.start_link(__MODULE__, 0, name: __MODULE__) | |
end | |
def init(offset) do | |
schedule_polling() | |
{:ok, offset} | |
end | |
def handle_info(:poll, offset) do | |
new_offset = poll(offset) | |
schedule_polling() | |
{:noreply, new_offset + 1} | |
end | |
@spec poll(integer()) :: integer() | |
defp poll(offset) do | |
Logic.poll(offset) | |
end | |
defp schedule_polling do | |
Process.send_after(self(), :poll, Config.polling_period) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment