Created
July 26, 2016 02:20
-
-
Save ybur-yug/33196333aefccade51529d7c313ebc6d 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 MyApp.Periodically do | |
use GenServer | |
def start_link do | |
GenServer.start_link(__MODULE__, %{}) | |
end | |
def init(state) do | |
schedule_work() # Schedule work to be performed at some point | |
{:ok, state} | |
end | |
def handle_info(:work, state) do | |
# Do the work you desire here | |
schedule_work() # Reschedule once more | |
{:noreply, state} | |
end | |
defp schedule_work() do | |
Process.send_after(self(), :work, 2 * 60 * 60 * 1000) # In 2 hours | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment