I'm really struggling with Elixir supervisors and figuring out how to name them so that I can use them. Basically, I'm just trying to start a supervised Task
which I can send messages to.
So I have the following:
defmodule Run.Command do
def start_link do
Task.start_link(fn ->
receive do
{:run, cmd} -> System.cmd(cmd, [])
end
end)
end
end
with the project entry point as:
defmodule Run do
use Application
# 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
children = [
# Define workers and child supervisors to be supervised
worker(Run.Command, [])
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Run.Command]
Supervisor.start_link(children, opts)
end
end
At this point, I don't even feel confident that I'm using the right thing (Task
specifically). Basically, all I want is to spawn a process or task or GenServer or whatever is right when the application starts that I can send messages to which will in essence do a System.cmd(cmd, opts)
. I want this task or process to be supervised. When I send it a {:run, cmd, opts}
message such as {:run, "mv", ["/file/to/move", "/move/to/here"]}
I want it to spawn a new task or process to execute that command. For my use, I don't even need to ever get back the response from the task, I just need it to execute. Any guidance on where to go would be helpful. I've read through the getting started guide but honestly it left me more confused because when I try to do what is done it never turns out as it does in the application.
Thanks for your patience.