Games: Supervisor Setup
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Games: Supervisor Setup
We’ve seen we can initialize a new mix project with a supervisor using the --sup flag when we create a mix project.
Now, you’re going to add a supervisor to your existing Games project.
Configure Games.Application
Supervised Mix projects include an Application module.
> ### Application > > A module for working with applications and defining application callbacks. > Applications are the idiomatic way to package software in Erlang/OTP. To get the idea, they are similar to the “library” concept common in other programming languages, but with some additional characteristics. > An application is a component implementing some specific functionality, with a standardized directory structure, configuration, and life cycle. Applications are loaded, started, and stopped. Each application also has its own environment, which provides a unified API for configuring each application. > > HexDocs: Application
Create a lib/games/application.ex file with the following content. This will be the callback function that starts your application supervisor.
We’ve included an IO.puts/2 statement to demonstrate that the supervisor does start. This is not strictly necessary but will allow you to test that you have configured this correctly later.
defmodule Games.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
@impl true
def start(_type, _args) do
IO.puts("Starting Games.Application")
children = []
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Games.Supervisor]
Supervisor.start_link(children, opts)
end
end
Configure Games.MixProject
In lib/mix.exs configure the application callback module using the :mod option in the Games.MixProject.application/1 function. This tells your mix project to run the Games.Application.start/2 function you just defined.
def application do
[
extra_applications: [:logger],
mod: {Games.Application, []}
]
end
Test your application from the command line by running mix run. You should see your IO.puts/2 message confirming the application does start the supervisor.
$ mix run
Compiling 1 file (.ex)
Starting Games.Application
The same will print if you start the application in the IEx shell.
$ iex -S mix
...
Starting Games.Application
...
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "games_supervisor_setup_reading"
"exercises" -> "games_supervisor_setup_exercise"
end
progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()
default = Map.get(existing_progress, save_name, false)
form =
Kino.Control.form(
[
completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
],
report_changes: true
)
Task.async(fn ->
for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
File.write!(
progress_path,
Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
)
end
end)
form
Commit Your Progress
Run the following in your command line from the curriculum folder to track and save your progress in a Git commit.
Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.
$ git checkout -b games-supervisor-setup-exercise
$ git add .
$ git commit -m "finish games supervisor setup exercise"
$ git push origin games-supervisor-setup-exercise
Create a pull request from your games-supervisor-setup-exercise branch to your solutions branch.
Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.
DockYard Academy Students Only:
Notify your teacher by including @BrooklinJazz in your PR description to get feedback.
You (or your teacher) may merge your PR into your solutions branch after review.
If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.
Up Next
| Previous | Next |
|---|---|
| Supervisors and GenServers | Games: Supervised Score Tracker |