Skip to content

Instantly share code, notes, and snippets.

@ivanhercaz
Created July 12, 2021 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanhercaz/5cc67c168e5d007a2723e8029e684bb5 to your computer and use it in GitHub Desktop.
Save ivanhercaz/5cc67c168e5d007a2723e8029e684bb5 to your computer and use it in GitHub Desktop.
Basic supervision tree with Elixir (example provided by @Quarkex).
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
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