bitwalker / swarm

Easy clustering, registration, and distribution of worker processes for Erlang/Elixir
MIT License
1.2k stars 103 forks source link

Restart crashed process #68

Closed FabienHenon closed 6 years ago

FabienHenon commented 6 years ago

Is there any way to restart a crashed process?

I tried to set restart: :permanent but my process is not restarted when I kill it with: Process.exit(pid, :kill).

My process is created like this:

defmodule Scheduler do
  def start_link(default) do
    case Swarm.register_name(__MODULE__, __MODULE__, :register, [default]) do
      {:ok, pid} ->
        Swarm.join(:notification_sender, pid)
        {:ok, pid}

      {:error, reason} ->
        {:error, reason}
    end
  end

  def register(default) do
    GenServer.start_link(__MODULE__, default)
  end

  # ... snip
end

The supervisor calling start_link is defined like this:

defmodule App.Application do
  use Supervisor

  def start_link(args \\ []) do
    Supervisor.start_link(__MODULE__, args, name: __MODULE__)
  end

  def init(_) do
    children = [
      %{
        id: Scheduler,
        start: {Scheduler, :start_link, [[]]}
      }
    ]

    Supervisor.init(children, strategy: :one_for_one)
  end
end
kelostrada commented 6 years ago

If your process is not restarted at all then it's not Swarm's fault. If it is restarted but not being registered in Swarm then it is expected behaviour right now - see: https://github.com/bitwalker/swarm/issues/6

FabienHenon commented 6 years ago

I forgot to mention that because I start my Scheduler at application startup Swarm is postponing the creation of the process and the process is created from Swarm, not from my supervision tree

FabienHenon commented 6 years ago

Any idea on how to attach the process to my scheduler instead of swarm's? So that my process can be restarted if crashed?

bitwalker commented 6 years ago

An implementation of Swarm with support for external supervision is forthcoming. Since this issue is a duplicate of that one, I'm going to close this, but keep an eye out for that soon!