Created
July 12, 2021 18:33
-
-
Save ivanhercaz/5cc67c168e5d007a2723e8029e684bb5 to your computer and use it in GitHub Desktop.
Basic supervision tree with Elixir (example provided by @Quarkex).
This file contains 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 Client.Application do | |
@moduledoc false | |
use Application | |
@impl true | |
def start(_type, _args) do | |
children = [ | |
{Client, []} | |
] | |
opts = [strategy: :one_for_one, name: Client.Supervisor] | |
Supervisor.start_link(children, opts) | |
end | |
end |
This file contains 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 Client do | |
@moduledoc false | |
use GenServer | |
def start_link(opts \\ %{}), | |
do: GenServer.start_link(__MODULE__, opts) | |
def init(state) do | |
schedule_work(state[:interval] || 1) | |
{:ok, state} | |
end | |
def handle_info(:work, state) do | |
perform_work() | |
schedule_work(state[:interval] || 1) | |
{:noreply, state} | |
end | |
defp schedule_work(interval), | |
do: Process.send_after(self(), :work, interval * 1000) | |
def perform_work() do | |
# Do some work! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment