Thiago Felippe
Thiago Felippe

Reputation: 53

mix run terminates my programs

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:

Thanks for the help!

Upvotes: 2

Views: 599

Answers (1)

Thiago Felippe
Thiago Felippe

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

Related Questions