Skip to content

Instantly share code, notes, and snippets.

@elbow-jason
Created January 8, 2017 00:25
Show Gist options
  • Save elbow-jason/be2383a71ace1a798403900aa0ab92af to your computer and use it in GitHub Desktop.
Save elbow-jason/be2383a71ace1a798403900aa0ab92af to your computer and use it in GitHub Desktop.
A GenServer Poller
defmodule Poller do
use GenServer
def start_link do
# this starts the genserver (for supervision)
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_) do
set_timer
{:ok, []}
end
def execute do
GenServer.cast(__MODULE__, :do_the_task)
end
def set_timer do
delay_in_ms = 3_000
:timer.apply_after(delay_in_ms, __MODULE__, :execute, [])
end
def handle_cast(:do_the_task, state) do
set_timer
# this where you'd do the task
do_the_task
{:noreply, state}
end
def do_the_task do
IO.puts("Task was done @ #{DateTime.utc_now |> to_string}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment