Add configurable refresh frequency to feeds

This commit is contained in:
Shadowfacts 2020-05-31 22:48:04 -04:00
parent 939470767b
commit e429d256d6
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 46 additions and 8 deletions

View File

@ -36,6 +36,8 @@ defmodule Frenzy.Feed do
field :title, :string field :title, :string
field :favicon, :string field :favicon, :string
field :favicon_url, :string field :favicon_url, :string
field :last_refreshed_at, :utc_datetime
field :refresh_frequency, :integer, default: 30
belongs_to :group, Frenzy.Group belongs_to :group, Frenzy.Group
belongs_to :pipeline, Frenzy.Pipeline belongs_to :pipeline, Frenzy.Pipeline
@ -54,6 +56,8 @@ defmodule Frenzy.Feed do
title: String.t() | nil, title: String.t() | nil,
favicon: String.t() | nil, favicon: String.t() | nil,
favicon_url: String.t() | nil, favicon_url: String.t() | nil,
last_refreshed_at: DateTime.t() | nil,
refresh_frequency: integer(),
group: Frenzy.Group.t() | Ecto.Association.NotLoaded.t(), group: Frenzy.Group.t() | Ecto.Association.NotLoaded.t(),
pipeline: Frenzy.Pipeline.t() | nil | Ecto.Association.NotLoaded.t(), pipeline: Frenzy.Pipeline.t() | nil | Ecto.Association.NotLoaded.t(),
items: [Frenzy.Item.t()] | Ecto.Association.NotLoaded.t(), items: [Frenzy.Item.t()] | Ecto.Association.NotLoaded.t(),
@ -71,8 +75,11 @@ defmodule Frenzy.Feed do
:last_updated, :last_updated,
:pipeline_id, :pipeline_id,
:favicon, :favicon,
:favicon_url :favicon_url,
:last_refreshed_at,
:refresh_frequency
]) ])
|> validate_required([:feed_url]) |> validate_required([:feed_url])
|> validate_inclusion(:refresh_frequency, [30 * 60, 60 * 60, 6 * 60 * 60, 24 * 60 * 60])
end end
end end

View File

@ -40,8 +40,17 @@ defmodule Frenzy.UpdateFeeds do
defp update_feeds() do defp update_feeds() do
Logger.info("Updating all feeds") Logger.info("Updating all feeds")
Repo.all(from(Feed)) {_count, feeds} =
|> Enum.map(&update_feed/1) Feed
|> where(
[f],
is_nil(f.last_refreshed_at) or
f.last_refreshed_at <= from_now(-1 * f.refresh_frequency, "second")
)
|> select([f], f)
|> Repo.update_all(set: [last_refreshed_at: DateTime.utc_now()])
Enum.map(feeds, &update_feed/1)
prune_old_items() prune_old_items()
end end

View File

@ -77,7 +77,13 @@ defmodule FrenzyWeb.FeedController do
render(conn, "edit.html", %{ render(conn, "edit.html", %{
feed: feed, feed: feed,
changeset: edit_changeset, changeset: edit_changeset,
pipelines: [{"No Pipeline", nil} | pipelines] pipelines: [{"No Pipeline", nil} | pipelines],
refresh_frequencies: [
{"Every 30 minutes", 30 * 60},
{"Every hour", 60 * 60},
{"Every 6 hours", 6 * 60 * 60},
{"Every 24 hours", 24 * 60 * 60}
]
}) })
end end

View File

@ -1,6 +1,12 @@
<h1>Edit Feed</h1> <h1>Edit Feed</h1>
<%= form_for @changeset, Routes.feed_path(@conn, :update, @feed.id), fn f -> %> <%= form_for @changeset, Routes.feed_path(@conn, :update, @feed.id), fn f -> %>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="refresh_frequency">Refresh Frequency</label>
<div class="col-sm-10">
<%= select f, :refresh_frequency, @refresh_frequencies, class: "custom-select" %>
</div>
</div>
<div class="form-group row"> <div class="form-group row">
<label class="col-sm-2 col-form-label" for="pipeline_id">Pipeline ID</label> <label class="col-sm-2 col-form-label" for="pipeline_id">Pipeline ID</label>
<div class="col-sm-10"> <div class="col-sm-10">
@ -8,11 +14,11 @@
</div> </div>
</div> </div>
<%= if @feed.pipeline_id do %> <%= if @feed.pipeline_id do %>
<div class="form-group row"> <div class="form-group row">
<div class="col-sm-10"> <div class="col-sm-10">
<a href="<%= Routes.pipeline_path(@conn, :show, @feed.pipeline_id) %>" class="col-sm-2">View Pipeline</a> <a href="<%= Routes.pipeline_path(@conn, :show, @feed.pipeline_id) %>" class="col-sm-2">View Pipeline</a>
</div>
</div> </div>
</div>
<% end %> <% end %>
<div class="form-group row"> <div class="form-group row">
<div class="col-sm-10"> <div class="col-sm-10">

View File

@ -0,0 +1,10 @@
defmodule Frenzy.Repo.Migrations.FeedsAddRefreshFrequency do
use Ecto.Migration
def change do
alter table(:feeds) do
add :last_refreshed_at, :naive_datetime, default: nil
add :refresh_frequency, :integer, default: 30 * 60
end
end
end