Skip to content

Instantly share code, notes, and snippets.

@ayrat555
Created September 22, 2018 19:59
Show Gist options
  • Save ayrat555/f232a3fdda6067784aa7f2ff740a2284 to your computer and use it in GitHub Desktop.
Save ayrat555/f232a3fdda6067784aa7f2ff740a2284 to your computer and use it in GitHub Desktop.
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