Refactor last_fm configuration
Move all configuration outside of the LastFm module, so that it's not polluted with references to the parent music_library application.
This commit is contained in:
@@ -5,14 +5,14 @@ defmodule LastFm.Refresh do
|
||||
|
||||
alias LastFm.Feed
|
||||
|
||||
@refresh_interval System.convert_time_unit(60, :second, :millisecond)
|
||||
|
||||
def start_link(config) do
|
||||
GenServer.start_link(__MODULE__, config, name: __MODULE__)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(config) do
|
||||
config = Map.new(config)
|
||||
|
||||
if enabled?(config) do
|
||||
{:ok, config, {:continue, :refresh}}
|
||||
else
|
||||
@@ -25,12 +25,12 @@ defmodule LastFm.Refresh do
|
||||
case config.api.get_recent_tracks(config.user, config.api_key) do
|
||||
{:ok, tracks} ->
|
||||
Feed.update(tracks)
|
||||
Process.send_after(self(), :refresh, @refresh_interval)
|
||||
Process.send_after(self(), :refresh, config.refresh_interval)
|
||||
{:noreply, config}
|
||||
|
||||
{:error, _reason} ->
|
||||
# TODO: think about failure scenario - error is logged at the API level
|
||||
Process.send_after(self(), :refresh, @refresh_interval)
|
||||
Process.send_after(self(), :refresh, config.refresh_interval)
|
||||
{:noreply, config}
|
||||
end
|
||||
end
|
||||
@@ -40,12 +40,12 @@ defmodule LastFm.Refresh do
|
||||
case config.api.get_recent_tracks(config.user, config.api_key) do
|
||||
{:ok, tracks} ->
|
||||
Feed.update(tracks)
|
||||
Process.send_after(self(), :refresh, @refresh_interval)
|
||||
Process.send_after(self(), :refresh, config.refresh_interval)
|
||||
{:noreply, config}
|
||||
|
||||
{:error, _reason} ->
|
||||
# TODO: think about failure scenario - error is logged at the API level
|
||||
Process.send_after(self(), :refresh, @refresh_interval)
|
||||
Process.send_after(self(), :refresh, config.refresh_interval)
|
||||
{:noreply, config}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,28 +1,19 @@
|
||||
defmodule LastFm.Supervisor do
|
||||
use Supervisor
|
||||
|
||||
def start_link(init_arg) do
|
||||
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
|
||||
def start_link(config) do
|
||||
Supervisor.start_link(__MODULE__, config, name: __MODULE__)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(_opts) do
|
||||
def init(config) do
|
||||
:ok = LastFm.Feed.create_table!()
|
||||
|
||||
children = [
|
||||
{Phoenix.PubSub, name: LastFm.PubSub},
|
||||
{LastFm.Refresh, %{api: api(), user: "cloud8421", api_key: api_key()}}
|
||||
{LastFm.Refresh, config}
|
||||
]
|
||||
|
||||
Supervisor.init(children, strategy: :one_for_one)
|
||||
end
|
||||
|
||||
defp api_key do
|
||||
Application.get_env(:music_library, LastFm, [])
|
||||
|> Keyword.get(:api_key)
|
||||
end
|
||||
|
||||
defp api do
|
||||
Application.get_env(:music_library, :last_fm, LastFm.APIImpl)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -15,7 +15,7 @@ defmodule MusicLibrary.Application do
|
||||
repos: Application.fetch_env!(:music_library, :ecto_repos), skip: skip_migrations?()},
|
||||
{DNSCluster, query: Application.get_env(:music_library, :dns_cluster_query) || :ignore},
|
||||
{Phoenix.PubSub, name: MusicLibrary.PubSub},
|
||||
LastFm.Supervisor,
|
||||
{LastFm.Supervisor, last_fm_config()},
|
||||
# Start a worker by calling: MusicLibrary.Worker.start_link(arg)
|
||||
# {MusicLibrary.Worker, arg},
|
||||
# Start to serve requests, typically the last entry
|
||||
@@ -40,4 +40,8 @@ defmodule MusicLibrary.Application do
|
||||
# By default, sqlite migrations are run when using a release
|
||||
System.get_env("RELEASE_NAME") != nil
|
||||
end
|
||||
|
||||
defp last_fm_config do
|
||||
Application.get_env(:music_library, LastFm)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user