Deployment and lifecycle management for Nix
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

server/agent: initial presence

+26 -11
+2 -1
lib/sower/application.ex
··· 13 13 Sower.Repo, 14 14 Sower.Forge.Oauth, 15 15 Sower.Forge.WebhookStorage, 16 + {Finch, name: Sower.Finch}, 16 17 {Phoenix.PubSub, name: Sower.PubSub}, 17 - {Finch, name: Sower.Finch}, 18 + SowerWeb.Presence, 18 19 SowerWeb.Endpoint, 19 20 :systemd.ready() 20 21 ]
+19 -10
lib/sower_web/agent_channel.ex
··· 3 3 use Phoenix.Channel 4 4 5 5 alias Sower.Orchestration 6 + alias SowerWeb.Presence 6 7 require Logger 7 8 8 9 def join("agent:lobby", _message, %{assigns: %{conn_sid: conn_sid}} = socket) do ··· 31 32 nil -> 32 33 {:error, %{reason: "unauthorized"}} 33 34 34 - agent -> 35 - if is_nil(agent.local_sid) do 36 - {:error, %{reason: "unauthorized"}} 37 - end 35 + agent when is_nil(agent.local_sid) -> 36 + {:error, %{reason: "unauthorized"}} 38 37 39 - case agent.local_sid do 40 - ^local_sid -> 41 - {:ok, %{conn_sid: conn_sid}, socket} 38 + agent when agent.local_sid == local_sid -> 39 + send(self(), :track_presence) 40 + {:ok, %{conn_sid: conn_sid}, assign(socket, :agent, agent)} 42 41 43 - _ -> 44 - {:error, %{reason: "unauthorized"}} 45 - end 42 + _ -> 43 + {:error, %{reason: "unauthorized"}} 46 44 end 47 45 end 48 46 ··· 78 76 Logger.error(msg: "Error returning hello", error: error) 79 77 {:reply, {:error, error}, socket} 80 78 end 79 + end 80 + 81 + def handle_info(:track_presence, %Phoenix.Socket{assigns: %{agent: agent}} = socket) do 82 + Logger.debug(msg: "Tracking agent presence", agent_sid: agent.sid) 83 + 84 + {:ok, _} = 85 + Presence.track(self(), "agents", socket.assigns.agent.sid, %{ 86 + online_at: DateTime.utc_now() 87 + }) 88 + 89 + {:noreply, socket} 81 90 end 82 91 83 92 def handle_info(:ping, %Phoenix.Socket{assigns: %{sid: sid}} = socket) do
+5
lib/sower_web/presence.ex
··· 1 + defmodule SowerWeb.Presence do 2 + use Phoenix.Presence, 3 + otp_app: :sower_web, 4 + pubsub_server: Sower.PubSub 5 + end