Question: Cleanly killing a process
Read through the docs and didn't see anything related to this, so thought I'd ask here.
Is there an intended way to kill a process and have it be cleanly removed from the Swarm cluster and it's tracking (as well as have the code on the process stop running), or is Process.exit(pid, :kill) or Process.exit(pid, :normal) sufficient?
I am handling this via:
~ incoming delete event do this:
pid = Swarm.whereis_name(check_id)
Swarm.unregister_name(check_id)
Probe.Svc.Registry.kill(pid)
~ Probe.Svc.Registry.kill:
def kill(pid), do: Supervisor.terminate_child(__MODULE__, pid)
But I would propose there should be a defdelegate or something for this or something to make things a little more clear.
This could be added to the Swarm API, though we probably want to differentiate between clean and brutal shutdowns.
I like the api for registering Swarm.register_name/4 what about extending the Swarm.unregister_name in the fashion of Swarm.register_name/4?
unregister_name and register_name/2 are present to work with the way Erlang performs lookups with whereis and registrations with {:via, registry, name} tuples. unregister_name does not kill the process, it simply removes the registration, and I don't want to change those semantics. Since name registration is not necessarily synonymous with the life of the process being registered (with the exception of register_name/4), I don't think it's a good idea to do so. I think it is better to remain explicit, and use Process.exit/2, by sending a message to the process telling it to shutdown, or by using something like Supervisor.terminate_child/2. Names are implicitly unregistered when the process dies, so it's very rare where you would need to ever call unregister_name/2 yourself, basically only if you need to rename a process.
We could introduce Swarm.exit(name) which would send the {:swarm, :die} message already sent to processes registered with register_name/4 when node changes occur and a process is moved or a conflict is seen and a registered process needs to be shut down - but this would not work with processes which don't recognize this message, so it may be a bit of a misnomer. I'd rather rely on the standard mechanisms for terminating processes.