From 64822925ed27427ee52676b4ded27d782c097401 Mon Sep 17 00:00:00 2001 From: Robert Prehn Date: Fri, 9 Oct 2020 14:32:53 -0500 Subject: [PATCH] feat: Switch Sitemaps to Oban --- apps/content/lib/content/application.ex | 7 ++++++- apps/content/lib/content/scheduler.ex | 9 --------- apps/content/lib/content/sitemap.ex | 15 ++++++++++++++- apps/content/lib/content/sitemap_storage.ex | 2 +- apps/content/mix.exs | 2 +- .../20201009173604_add_oban_jobs_table.exs | 13 +++++++++++++ config/config.exs | 10 +++++++--- config/test.exs | 2 ++ mix.lock | 1 + 9 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 apps/content/priv/repo/migrations/20201009173604_add_oban_jobs_table.exs diff --git a/apps/content/lib/content/application.ex b/apps/content/lib/content/application.ex index 2c03a6d2..e2a51ea2 100644 --- a/apps/content/lib/content/application.ex +++ b/apps/content/lib/content/application.ex @@ -16,9 +16,9 @@ defmodule Content.Application do # Start the endpoint when the application starts # Start your own worker by calling: Content.Worker.start_link(arg1, arg2, arg3) # worker(Content.Worker, [arg1, arg2, arg3]), - worker(Content.Scheduler, []), Content.Telemetry, Content.Endpoint, + {Oban, oban_config()}, ] # See https://hexdocs.pm/elixir/Supervisor.html @@ -33,4 +33,9 @@ defmodule Content.Application do Content.Endpoint.config_change(changed, removed) :ok end + + # Conditionally disable crontab, queues, or plugins here. + defp oban_config do + Application.get_env(:content, Oban) + end end diff --git a/apps/content/lib/content/scheduler.ex b/apps/content/lib/content/scheduler.ex index 88d07146..e69de29b 100644 --- a/apps/content/lib/content/scheduler.ex +++ b/apps/content/lib/content/scheduler.ex @@ -1,9 +0,0 @@ -defmodule Content.Scheduler do - @moduledoc """ - The Quantum cron-like scheduler for this application. See config.exs for - configured jobs. - """ - - use Quantum.Scheduler, - otp_app: :content -end diff --git a/apps/content/lib/content/sitemap.ex b/apps/content/lib/content/sitemap.ex index 80edd50b..57d4f735 100644 --- a/apps/content/lib/content/sitemap.ex +++ b/apps/content/lib/content/sitemap.ex @@ -6,12 +6,21 @@ defmodule Content.Sitemaps do alias Content.{Endpoint, Post, Posts, Repo, Router.Helpers, Terms} import Ecto.Query + require Logger + + use Oban.Worker + use Sitemap, host: "https://#{Application.get_env(:content, Endpoint)[:url][:host]}", files_path: "tmp/sitemap/", public_path: "", adapter: Content.SitemapStorage + @impl Oban.Worker + def perform(_job) do + generate() + end + def generate do create do add "", priority: 0.5, changefreq: "hourly", expires: nil @@ -21,7 +30,7 @@ defmodule Content.Sitemaps do |> where([p], p.type not in ["nav_menu_item", "attachment"]) |> Repo.all() - for post <- posts do + for post <- posts do add Helpers.posts_path(Endpoint, :show, post), priority: 0.5, changefreq: "hourly", expires: nil page_count = Post.content_page_count(post) if page_count > 1 do @@ -41,5 +50,9 @@ defmodule Content.Sitemaps do # notify search engines (currently Google and Bing) of the updated sitemap if Mix.env() == :prod, do: ping() + + Logger.info "Sitemap generated." + + :ok end end diff --git a/apps/content/lib/content/sitemap_storage.ex b/apps/content/lib/content/sitemap_storage.ex index 5954b529..82587694 100644 --- a/apps/content/lib/content/sitemap_storage.ex +++ b/apps/content/lib/content/sitemap_storage.ex @@ -4,7 +4,7 @@ defmodule Content.SitemapStorage do the sitemap as an attachment post into the system, so that the CMS will serve it up. """ - alias Content.{Endpoint, Post, Repo, Router.Helpers, User} + alias Content.{Endpoint, Post, Repo, Router.Helpers} alias Ecto.Changeset alias Sitemap.{Location} import Ecto.Query diff --git a/apps/content/mix.exs b/apps/content/mix.exs index 6ef37237..938004ef 100644 --- a/apps/content/mix.exs +++ b/apps/content/mix.exs @@ -50,6 +50,7 @@ defmodule Content.MixProject do {:mock, "~> 0.3.0", only: :test}, {:meck, "~> 0.8.13", only: :test}, {:neotomex, "~> 0.1.7"}, + {:oban, "~> 2.1"}, {:phoenix, "~> 1.5.3"}, {:phoenix_ecto, "~> 4.0"}, {:phoenix_html, "~> 2.11"}, @@ -58,7 +59,6 @@ defmodule Content.MixProject do {:phoenix_live_dashboard, "~> 0.2.0"}, {:php_serializer, "~> 0.9.0"}, {:plug_cowboy, "~> 2.0"}, - {:quantum, "~> 2.3"}, {:sitemap, "~> 1.1"}, {:slugger, "~> 0.3"}, {:telemetry_metrics, "~> 0.4"}, diff --git a/apps/content/priv/repo/migrations/20201009173604_add_oban_jobs_table.exs b/apps/content/priv/repo/migrations/20201009173604_add_oban_jobs_table.exs new file mode 100644 index 00000000..9cc6b38f --- /dev/null +++ b/apps/content/priv/repo/migrations/20201009173604_add_oban_jobs_table.exs @@ -0,0 +1,13 @@ +defmodule Content.Repo.Migrations.AddObanJobsTable do + use Ecto.Migration + + def up do + Oban.Migrations.up() + end + + # We specify `version: 1` in `down`, ensuring that we'll roll all the way back down if + # necessary, regardless of which version we've migrated `up` to. + def down do + Oban.Migrations.down(version: 1) + end +end diff --git a/config/config.exs b/config/config.exs index aa4297ea..67c22e49 100644 --- a/config/config.exs +++ b/config/config.exs @@ -53,6 +53,7 @@ config :core, email_from: "example@example.org" # Configures Elixir's Logger config :logger, :console, + level: :debug, format: "$time $metadata[$level] $message\n", metadata: [:request_id] @@ -61,9 +62,12 @@ config :phoenix, :json_library, Jason config :linguist, pluralization_key: :count -config :content, Content.Scheduler, - jobs: [ - {"@hourly", {Content.Sitemaps, :generate, []}} +config :content, + Oban, + repo: Content.Repo, + queues: [default: 10], + crontab: [ + {"0 * * * *", Content.Sitemaps}, ] import_config "email_styles.exs" diff --git a/config/test.exs b/config/test.exs index 5e4f05ba..4c031c56 100644 --- a/config/test.exs +++ b/config/test.exs @@ -39,4 +39,6 @@ end) config :core, CoreMailer, adapter: Bamboo.TestAdapter +config :content, Oban, crontab: false, queues: false, plugins: false + config :logger, level: :warn diff --git a/mix.lock b/mix.lock index 6880c55a..bde38b2e 100644 --- a/mix.lock +++ b/mix.lock @@ -42,6 +42,7 @@ "mochiweb": {:hex, :mochiweb, "2.12.2", "80804ad342afa3d7f3524040d4eed66ce74b17a555de454ac85b07c479928e46", [:make, :rebar], [], "hexpm", "d3e681d4054b74a96cf2efcd09e94157ab83a5f55ddc4ce69f90b8144673bd7a"}, "mock": {:hex, :mock, "0.3.5", "feb81f52b8dcf0a0d65001d2fec459f6b6a8c22562d94a965862f6cc066b5431", [:mix], [{:meck, "~> 0.8.13", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "6fae404799408300f863550392635d8f7e3da6b71abdd5c393faf41b131c8728"}, "neotomex": {:hex, :neotomex, "0.1.7", "64f76513653aa87ea7abdde0fd600e56955d838020a13d88f2bf334c88ac3e7a", [:mix], [], "hexpm", "4b87b8f614d1cd89dc8ba80ba0e559bedb3ebf6f6d74cd774fcfdd215e861445"}, + "oban": {:hex, :oban, "2.1.0", "034144686f7e76a102b5d67731f098d98a9e4a52b07c25ad580a01f83a7f1cf5", [:mix], [{:ecto_sql, ">= 3.4.3", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.14", [hex: :postgrex, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c6f067fa3b308ed9e0e6beb2b34277c9c4e48bf95338edabd8f4a757a26e04c2"}, "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"}, "phoenix": {:hex, :phoenix, "1.5.5", "9a5a197edc1828c5f138a8ef10524dfecc43e36ab435c14578b1e9b4bd98858c", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b10eaf86ad026eafad2ee3dd336f0fb1c95a3711789855d913244e270bde463b"}, "phoenix_ecto": {:hex, :phoenix_ecto, "4.1.0", "a044d0756d0464c5a541b4a0bf4bcaf89bffcaf92468862408290682c73ae50d", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "c5e666a341ff104d0399d8f0e4ff094559b2fde13a5985d4cb5023b2c2ac558b"},