Merge branch 'oban-jobs' into 'master'
feat: Switch Sitemaps to Oban See merge request mythic-insight/legendary!22
This commit is contained in:
		
						commit
						2277d5c45c
					
				
					 9 changed files with 45 additions and 16 deletions
				
			
		| 
						 | 
					@ -16,9 +16,9 @@ defmodule Content.Application do
 | 
				
			||||||
      # Start the endpoint when the application starts
 | 
					      # Start the endpoint when the application starts
 | 
				
			||||||
      # Start your own worker by calling: Content.Worker.start_link(arg1, arg2, arg3)
 | 
					      # Start your own worker by calling: Content.Worker.start_link(arg1, arg2, arg3)
 | 
				
			||||||
      # worker(Content.Worker, [arg1, arg2, arg3]),
 | 
					      # worker(Content.Worker, [arg1, arg2, arg3]),
 | 
				
			||||||
      worker(Content.Scheduler, []),
 | 
					 | 
				
			||||||
      Content.Telemetry,
 | 
					      Content.Telemetry,
 | 
				
			||||||
      Content.Endpoint,
 | 
					      Content.Endpoint,
 | 
				
			||||||
 | 
					      {Oban, oban_config()},
 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # See https://hexdocs.pm/elixir/Supervisor.html
 | 
					    # See https://hexdocs.pm/elixir/Supervisor.html
 | 
				
			||||||
| 
						 | 
					@ -33,4 +33,9 @@ defmodule Content.Application do
 | 
				
			||||||
    Content.Endpoint.config_change(changed, removed)
 | 
					    Content.Endpoint.config_change(changed, removed)
 | 
				
			||||||
    :ok
 | 
					    :ok
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # Conditionally disable crontab, queues, or plugins here.
 | 
				
			||||||
 | 
					  defp oban_config do
 | 
				
			||||||
 | 
					    Application.get_env(:content, Oban)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
					 | 
				
			||||||
| 
						 | 
					@ -6,12 +6,21 @@ defmodule Content.Sitemaps do
 | 
				
			||||||
  alias Content.{Endpoint, Post, Posts, Repo,  Router.Helpers, Terms}
 | 
					  alias Content.{Endpoint, Post, Posts, Repo,  Router.Helpers, Terms}
 | 
				
			||||||
  import Ecto.Query
 | 
					  import Ecto.Query
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  require Logger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  use Oban.Worker
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  use Sitemap,
 | 
					  use Sitemap,
 | 
				
			||||||
    host: "https://#{Application.get_env(:content, Endpoint)[:url][:host]}",
 | 
					    host: "https://#{Application.get_env(:content, Endpoint)[:url][:host]}",
 | 
				
			||||||
    files_path: "tmp/sitemap/",
 | 
					    files_path: "tmp/sitemap/",
 | 
				
			||||||
    public_path: "",
 | 
					    public_path: "",
 | 
				
			||||||
    adapter: Content.SitemapStorage
 | 
					    adapter: Content.SitemapStorage
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @impl Oban.Worker
 | 
				
			||||||
 | 
					  def perform(_job) do
 | 
				
			||||||
 | 
					    generate()
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def generate do
 | 
					  def generate do
 | 
				
			||||||
    create do
 | 
					    create do
 | 
				
			||||||
      add "", priority: 0.5, changefreq: "hourly", expires: nil
 | 
					      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"])
 | 
					        |> where([p], p.type not in ["nav_menu_item", "attachment"])
 | 
				
			||||||
        |> Repo.all()
 | 
					        |> Repo.all()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for post <- posts do
 | 
					      for post <- posts do
 | 
				
			||||||
        add Helpers.posts_path(Endpoint, :show, post), priority: 0.5, changefreq: "hourly", expires: nil
 | 
					        add Helpers.posts_path(Endpoint, :show, post), priority: 0.5, changefreq: "hourly", expires: nil
 | 
				
			||||||
        page_count = Post.content_page_count(post)
 | 
					        page_count = Post.content_page_count(post)
 | 
				
			||||||
        if page_count > 1 do
 | 
					        if page_count > 1 do
 | 
				
			||||||
