Strange error with Process.sleep in a supervised Task

This simple code is not working

defmodule ElixirCluster do
  use Application

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      worker(Task, [fn ->
        IO.puts("Hello")
        Process.sleep(1000) #:timer.sleep(1000)
        IO.puts("World")
      end])
    ]
    opts = [strategy: :one_for_one, name: ElixirCluster.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

it only prints Hello and then the program stop, it doesn’t even retry, and never prints World. I dont have a clue what is wrong here.

The application is starting, then it starts its children before it returns, then the child is sleeping, meaning that it will then wait for the timeout message from the system, but the system is not started yet as the application has not returned from start/2 yet.

I.E., never do anything blocking during the application start/2 call at all, ever ever. :slight_smile:

@OvermindDL1 How can I execute a supervised Task every 1h?

http://erlang.org/doc/man/timer.html#send_interval-2

1 Like

@deadtrickster Thanks! Care to elaborate on the solution?

The answer was to run with mix run --no-halt