Hi, I have a DynamicSupervisor
and Genserver
and I want to terminate a state, not all of them, so I send {:stop, :normal, state}
, but it calls init
again, and I don’t want it.
The document of the Supervisor
:transient
- the child process is restarted only if it terminates abnormally, i.e., with an exit reason other than:normal
,:shutdown
, or{:shutdown, term}
.
My code:
I start a state like this (dynamic_supervisor.ex
) →
def start_job(args) do
DynamicSupervisor.start_child(PluginStateOtpRunner, {PluginState, args})
My Genserver code:
defmodule MishkaInstaller.PluginState do
use GenServer
def stop(module: module_name) do
GenServer.cast(pid, {:delete, :module})
end
def child_spec(process_name) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [process_name]},
restart: :transient
}
end
@impl true
def handle_cast({:delete, :module}, state) do
{:stop, :normal, state}
end
@impl true
def terminate(reason, state) do
Logger.warn(
"#{Map.get(state, :name)} from #{Map.get(state, :event)} event of Plugins manager was Terminated,
Reason of Terminate #{inspect(reason)}"
)
end
end
when I call stop
function, it calls the init
again, and I cannot delete the state
iex(45)> MishkaInstaller.PluginState.stop(module: "joomla_login")
[warning] joomla_login from after_login event of Plugins manager was Terminated,
Reason of Terminate :normal
iex(46)> [info] joomla_login from after_login event of Plugins manager system `was started`
I changed use GenServer, restart: :transient
to use GenServer
, but it did not work for me
How can terminate a state and clean in on ram, thank you.