Skip to content

Commit

Permalink
This was an experiment before running across this PR
Browse files Browse the repository at this point in the history
derekkraan/horde#271

Long story short, its not a good idea to nest Horde Supervisors in this way, probably a better approach
is have the "sub" processes attached to the main HordeSupervisor (via the project Module) and maybe
monitor that process in the "parent". Or just rethink needing them to be parent/child processes.
  • Loading branch information
callahat committed Jan 5, 2025
1 parent b60665c commit 2623b31
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
53 changes: 42 additions & 11 deletions horde_background_job/lib/horde_background_job/database_cleaner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,61 @@ defmodule HordeBackgroundJob.DatabaseCleaner do
timeout = Keyword.get(opts, :timeout, :timer.seconds(2))
count = Keyword.get(opts, :count, 0)

GenServer.start_link(__MODULE__, {timeout, name, count}, name: name)
case GenServer.start_link(__MODULE__, {timeout, name, count}, name: name) do
{:ok, pid} ->
nil # nothing to do

{:error, {:already_started, pid}} ->
# also nothing to do but log it - mostly interested if a name is being set incorrectly
log_msg("already started at #{inspect(pid)} for #{ name }")
end
end

@impl GenServer
def init({timeout, name, count}) do
Process.set_label(_name(name))
polisher_name = "#{_name(name)}Polisher"
log_msg("starting")
log_msg("polisher name")
log_msg(inspect name)
log_msg(inspect polisher_name)

schedule(timeout)

log_msg("starting horde super for the polisher - #{ inspect _name(name) }")
{:ok, polish_supervisor} = Horde.DynamicSupervisor.start_link([strategy: :one_for_one, name: :"#{polisher_name}"])
log_msg("should have started polish supervisor - #{ inspect polish_supervisor }")

child_spec = %{
id: polisher_name,
start: {HordeBackgroundJob.DatabasePolisher, :start_link, [[name: polisher_name, timeout: timeout]]},
type: :worker,
restart: :temporary,
shutdown: 500,
}
polish_supervisor = \
case Horde.DynamicSupervisor.start_link(strategy: :one_for_one, members: :auto, name: :"#{polisher_name}") do
{:ok, pid} ->
log_msg("success #{ inspect pid }")
pid

{:error, {:already_started, pid}} ->
log_msg("already started polisher at #{inspect(pid)} for #{ polisher_name }")
pid

something_else ->
log_msg("uh oh - #{ inspect something_else }")
nil
end

log_msg("should have started polish supervisor - #{ inspect polish_supervisor }")

{:ok, _pid} = Horde.DynamicSupervisor.start_child(polish_supervisor, child_spec)
# a random number of polishers
Enum.map(1..:rand.uniform(4), fn(i) ->
polisher_name_x = "#{polisher_name}#{i}"

child_spec = %{
id: polisher_name_x,
start: {HordeBackgroundJob.DatabasePolisher, :start_link, [[name: polisher_name_x, timeout: timeout]]},
type: :worker,
restart: :temporary,
shutdown: 500,
}

# keeps returning an :error tuple, but seems the process starts anyway :/
IO.inspect Horde.DynamicSupervisor.start_child(:"#{polisher_name}", child_spec)
end)

{:ok, {timeout, name, count}}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ defmodule HordeBackgroundJob.DatabasePolisher do

log_msg("start_link polisher #{ inspect name}")

{:ok, _pid} = GenServer.start_link(__MODULE__, {timeout, name}, name: via_tuple(name))
case GenServer.start_link(__MODULE__, {timeout, name}, name: via_tuple(name)) do
{:ok, pid} ->
nil # nothing to do

{:error, {:already_started, pid}} ->
# also nothing to do but log it - mostly interested if a name is being set incorrectly
log_msg("already started at #{inspect(pid)} for #{ inspect(via_tuple(name)) }")
end
end

@impl GenServer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ defmodule HordeBackgroundJob.HordeSupervisor do
Enum.map([Node.self() | Node.list()], &{__MODULE__, &1})
end

# defp via_tuple(name) do
# {:via, Horde.Registry, {HordeRegistry, name}}
# end
end

0 comments on commit 2623b31

Please sign in to comment.