From eaedd08581d47567504225dc32e2c7bdac58e877 Mon Sep 17 00:00:00 2001 From: Berni Date: Wed, 7 Oct 2020 16:30:49 +0200 Subject: [PATCH] fix(*): Introduce application to have a supervisor as the first process Closes #68 --- lib/cloudex/application.ex | 12 ++++++++++++ lib/cloudex/settings.ex | 23 ++++++++++++----------- mix.exs | 2 +- 3 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 lib/cloudex/application.ex diff --git a/lib/cloudex/application.ex b/lib/cloudex/application.ex new file mode 100644 index 0000000..9659a22 --- /dev/null +++ b/lib/cloudex/application.ex @@ -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 diff --git a/lib/cloudex/settings.ex b/lib/cloudex/settings.ex index 26c5f2d..665597b 100644 --- a/lib/cloudex/settings.ex +++ b/lib/cloudex/settings.ex @@ -9,6 +9,7 @@ defmodule Cloudex.Settings do @doc """ Required by GenServer """ + @impl true def init(args) do {:ok, args} end @@ -16,11 +17,11 @@ defmodule Cloudex.Settings do @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 """ @@ -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} @@ -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 """ diff --git a/mix.exs b/mix.exs index bed5ada..f3fada4 100644 --- a/mix.exs +++ b/mix.exs @@ -43,7 +43,7 @@ defmodule Cloudex.Mixfile do def application do [ extra_applications: [:logger], - mod: {Cloudex.Settings, []} + mod: {Cloudex.Application, []} ] end