Compare commits
3 Commits
85a83d30df
...
87ea1f5624
Author | SHA1 | Date |
---|---|---|
Shadowfacts | 87ea1f5624 | |
Shadowfacts | a7a296b342 | |
Shadowfacts | bbc729b5ca |
|
@ -8,6 +8,7 @@ defmodule Frenzy.Network do
|
|||
|
||||
plug Tesla.Middleware.Logger, log_level: &log_level/1
|
||||
plug Tesla.Middleware.FollowRedirects
|
||||
plug Tesla.Middleware.Compression
|
||||
|
||||
# can't use JSON middleware currently, because feed_parser expects to parse the raw body data itself
|
||||
# plug Tesla.Middleware.JSON
|
||||
|
|
|
@ -24,6 +24,11 @@ defmodule Frenzy.UpdateFeeds do
|
|||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_cast({:update_feed, feed_id, retry_count}, state) do
|
||||
update_feed(Repo.get(Feed, feed_id), retry_count)
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_info(:update_feeds, state) do
|
||||
update_feeds()
|
||||
schedule_update()
|
||||
|
@ -103,22 +108,22 @@ defmodule Frenzy.UpdateFeeds do
|
|||
Logger.info("Converted #{count} read items to tombstones")
|
||||
end
|
||||
|
||||
defp update_feed(feed) do
|
||||
defp update_feed(feed, retry_count \\ 0) do
|
||||
Logger.debug("Updating #{feed.feed_url}")
|
||||
|
||||
case URI.parse(feed.feed_url) do
|
||||
%URI{scheme: "gemini"} = uri ->
|
||||
update_feed_gemini(feed, uri)
|
||||
update_feed_gemini(feed, uri, retry_count)
|
||||
|
||||
%URI{scheme: scheme} when scheme in ["http", "https"] ->
|
||||
update_feed_http(feed)
|
||||
update_feed_http(feed, retry_count)
|
||||
|
||||
%URI{scheme: scheme} ->
|
||||
Logger.warn("Unhandled scheme for feed: #{scheme}")
|
||||
end
|
||||
end
|
||||
|
||||
defp update_feed_http(feed) do
|
||||
defp update_feed_http(feed, retry_count) do
|
||||
case Network.http_get(feed.feed_url) do
|
||||
{:ok,
|
||||
%Tesla.Env{
|
||||
|
@ -148,6 +153,13 @@ defmodule Frenzy.UpdateFeeds do
|
|||
end
|
||||
|
||||
{:error, reason} ->
|
||||
if retry_count < 5 do
|
||||
Process.send_after(
|
||||
self(),
|
||||
{:update_feed, feed, retry_count + 1},
|
||||
trunc(:math.pow(4, retry_count)) * 1000
|
||||
)
|
||||
else
|
||||
Logger.error("Couldn't load feed #{feed.feed_url}: #{inspect(reason)}")
|
||||
|
||||
if Frenzy.sentry_enabled?() do
|
||||
|
@ -157,13 +169,21 @@ defmodule Frenzy.UpdateFeeds do
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp update_feed_gemini(feed, feed_uri) do
|
||||
defp update_feed_gemini(feed, feed_uri, retry_count) do
|
||||
case Network.gemini_request(feed_uri) do
|
||||
{:ok, %Gemini.Response{meta: content_type, body: body}} ->
|
||||
do_update_feed(feed, content_type, body)
|
||||
|
||||
{:error, reason} ->
|
||||
if retry_count < 5 do
|
||||
Process.send_after(
|
||||
self(),
|
||||
{:update_feed, feed, retry_count + 1},
|
||||
trunc(:math.pow(4, retry_count)) * 1000
|
||||
)
|
||||
else
|
||||
Logger.error("Couldn't load feed #{feed.feed_url}: #{inspect(reason)}")
|
||||
|
||||
if Frenzy.sentry_enabled?() do
|
||||
|
@ -174,12 +194,16 @@ defmodule Frenzy.UpdateFeeds do
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp do_update_feed(feed, content_type, data) do
|
||||
case FeedParser.parse(data, content_type) do
|
||||
{:ok, rss} ->
|
||||
update_feed_from_rss(feed, rss)
|
||||
|
||||
{:error, :no_data} ->
|
||||
:ok
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("Unable to parse feed at '#{feed.feed_url}': #{inspect(reason)}")
|
||||
|
||||
|
|
Loading…
Reference in New Issue