arcology-phx/arcology_app.org

3.6 KiB
Raw Permalink Blame History

Arcology Elixir Backend Logic

Setting up the Arcology elixir module

This is a module, the submodules are where the interesting things happen. The module doc says what it does:

Arcology keeps the contexts that define your domain
and business logic.

Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
defmodule Arcology do
  @moduledoc """
  <<arcology.moduledoc>>
  """
end

Arcology.Application Elixir Application Startup

defmodule Arcology.Application do
  @moduledoc false

  use Application

  <<start_2>>

  <<config_change_3>>
end

So Arcology.Application.start/2 is the entrypoint of the Phoenix OTP application, it starts up a bunch of other Supervised1 Elixir processes and makes sure they keep running.

Arcology.Application.start/2

We start the Ecto repository roam:Arcology.Repo:

Arcology.Repo

We start the PubSub messaging system:

{Phoenix.PubSub, name: Arcology.PubSub}

We start Finch, an HTTP client:

{Finch, name: Arcology.Finch}

We start the roam:ArcologyWeb.Endpoint which handles HTTP requests from the web.

ArcologyWeb.Endpoint

We will also start the ArcologyWeb.Telemetry server provided by Phoenix [n.b. this code block isn't directly included so that i can generate decent list-syntax without relying on metaprogramming or not being able to detangle this to my heart's content]

This supervisor is configured to only restart a single process when the process dies; for other applications you may want it to restart all children, or to only restart "younger" processes which may rely on state within the dead process. This is pretty simple for now. Check out the docs if you care.

@impl true
def start(_type, _args) do
  children = [
       ArcologyWeb.Telemetry
     , <<app-children>>
  ]

  opts = [strategy: :one_for_one, name: Arcology.Supervisor]
  Supervisor.start_link(children, opts)
end

This config_change function is a stub that sends new configuration to the ArcologyWeb.Endpoint process when the appication is upgraded. I'll probably just restart the thing in place, but for highly-available OTP applications this sort of stuff is bread-and-butter.

@impl true
def config_change(changed, _new, removed) do
  ArcologyWeb.Endpoint.config_change(changed, removed)
  :ok
end

Arcology.Mailer

This is a stub for now maybe i'll send mails for new posts in the future but shrug:

defmodule Arcology.Mailer do
  use Swoosh.Mailer, otp_app: :arcology
end

Footnotes


1

Elixir is built on Erlang, and Erlang has a process-based Actor model, you can send messages to processes whose IDs you have. The Supervisor is responsible for starting and managing the lifecycle of these processes. See the OTP Design Principles doc.