Skip to content

Instantly share code, notes, and snippets.

@elcritch
Created September 15, 2017 00:05
Show Gist options
  • Save elcritch/dde692b0e367819ef3606a88c7621c0a to your computer and use it in GitHub Desktop.
Save elcritch/dde692b0e367819ef3606a88c7621c0a to your computer and use it in GitHub Desktop.
Example task supervisor using a function handle in Elixir (nerves example)
require Logger
defmodule TestNpn.Application do
use Application
alias Nerves.Networking
@interface :eth0
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec, warn: false
# Define workers and child supervisors to be supervised
children = [
worker(Task, [fn -> init_network() end], restart: :transient, id: TestNerves.Application.Network),
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: TestNpn.Supervisor]
Supervisor.start_link(children, opts)
end
def init_network() do
Logger.info "Starting Networking:interface: #{inspect @interface} pid: #{inspect self()}"
{:ok, _} = Networking.setup @interface
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment