Non-working Agent reload code

I have an Agent which processes a tree of TOML files on startup,
turning it into a tree of maps. Occasionally, while debugging,
I’d like to reload this tree. So, I set up a “/reload” page to:

  • get the current time
  • tell the Agent to print a message and then kill itself
  • get the current time
  • calculate the duration
  • render a “reload” page

Although the message is getting printed, the “reload” page is
not appearing. The browser says:

This page isn’t working
localhost didn’t send any data.

Suggestions? For the relevant code, see this gist:

Tracing RefData.Server.start_link/0, I found that it isn’t getting run after the exit. So, I probably have a problem in the Supervision setup. I will look into this, but meanwhile, here is a gist of my current setup:

It’s probably better and more reliable to restart the child using the supervisor. I don’t remember how Elixir Supervisors assign the id to their children, but this is generally how you’d do it:

:ok = Supervisor.terminate_child(RefData.Supervisor, RefData.Server)
{:ok, _pid} = Supervisor.restart_child(RefData.Supervisor, RefData.Server)

The reason you might be getting no data sometimes is:

def reload(message) do
    IO.puts message
    pid   = self() # <--
    Process.exit(pid, :kill)
end

self() is the pid of the calling process. I think you’re crashing the connection and not the agent. The reason sometimes data does get returned and rendered is because Process.exit/2 is asynchronous.

Well, I tried that, but I got a nastygram. Help?

[error] #PID&lt;0.555.0&gt; running PhxHttpWeb.Endpoint
(connection #PID&lt;0.554.0&gt;, stream id 1) terminated

Server: localhost:4000 (http)

Request: GET /reload

** (exit) exited in: GenServer.call(Supervisor,
{:terminate_child, RefData.Server}, :infinity)

** (EXIT) no process: the process is not alive or
there's no process currently associated with the given name,
possibly because its application isn't started

Looking over my startup code, I can’t see anything that differs much
from the way PhxHttp does things…