Reputation: 53
Been struggling wrapping my head around this and hoping someone would be able to point me in the right direction.
I ran mix new my_other_app --sup
and got the bellow application:
defmodule MyOtherApp do
def start_link do
Task.start_link(fn -> loop() end)
end
def loop do
IO.puts "running..."
:timer.sleep(1000)
loop()
end
end
And my application supervisor:
defmodule MyOtherApp.Application do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
worker(MyOtherApp, [])
]
opts = [strategy: :one_for_one, name: MyOtherApp.Supervisor]
Supervisor.start_link(children, opts)
end
end
and mix application/0
def application do
[extra_applications: [:logger],
mod: {MyOtherApp.Application, []}]
end
Now my questions are:
mix app.start
, the application runs and halts straight away?mix app.start
instead of mix run
?mix run --no-halt
to get the application continue running forever, but why? Why is it that if I remove the supervisor and put a call to loop/0
at the bottom of my my_other_app.ex
, it continues running forever with mix run
but with the supervisor it doesn't?Thanks for the help!
Upvotes: 2
Views: 599
Reputation: 53
The issue seems to be related to how I was calling the loop/0
function within a task process that wasn't being supervised.
Pretty simple but the process of writing this helped me get there ;)
Thanks for the rubber ducking, SO ;)
EDIT: I will still mark right answer to someone who can explain to me when I'd run mix run
vs mix app.start
Thanks
Upvotes: 1