mirror of
https://codeberg.org/JasterV/intisync.ex.git
synced 2026-04-26 18:10:07 +00:00
develop a LiveViewMonitor to monitor LiveView shutdowns
This commit is contained in:
parent
4fea03a6e6
commit
e60e1be337
2 changed files with 32 additions and 3 deletions
|
|
@ -11,9 +11,7 @@ defmodule Intisync.Application do
|
|||
IntisyncWeb.Telemetry,
|
||||
{DNSCluster, query: Application.get_env(:intisync, :dns_cluster_query) || :ignore},
|
||||
{Phoenix.PubSub, name: Intisync.PubSub},
|
||||
# Start a worker by calling: Intisync.Worker.start_link(arg)
|
||||
# {Intisync.Worker, arg},
|
||||
# Start to serve requests, typically the last entry
|
||||
{IntisyncWeb.LiveViewMonitor, %{}},
|
||||
IntisyncWeb.Endpoint
|
||||
]
|
||||
|
||||
|
|
|
|||
31
lib/intisync_web/channels/live_view_monitor.ex
Normal file
31
lib/intisync_web/channels/live_view_monitor.ex
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
defmodule IntisyncWeb.LiveViewMonitor do
|
||||
use GenServer
|
||||
|
||||
def start_link(init_arg) do
|
||||
GenServer.start_link(__MODULE__, init_arg, name: {:global, __MODULE__})
|
||||
end
|
||||
|
||||
def init(_) do
|
||||
{:ok, %{views: %{}}}
|
||||
end
|
||||
|
||||
def monitor(pid, view_module, meta) do
|
||||
server_pid = GenServer.whereis({:global, __MODULE__})
|
||||
GenServer.call(server_pid, {:monitor, pid, view_module, meta})
|
||||
end
|
||||
|
||||
def handle_call({:monitor, pid, view_module, meta}, _, %{views: views} = state) do
|
||||
mref = Process.monitor(pid)
|
||||
{:reply, :ok, %{state | views: Map.put(views, pid, {view_module, meta, mref})}}
|
||||
end
|
||||
|
||||
def handle_info({:DOWN, _ref, :process, pid, reason}, state) do
|
||||
{{module, meta, mref}, new_views} = Map.pop(state.views, pid)
|
||||
|
||||
Task.start(fn -> module.unmount(reason, meta) end)
|
||||
|
||||
Process.demonitor(mref)
|
||||
|
||||
{:noreply, %{state | views: new_views}}
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue