Elixir Application Cookbook

sawcce a.k.a Alex=

Written on: 2/15/2025 Last edited: 2/15/2025

1

Applications

Official documentation

Mix Config

1# In mix.exs
2def application do
3  [
4    mod: {YourApp, []},
5    extra_applications: [:logger]
6  ]
7end

Main file

Here's a snippet that uses a classic supervisor, you may also use a dynamic supervisor that allows you to spawn and terminate child processes at runtime.

1defmodule Hi do
2  use Application
3
4  def start(_type, _args) do
5    children = []
6
7    # https://hexdocs.pm/elixir/1.18.2/Supervisor.html#start_link/2
8    Supervisor.start_link(children, strategy: :one_for_one)
9  end
10end

Supervisors

Child specification

Child specification documentation

Various specs: :id, :start, :restart, :shutdown, :type, :modules, :significant

Shorthand: {Module, init_args}

Strategies

Strategies documentation

  • :one_for_one - if a child process terminates, only that process is restarted.

  • :one_for_all - if a child process terminates, all other child processes are terminated and then all child processes (including the terminated one) are restarted.

  • :rest_for_one - if a child process terminates, the terminated child process and the rest of the children started after it, are terminated and restarted.

Comments (0)

You need to login to comment!