This commit is contained in:
Rüdiger Diedrich
2022-07-21 18:42:42 +02:00
parent 7035fe5b2d
commit de9afd2907
8 changed files with 191 additions and 24 deletions

View File

@ -8,6 +8,10 @@ defmodule SomethingErlang.Application do
@impl true
def start(_type, _args) do
children = [
{Registry, [name: SomethingErlang.Registry.Grovers,
keys: :unique]},
{DynamicSupervisor, [name: SomethingErlang.Supervisor.Grovers,
strategy: :one_for_one]},
# Start the Ecto repository
SomethingErlang.Repo,
# Start the Telemetry supervisor

View File

@ -0,0 +1,57 @@
defmodule SomethingErlang.Grover do
use GenServer
require Logger
def mount(lv_pid, user, thread_id) do
DynamicSupervisor.start_child(
SomethingErlang.Supervisor.Grovers,
{__MODULE__, [lv_pid, user, thread_id]}
)
end
def get_thread!(lv_pid, thread_id, page_number) do
GenServer.call(via(lv_pid), {:show_thread, thread_id, page_number})
end
def start_link([lv_pid, user, thread_id]) do
GenServer.start_link(
__MODULE__,
[lv_pid, user, thread_id],
name: via(lv_pid))
end
@impl true
def init([pid, user, thread_id]) do
%{bbuserid: userid, bbpassword: userhash} = user
initial_state = %{
lv_pid: pid,
thread_id: thread_id,
page_number: 1,
user: %{id: userid, hash: userhash}
}
Logger.debug "init #{userid} #{thread_id}"
Process.monitor(pid)
{:ok, initial_state}
end
@impl true
def handle_call({:show_thread, thread_id, page_number}, _from, state) do
thread = AwfulApi.parsed_thread(thread_id, page_number, state.user)
{:reply, thread, %{state | page_number: page_number}}
end
@impl true
def handle_info({:DOWN, _ref, :process, _object, reason}, state) do
Logger.debug "received :DOWN from: #{inspect(state.lv_pid)} reason: #{inspect(reason)}"
case reason do
{:shutdown, _} -> {:stop, :normal, state}
_ -> {:noreply, state}
end
|> IO.inspect()
end
defp via(lv_pid),
do: {:via, Registry, {SomethingErlang.Registry.Grovers, lv_pid}}
end