How to clean Agent process?

I do start a process in agent, like this for example:

{:ok, pid} = Agent.start_link( fn -> stream end, name: {:global, String.to_atom(process_name)})

I want to clean the process and free the registered name too, How would I clean that process ? as I have registered it globally {:global, String.to_atom(process_name)} ?

Agent.stop sounds the most logical. The name will be unregistered automatically, it doesn’t matter if the process is stopped nicely, killed, crashed, etc.

Precisely, if the process dies in any way, the name dies too. :slight_smile:

Thanks! I thought the name will be pointing to a null reference, cool Elixir :slight_smile:

Is this the right way to use stop ?

Agent.stop({:global, process_name});

where {:global, process_name} were used in the first place of creating the agent:

{:ok, pid} = Agent.start_link( fn -> stream end, name: {:global, process_name})

correct?

If you go here: Agent — Elixir v1.20.2

Then click on the “agent” link in the type definition, it will bring you to the definition of that type, which does include the :global tuple (as one of the possible values for name). So yes, it should be fine.

You can double check out that the process was unregistered by calling :global.whereis_name(process_name). This returns a pid iff the process is globally registered. You may also find Process.alive?(pid) interesting.

Ah, thanks for the tip on doc, :global.whereis_name(process_name) is just what I need.

If you are curious, by ‘naming a pid’ with a tuple like {:global, :blahname} you are saying to register it with the :global module and it is what handles monitoring, auto-unregistering, etc… You can make and use your own custom registration service, say named :blahglobal and use it like {:via, :blahglobal, :blahname}. :slight_smile:

I am new to Elixir, I don’t know if I will need or why I would need to have my own custom registration service, at least, I know how to register an agent and recall it now :slight_smile: