Start and Stop Child Processes
Explore how to use the GameSupervisor module to start and stop child processes safely in Elixir. Understand how supervision ensures game processes are managed, how to start new games, and stop them programmatically to avoid idle processes using Supervisor functions. This lesson helps you grasp supervising child processes for fault tolerance and resource management in your back-end applications.
We'll cover the following...
We want each new game to be supervised. That means that we need the GameSupervisor process to start them. As the GameSupervisor process starts new game processes, it will link to them and trap exits so it can receive ordinary messages if any game should crash.
Changes to the GameSupervisor module
Let’s start by adding a public start_game/1 function in the GameSupervisor module to the start games. Once we have the supervisor starting the processes, it will take care of the rest:
def start_game(name), do:
Supervisor.start_child(__MODULE__, [name])
Here’s what happens when we call start_game/1:
__MODULE__evaluates toGameSupervisor, which is the local name we