2020-06-08 23:37:11 +02:00
|
|
|
defmodule Openpod.Boundary.ArchiveServer do
|
2020-06-07 21:37:41 +02:00
|
|
|
@moduledoc false
|
|
|
|
|
|
|
|
use GenServer, restart: :temporary
|
|
|
|
require Logger
|
2020-06-08 23:37:11 +02:00
|
|
|
alias Openpod
|
|
|
|
alias Openpod.Provider.Archive
|
2020-06-07 21:37:41 +02:00
|
|
|
|
|
|
|
@registry :podcast_registry
|
|
|
|
@archive_supervisor :archive_supervisor
|
|
|
|
|
|
|
|
##########
|
|
|
|
### API
|
|
|
|
|
|
|
|
def start_link(identifier) do
|
|
|
|
GenServer.start_link(__MODULE__, identifier, name: via(identifier))
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_or_fetch(identifier) do
|
|
|
|
case DynamicSupervisor.start_child(@archive_supervisor, {__MODULE__, identifier}) do
|
|
|
|
{:ok, pid} -> {:ok, pid}
|
|
|
|
{:error, {:already_started, pid}} -> {:ok, pid}
|
|
|
|
error -> error
|
|
|
|
end
|
|
|
|
# %{id: Worker, start: { Worker, :start_link, [child_name]}, restart: :transient})
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_feed(identifier) do
|
|
|
|
cache_or_fetch(identifier)
|
|
|
|
GenServer.call(via(identifier), :get_feed)
|
|
|
|
end
|
|
|
|
|
2020-06-10 22:56:29 +02:00
|
|
|
def reload(pid) when is_pid(pid) do
|
|
|
|
Logger.debug "starting reload..."
|
|
|
|
GenServer.cast(pid, :reload)
|
|
|
|
end
|
|
|
|
|
2020-06-07 21:37:41 +02:00
|
|
|
def reload(identifier) do
|
2020-06-07 23:10:40 +02:00
|
|
|
cache_or_fetch(identifier)
|
2020-06-07 21:37:41 +02:00
|
|
|
GenServer.call(via(identifier), :reload)
|
|
|
|
end
|
|
|
|
|
2020-06-10 22:56:29 +02:00
|
|
|
def count(pid) when is_pid(pid) do
|
|
|
|
GenServer.call(pid, :count)
|
|
|
|
end
|
|
|
|
|
|
|
|
def count(identifier) when is_binary(identifier) do
|
|
|
|
GenServer.call(via(identifier), :count)
|
|
|
|
end
|
|
|
|
|
2020-06-07 21:37:41 +02:00
|
|
|
##########
|
|
|
|
### SERVER
|
|
|
|
|
|
|
|
def init(identifier) do
|
2020-06-10 22:56:29 +02:00
|
|
|
{:ok, %{count: 1, identifier: identifier, feed_data: fetch_feed_data(identifier)}}
|
2020-06-07 21:37:41 +02:00
|
|
|
end
|
|
|
|
|
2020-06-10 22:56:29 +02:00
|
|
|
def handle_call(:get_feed, _from, feed = %{count: count, feed_data: feed_data}) do
|
2020-06-07 21:37:41 +02:00
|
|
|
Logger.debug "retrieve cached feed data for #{feed[:identifier]}..."
|
2020-06-10 22:56:29 +02:00
|
|
|
{:reply, feed_data, %{feed | count: count + 1}, Application.fetch_env!(:openpod, :cache_duration)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_call(:reload, _from, feed = %{count: count, identifier: identifier}) do
|
|
|
|
Logger.debug "discard cached feed data for #{identifier}..."
|
|
|
|
feed_data = fetch_feed_data(identifier)
|
|
|
|
{:reply, feed_data, %{feed | count: count + 1, feed_data: feed_data}}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_call(:count, _from, feed = %{identifier: identifier, count: count}) do
|
|
|
|
{:reply, %{identifier: identifier, count: count}, feed}
|
2020-06-07 21:37:41 +02:00
|
|
|
end
|
|
|
|
|
2020-06-10 22:56:29 +02:00
|
|
|
def handle_cast(:reload, feed = %{identifier: identifier}) do
|
|
|
|
Logger.debug "discard cached feed data for #{identifier}..."
|
|
|
|
{:noreply, %{feed | feed_data: fetch_feed_data(identifier)}}
|
2020-06-07 21:37:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_info(:timeout, %{identifier: identifier}) do
|
|
|
|
Logger.debug "shutting down #{identifier}..."
|
|
|
|
{:stop, :normal, []}
|
|
|
|
end
|
|
|
|
|
|
|
|
##########
|
|
|
|
### PRIVATES
|
|
|
|
|
|
|
|
defp fetch_feed_data(identifier) do
|
|
|
|
Logger.debug "fetching archive metadata for #{identifier}..."
|
|
|
|
Archive.Parser.by_identifier(identifier)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp via(identifier) do
|
|
|
|
{:via, Registry, {@registry, identifier}}
|
|
|
|
end
|
|
|
|
end
|