1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
defmodule ExOvh.Supervisor do
@moduledoc ~s"""
Supervisor for the Ovh Configuration
"""
use Supervisor
alias ExOvh.Ovh.Cache
alias ExOvh.Ovh.Defaults, as: OvhDefaults
alias ExOvh.Hubic.Defaults, as: HubicDefaults
alias ExOvh.Ovh.Supervisor, as: OvhSupervisor
alias ExOvh.Hubic.Supervisor, as: HubicSupervisor
require Logger
#####################
# Public
#####################
@doc ~s"""
Starts the OVH and Hubic supervisors.
"""
def start_link(client, config, opts) do
LoggingUtils.log_mod_func_line(__ENV__, :debug)
Supervisor.start_link(__MODULE__, {client, config, opts}, [name: client])
end
#####################
# Callbacks
#####################
def init({client, config, opts}) do
LoggingUtils.log_mod_func_line(__ENV__, :debug)
sup_tree =
case ovh_config(config, client) do
{:error, :config_not_found} ->
Logger.warn(IO. inspect("No ovh config found. OVH supervisor will not be started for client #{client}"))
[]
valid_config ->
[{OvhSupervisor,
{OvhSupervisor, :start_link, [client, valid_config, opts]}, :permanent, 10_000, :supervisor, [OvhSupervisor]}]
end
sup_tree =
case hubic_config(config, client) do
{:error, :config_not_found} ->
LoggingUtils.log_return("No hubic config found. Hubic supervisor will not be started for client #{client}", :warn)
sup_tree
valid_config -> sup_tree ++
[{HubicSupervisor,
{HubicSupervisor, :start_link, [client, valid_config, opts]}, :permanent, 10_000, :supervisor, [HubicSupervisor]}]
end
if sup_tree === [] do
raise "No configuration found for hubic or ovh."
end
supervise(sup_tree, strategy: :one_for_one, max_restarts: 20)
end
@doc """
Returns ovh configuration settings or an error
if the ovh config is not found
## <<TODO>> Add some additional config validation (
to test absence of essential information)
"""
@spec ovh_config(config :: map, client :: atom) :: map | {:error, atom}
def ovh_config(config, client) do
case config[:ovh] do
:nil -> {:error, :config_not_found}
_ -> Map.merge(OvhDefaults.ovh(), client.config()[:ovh])
end
end
@doc """
Returns hubic configuration settings
"""
@spec hubic_config(config :: map, client :: atom) :: map | :nil
def hubic_config(config, client) do
case config[:hubic] do
:nil -> {:error, :config_not_found}
_ -> Map.merge(HubicDefaults.hubic(), client.config()[:hubic])
end
end
end