Last active
December 8, 2018 16:36
-
-
Save RichMorin/fd894b325579d360e829687c7f4800da to your computer and use it in GitHub Desktop.
non-working Agent reload code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? See below for the relevant code... | |
========================================================= | |
defmodule PhxHttpWeb.Router do | |
... | |
scope "/", PhxHttpWeb do | |
... | |
get "/reload", TreeController, :reload | |
... | |
end | |
end | |
========================================================= | |
defmodule PhxHttpWeb.TreeController do | |
... | |
def reload(conn, _params) do | |
time_1 = Time.utc_now() | |
message = "Reloading by request." | |
RefData.reload(message) | |
time_2 = Time.utc_now() | |
duration = Time.diff(time_2, time_1, :millisecond) | |
render(conn, "reload.html", | |
duration: duration, | |
item_key: nil) | |
end | |
... | |
end | |
========================================================= | |
<!-- templates/tree/reload.html.eex --> | |
<h2>Reload</h2> | |
At your request, RefData.Server has been crashed and restarted, | |
causing the data to be reloaded from the TOML tree. | |
This process took approximately <%= @duration %> milliseconds. | |
========================================================= | |
defmodule RefData do | |
... | |
defdelegate reload(message), to: Server | |
... | |
end | |
========================================================= | |
defmodule RefData.Server do | |
... | |
def reload(message) do | |
IO.puts message | |
pid = self() | |
Process.exit(pid, :kill) | |
end | |
... | |
def start_link() do | |
Agent.start_link(&build_map/0, name: @me) | |
end | |
def build_map() do | |
tree_abs = "/Users/rdm/_Work/P_Elixir/PA_hax/PA_toml" #K | |
toml_map = Loader.load(tree_abs) | |
toml_ndx = Indexer.index(toml_map) | |
Map.put(toml_map, :index, toml_ndx) | |
end | |
end | |
========================================================= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment