Skip to content

Instantly share code, notes, and snippets.

@RichMorin
Created December 8, 2018 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichMorin/6fca3429b0e3754d21b92e11f6818559 to your computer and use it in GitHub Desktop.
Save RichMorin/6fca3429b0e3754d21b92e11f6818559 to your computer and use it in GitHub Desktop.
Re: non-working Agent reload code
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 my current setup:
=========================================================
defmodule PhxHttp.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
def start(_type, _args) do
# List all child processes to be supervised
children = [
# Start the endpoint when the application starts
PhxHttpWeb.Endpoint
# Starts a worker by calling: PhxHttp.Worker.start_link(arg)
# {PhxHttp.Worker, arg},
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: PhxHttp.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
PhxHttpWeb.Endpoint.config_change(changed, removed)
:ok
end
end
=========================================================
defmodule RefData.Application do
use Application
@doc """
Load the TOML file tree, then fold in the index.
"""
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
worker(RefData.Server, []),
]
options = [
name: RefData.Supervisor,
strategy: :one_for_one,
]
Supervisor.start_link(children, options)
end
end
=========================================================
@voughtdq
Copy link

voughtdq commented Dec 10, 2018

While it's a valid strategy to have two different applications to separate these concerns, go ahead and just use one Application for now and treat your RefData.Server as a kind of library:

defmodule PhxHttp.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    # List all child processes to be supervised
    children = [
      # Start the endpoint when the application starts
      PhxHttpWeb.Endpoint,
      %{id: RefData.Server, start: {RefData.Server, :start_link, []}}, # <-- see notes below about child_spec
      # Starts a worker by calling: PhxHttp.Worker.start_link(arg)
      # {PhxHttp.Worker, arg},
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: PhxHttp.Supervisor]
    Supervisor.start_link(children, opts)
  end

  # Tell Phoenix to update the endpoint configuration
  # whenever the application is updated.
  def config_change(changed, _new, removed) do
    PhxHttpWeb.Endpoint.config_change(changed, removed)
    :ok
  end
end

I've added a child_spec so that you can explicitly have an ID for RefData.Server, which is necessary to look it up in the supervisor. An ID can actually be any valid atom (or maybe even any valid term). Check out the documentation for Supervisor for more information on that.

Now at this point, you should be able to call this:

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

without any problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment