Hello everyone,
I have trouble understanding the state of a closing process.
Here is the context. I catch the terminate function when leaving a channel, to leave a game GenServer. If the number of player get to 0, I close the game.
I would like to broadcast the state of the game if it is alive and not closing.
Here is the leaving function of the GenServer
def handle_cast({:leave, player}, state) do
players = List.delete(state.players, player)
new_state = %{state | players: players, status: :idle}
case Enum.count(players) do
0 -> {:stop, :normal, new_state}
_ -> {:noreply, new_state}
end
end
Here is the terminate function of the game channel
def terminate(reason, socket) do
Logger.debug("> leave #{inspect reason}")
game = Registry.game_process(socket.assigns.game)
username = socket.assigns.current_user.username
game |> Game.leave(username)
:timer.sleep 1000
if Process.alive?(game) do
broadcast! socket, "update_game_state", Game.get_state(game)
end
:ok
end
The question, if I remove the sleep, game process is seen as alive, but is in fact closing, and call to Game.get_state(game) returns an error.
How can I catch the real status of the process?
Thanks for taking time.