| 
						 | 
					@ -41,5 +50,9 @@ defmodule Content.Sitemaps do
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # notify search engines (currently Google and Bing) of the updated sitemap
 | 
					    # notify search engines (currently Google and Bing) of the updated sitemap
 | 
				
			||||||
    if Mix.env() == :prod, do: ping()
 | 
					    if Mix.env() == :prod, do: ping()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Logger.info "Sitemap generated."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    :ok
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,7 +4,7 @@ defmodule Content.SitemapStorage do
 | 
				
			||||||
    the sitemap as an attachment post into the system, so that the CMS will
 | 
					    the sitemap as an attachment post into the system, so that the CMS will
 | 
				
			||||||
    serve it up.
 | 
					    serve it up.
 | 
				
			||||||
  """
 | 
					  """
 | 
				
			||||||
  alias Content.{Endpoint, Post, Repo, Router.Helpers, User}
 | 
					  alias Content.{Endpoint, Post, Repo, Router.Helpers}
 | 
				
			||||||
  alias Ecto.Changeset
 | 
					  alias Ecto.Changeset
 | 
				
			||||||
  alias Sitemap.{Location}
 | 
					  alias Sitemap.{Location}
 | 
				
			||||||
  import Ecto.Query
 | 
					  import Ecto.Query
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -50,6 +50,7 @@ defmodule Content.MixProject do
 | 
				
			||||||
      {:mock, "~> 0.3.0", only: :test},
 | 
					      {:mock, "~> 0.3.0", only: :test},
 | 
				
			||||||
      {:meck, "~> 0.8.13", only: :test},
 | 
					      {:meck, "~> 0.8.13", only: :test},
 | 
				
			||||||
      {:neotomex, "~> 0.1.7"},
 | 
					      {:neotomex, "~> 0.1.7"},
 | 
				
			||||||
 | 
					      {:oban, "~> 2.1"},
 | 
				
			||||||
      {:phoenix, "~> 1.5.3"},
 | 
					      {:phoenix, "~> 1.5.3"},
 | 
				
			||||||
      {:phoenix_ecto, "~> 4.0"},
 | 
					      {:phoenix_ecto, "~> 4.0"},
 | 
				
			||||||
      {:phoenix_html, "~> 2.11"},
 | 
					      {:phoenix_html, "~> 2.11"},
 | 
				
			||||||
| 
						 | 
					@ -58,7 +59,6 @@ defmodule Content.MixProject do
 | 
				
			||||||
      {:phoenix_live_dashboard, "~> 0.2.0"},
 | 
					      {:phoenix_live_dashboard, "~> 0.2.0"},
 | 
				
			||||||
      {:php_serializer, "~> 0.9.0"},
 | 
					      {:php_serializer, "~> 0.9.0"},
 | 
				
			||||||
      {:plug_cowboy, "~> 2.0"},
 | 
					      {:plug_cowboy, "~> 2.0"},
 | 
				
			||||||
      {:quantum, "~> 2.3"},
 | 
					 | 
				
			||||||
      {:sitemap, "~> 1.1"},
 | 
					      {:sitemap, "~> 1.1"},
 | 
				
			||||||
      {:slugger, "~> 0.3"},
 | 
					      {:slugger, "~> 0.3"},
 | 
				
			||||||
      {:telemetry_metrics, "~> 0.4"},
 | 
					      {:telemetry_metrics, "~> 0.4"},
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -53,6 +53,7 @@ config :core, email_from: "example@example.org"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Configures Elixir's Logger
 | 
					# Configures Elixir's Logger
 | 
				
			||||||
config :logger, :console,
 | 
					config :logger, :console,
 | 
				
			||||||
 | 
					  level: :debug,
 | 
				
			||||||
  format: "$time $metadata[$level] $message\n",
 | 
					  format: "$time $metadata[$level] $message\n",
 | 
				
			||||||
  metadata: [:request_id]
 | 
					  metadata: [:request_id]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -61,9 +62,12 @@ config :phoenix, :json_library, Jason
 | 
				
			||||||
 | 
					
 | 
				
			||||||
config :linguist, pluralization_key: :count
 | 
					config :linguist, pluralization_key: :count
 | 
				
			||||||
 | 
					
 | 
				
			||||||
config :content, Content.Scheduler,
 | 
					config :content,
 | 
				
			||||||
  jobs: [
 | 
					  Oban,
 | 
				
			||||||
    {"@hourly", {Content.Sitemaps, :generate, []}}
 | 
					  repo: Content.Repo,
 | 
				
			||||||
 | 
					  queues: [default: 10],
 | 
				
			||||||
 | 
					  crontab: [
 | 
				
			||||||
 | 
					    {"0 * * * *", Content.Sitemaps},
 | 
				
			||||||
  ]
 | 
					  ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import_config "email_styles.exs"
 | 
					import_config "email_styles.exs"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,4 +39,6 @@ end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
config :core, CoreMailer, adapter: Bamboo.TestAdapter
 | 
					config :core, CoreMailer, adapter: Bamboo.TestAdapter
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					config :content, Oban, crontab: false, queues: false, plugins: false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
config :logger, level: :warn
 | 
					config :logger, level: :warn
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										1
									
								
								mix.lock
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								mix.lock
									
									
									
									
									
								
							| 
						 | 
					@ -42,6 +42,7 @@
 | 
				
			||||||
  "mochiweb": {:hex, :mochiweb, "2.12.2", "80804ad342afa3d7f3524040d4eed66ce74b17a555de454ac85b07c479928e46", [:make, :rebar], [], "hexpm", "d3e681d4054b74a96cf2efcd09e94157ab83a5f55ddc4ce69f90b8144673bd7a"},
 | 
					  "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"},
 | 
					  "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"},
 | 
					  "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"},
 | 
					  "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": {: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"},
 | 
					  "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"},
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue