📎 Webclip
Supervising multiple GenServer processes
The article explains a refactoring in the Trello tribute series: instead of keeping all connected board members in one GenServer, it creates one process per board and puts them under a supervisor. That way, if one process dies or restarts, the state of the other boards is not lost.
Reading notes#
- A single
PhoenixTrello.BoardChannel.Monitorkept a map of connected users by board, but a restart would lose that state. - The proposed fix is to split the state into multiple processes, one for each board.
- A module-based
Supervisorstarts and restarts the monitor processes. - The supervisor uses
restart: :transientso a child restarts only after an abnormal termination. - The setup uses
:simple_one_for_oneso only the child process that fails is restarted. PhoenixTrello.BoardChannel.Monitorgets a dynamic name fromboard_idthroughref(board_id).create(board_id)checks whether a process already exists for that board before callingSupervisor.start_child/2.user_joined/2,users_in_board/1, anduser_left/2call the correct process throughtry_call/2.- The application supervision tree is updated to include the new board-channel supervisor instead of the old monitor worker.
