Starting and Stopping Child Processes

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, under the hood it will link to them and trap exits so that it can receive ordinary messages if any of the games should crash.

Let’s start by adding a public start_game/1 function in the GameSupervisor module to start games with. Once we have the supervisor starting the processes, it will take care of the rest for us.

 def​ start_game(name), ​do​:
  Supervisor.start_child(__MODULE__, [name])

Here’s what happens when we call start_game/1:

  • __MODULE__ here evaluates to GameSupervisor, which is the local ...

Get Functional Web Development with Elixir, OTP, and Phoenix now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.