Games: Supervisor Setup
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Home Report An Issue Supervised StackGames: Supervised Score TrackerGames: 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
...
Commit Your Progress
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish Games: Supervisor Setup exercise"
$ git push
We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.