Skip to content

Commit

Permalink
fix(*): Introduce application to have a supervisor as the first process
Browse files Browse the repository at this point in the history
Closes smeevil#68
  • Loading branch information
Berni committed Oct 7, 2020
1 parent 547f78e commit eaedd08
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
12 changes: 12 additions & 0 deletions lib/cloudex/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Cloudex.Application do
@moduledoc false
use Application

@impl true
def start(_type, args \\ []) do
children = [{Cloudex.Settings, args}]

opts = [strategy: :one_for_one, name: MessagePipeline.Supervisor]
Supervisor.start_link(children, opts)
end
end
23 changes: 12 additions & 11 deletions lib/cloudex/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ defmodule Cloudex.Settings do
@doc """
Required by GenServer
"""
@impl true
def init(args) do
{:ok, args}
end

@doc """
Called by the supervisor, this will use settings defined in config.exs or ENV vars
"""
def start(:normal, []) do
def start_link(_opts) do
[:api_key, :secret, :cloud_name]
|> get_app_config
|> get_app_config()
|> Cloudex.EnvOptions.merge_missing_settings()
|> start
|> start()
end

@doc """
Expand All @@ -29,19 +30,16 @@ defmodule Cloudex.Settings do
def start(%{} = settings) do
settings
|> Map.merge(settings)
|> validate
|> do_start
|> validate()
|> do_start()
end

def do_start({:error, :placeholder_settings}),
defp do_start({:error, :placeholder_settings}),
do: {:error, placeholder_settings_error_message()}

def do_start({:error, _} = error), do: error
defp do_start({:error, _} = error), do: error

@doc """
Helper function to start or restart the GenServer when already started with given settings
"""
def do_start({:ok, settings}) do
defp do_start({:ok, settings}) do
case GenServer.start(__MODULE__, settings, name: :cloudex) do
{:ok, pid} ->
{:ok, pid}
Expand All @@ -57,10 +55,13 @@ defmodule Cloudex.Settings do
"""
def stop, do: GenServer.call(:cloudex, :stop)

@impl true
def handle_call(:stop, _caller, state), do: {:stop, :normal, :ok, state}

@impl true
def handle_call(:settings, _caller, state), do: {:reply, state, state}

@impl true
def terminate(_reason, _state), do: :ok

@doc """
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule Cloudex.Mixfile do
def application do
[
extra_applications: [:logger],
mod: {Cloudex.Settings, []}
mod: {Cloudex.Application, []}
]
end

Expand Down

0 comments on commit eaedd08

Please sign in to comment.