adding kill button

This commit is contained in:
2026-03-03 14:30:15 -07:00
parent 4ac6c09759
commit 60714f9afd
6 changed files with 86 additions and 6 deletions

View File

@@ -9,6 +9,15 @@ defmodule Backend.Cluster do
def start_link(opts) do def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__) GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end end
def kill_node(node_name) when is_binary(node_name) do
kill_node(String.to_existing_atom(node_name))
end
def kill_node(target) when is_atom(target) do
Logger.warning("Killing node: #{target}")
:rpc.cast(target, :init, :stop, [])
end
@impl true @impl true
def init(_opts) do def init(_opts) do
@@ -62,4 +71,5 @@ defmodule Backend.Cluster do
# Retry connection every 10 seconds # Retry connection every 10 seconds
Process.send_after(self(), :periodic_connect, 10_000) Process.send_after(self(), :periodic_connect, 10_000)
end end
end end

View File

@@ -52,7 +52,6 @@ defmodule Backend.GlobalSingleton do
@impl true @impl true
def handle_info({:startup_args_updated, new_args}, state) do def handle_info({:startup_args_updated, new_args}, state) do
Logger.info("Received updated startup args for #{state.module}: #{inspect(new_args)}")
{:noreply, %{state | startup_args: new_args}} {:noreply, %{state | startup_args: new_args}}
end end

View File

@@ -6,11 +6,25 @@ defmodule BackendWeb.ClusterStatusChannel do
require Logger require Logger
@impl true @impl true
def join("clusterstatus", _params, socket) do def join("cluster_status", _params, socket) do
Logger.info("Client joined clusterstatus channel") Logger.info("Client joined clusterstatus channel")
{:ok, %{status: "connected"}, socket} {:ok, %{status: "connected"}, socket}
end end
@impl true
def handle_in("get_nodes", _payload, socket) do
Logger.info("Client requested node list #{inspect(Node.list())}")
push(socket, "node_list", %{other_nodes: Node.list(), connected_node: node()})
{:noreply, socket}
end
@impl true
def handle_in("kill_node", %{"node" => node_to_kill}, socket) do
Logger.warning("Client requested to kill node: #{node_to_kill}")
Backend.Cluster.kill_node(node_to_kill)
{:noreply, socket}
end
@impl true @impl true
def handle_in(_event, _payload, socket) do def handle_in(_event, _payload, socket) do
{:noreply, socket} {:noreply, socket}

View File

@@ -47,6 +47,8 @@ defmodule BackendWeb.ConnectedUserChannel do
end end
@impl true @impl true
@spec handle_in(<<_::48, _::_*8>>, map(), any()) ::
{:noreply, any()} | {:reply, :ok, Phoenix.Socket.t()}
def handle_in("join_game", %{"name" => name}, socket) do def handle_in("join_game", %{"name" => name}, socket) do
Logger.info("Player '#{name}' joining game on #{node()}") Logger.info("Player '#{name}' joining game on #{node()}")

View File

@@ -2,10 +2,9 @@ defmodule BackendWeb.UserSocket do
use Phoenix.Socket use Phoenix.Socket
require Logger require Logger
## Channels ## Channels
channel("user:*", BackendWeb.ConnectedUserChannel) channel("user:*", BackendWeb.ConnectedUserChannel)
channel("clusterstatus", BackendWeb.ClusterStatusChannel) channel("cluster_status", BackendWeb.ClusterStatusChannel)
@impl true @impl true
def connect(%{"user_name" => user_name}, socket, _connect_info) do def connect(%{"user_name" => user_name}, socket, _connect_info) do

View File

@@ -1,16 +1,21 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useWebSocketContext } from "../contexts/useWebSocketContext"; import { useWebSocketContext } from "../contexts/useWebSocketContext";
import type { Channel } from "phoenix";
export const ClusterStatus = () => { export const ClusterStatus = () => {
const { socket, isConnected } = useWebSocketContext(); const { socket, isConnected } = useWebSocketContext();
const [channelStatus, setChannelStatus] = useState<string>("waiting"); const [channelStatus, setChannelStatus] = useState<string>("waiting");
const [channel, setChannel] = useState<Channel | undefined>();
const [clusterStatus, setClusterStatus] = useState<
{ otherNodes: string[]; connectedNode: string } | undefined
>();
useEffect(() => { useEffect(() => {
if (!socket || !isConnected) { if (!socket || !isConnected) {
return; return;
} }
const channelName = "clusterstatus"; const channelName = "cluster_status";
console.log(`Joining channel: ${channelName}`); console.log(`Joining channel: ${channelName}`);
const newChannel = socket.channel(channelName, {}); const newChannel = socket.channel(channelName, {});
@@ -18,6 +23,8 @@ export const ClusterStatus = () => {
.join() .join()
.receive("ok", () => { .receive("ok", () => {
setChannelStatus("connected"); setChannelStatus("connected");
setChannel(newChannel);
newChannel.push("get_nodes", {});
}) })
.receive("error", (resp: unknown) => { .receive("error", (resp: unknown) => {
console.log(`Failed to join channel ${channelName}:`, resp); console.log(`Failed to join channel ${channelName}:`, resp);
@@ -27,17 +34,66 @@ export const ClusterStatus = () => {
setChannelStatus("timeout"); setChannelStatus("timeout");
}); });
newChannel.on(
"node_list",
(payload: { other_nodes: string[]; connected_node: string }) => {
console.log("Received node list:", payload.other_nodes);
setClusterStatus({
otherNodes: payload.other_nodes,
connectedNode: payload.connected_node,
});
},
);
return () => { return () => {
console.log(`Leaving channel: ${channelName}`); console.log(`Leaving channel: ${channelName}`);
newChannel.leave(); newChannel.leave();
setChannel(undefined);
setChannelStatus("waiting"); setChannelStatus("waiting");
}; };
}, [socket, isConnected]); }, [socket, isConnected]);
const allNodes = clusterStatus
? [...clusterStatus.otherNodes, clusterStatus.connectedNode].sort()
: [];
return ( return (
<div> <div>
<div>ClusterStatus</div> <div>ClusterStatus</div>
<div>Channel: {channelStatus}</div> <div>Channel: {channelStatus}</div>
<div>
{allNodes.length === 0 ? (
<span className="text-navy-300">No nodes available</span>
) : (
<>
{allNodes.map((node) => {
const isConnected = node === clusterStatus?.connectedNode;
return (
<div
key={node}
className={
isConnected
? "text-navy-100 font-semibold"
: "text-navy-400"
}
>
{node}
{isConnected && (
<span className="ml-2 text-xs text-navy-300">(you)</span>
)}
<button
onClick={() => {
channel?.push("kill_node", { node });
}}
>
kill
</button>
</div>
);
})}
</>
)}
</div>
</div> </div>
); );
} };