diff --git a/config/config.exs b/config/config.exs index 83c7a1fa97..8e48ca2b12 100644 --- a/config/config.exs +++ b/config/config.exs @@ -49,7 +49,7 @@ config :changelog, config :changelog, Oban, repo: Changelog.Repo, - queues: [audio_updater: 2, scheduled: 5, email: 5, feeds: 5], + queues: [default: 1, audio_updater: 2, scheduled: 5, email: 5, feeds: 5], plugins: [Oban.Plugins.Pruner] config :changelog, Changelog.Mailer, adapter: Bamboo.LocalAdapter diff --git a/lib/changelog/oban_workers/notes_pusher.ex b/lib/changelog/oban_workers/notes_pusher.ex new file mode 100644 index 0000000000..1e0b50d3d0 --- /dev/null +++ b/lib/changelog/oban_workers/notes_pusher.ex @@ -0,0 +1,22 @@ +defmodule Changelog.ObanWorkers.NotesPusher do + @moduledoc """ + This module defines the Oban worker for pushing episode notes to GitHub + """ + use Oban.Worker + + alias Changelog.{Episode, Github, Repo} + + @impl Oban.Worker + def perform(%Oban.Job{args: %{"episode_id" => episode_id}}) do + episode = Episode |> Repo.get(episode_id) |> Episode.preload_podcast() + source = Github.Source.new("show-notes", episode) + + Github.Pusher.push(source, episode.notes) + + :ok + end + + def queue(episode = %Episode{}) do + %{"episode_id" => episode.id} |> new() |> Oban.insert() + end +end diff --git a/lib/changelog_web/controllers/admin/episode_controller.ex b/lib/changelog_web/controllers/admin/episode_controller.ex index 752112e331..ef8e051a9a 100644 --- a/lib/changelog_web/controllers/admin/episode_controller.ex +++ b/lib/changelog_web/controllers/admin/episode_controller.ex @@ -18,7 +18,7 @@ defmodule ChangelogWeb.Admin.EpisodeController do Snap } - alias Changelog.ObanWorkers.{AudioUpdater, FeedUpdater} + alias Changelog.ObanWorkers.{AudioUpdater, FeedUpdater, NotesPusher} plug :assign_podcast plug Authorize, [Policies.Admin.Episode, :podcast] @@ -399,9 +399,7 @@ defmodule ChangelogWeb.Admin.EpisodeController do defp handle_notes_push_to_github(episode) do if Episode.is_published(episode) do - episode = Episode.preload_podcast(episode) - source = Github.Source.new("show-notes", episode) - Github.Pusher.push(source, episode.notes) + NotesPusher.queue(episode) end end diff --git a/test/changelog_web/controllers/admin/episode_controller_test.exs b/test/changelog_web/controllers/admin/episode_controller_test.exs index 9043282151..db2fdd9a12 100644 --- a/test/changelog_web/controllers/admin/episode_controller_test.exs +++ b/test/changelog_web/controllers/admin/episode_controller_test.exs @@ -11,10 +11,10 @@ defmodule ChangelogWeb.Admin.EpisodeControllerTest do setup_with_mocks( [ - {Github.Pusher, [], [push: fn _, _ -> {:ok, "success"} end]}, {Github.Puller, [], [update: fn _, _ -> true end]}, {Changelog.Merch, [], [create_discount: fn _, _ -> {:ok, %{code: "yup"}} end]}, {ObanWorkers.AudioUpdater, [], [queue: fn _ -> :ok end]}, + {ObanWorkers.NotesPusher, [], [queue: fn _ -> :ok end]}, {Changelog.Snap, [], [purge: fn _ -> :ok end]}, {Craisin.Client, [], [stats: fn _ -> %{"Delivered" => 0, "Opened" => 0} end]} ], @@ -106,7 +106,7 @@ defmodule ChangelogWeb.Admin.EpisodeControllerTest do episode: @valid_attrs ) - refute called(Github.Pusher.push(:_, :_)) + refute called(ObanWorkers.NotesPusher.queue(:_)) assert called(ObanWorkers.AudioUpdater.queue(:_)) assert called(Changelog.Snap.purge(:_)) assert redirected_to(conn) == Routes.admin_podcast_episode_path(conn, :index, p.slug) @@ -123,7 +123,7 @@ defmodule ChangelogWeb.Admin.EpisodeControllerTest do episode: @valid_attrs ) - assert called(Github.Pusher.push(:_, e.notes)) + assert called(ObanWorkers.NotesPusher.queue(:_)) assert called(ObanWorkers.AudioUpdater.queue(:_)) assert redirected_to(conn) == Routes.admin_podcast_episode_path(conn, :index, p.slug) end @@ -138,7 +138,7 @@ defmodule ChangelogWeb.Admin.EpisodeControllerTest do episode: @invalid_attrs ) - refute called(Github.Pusher.push()) + refute called(ObanWorkers.NotesPusher.queue(:_)) refute called(ObanWorkers.AudioUpdater.queue(:_)) assert html_response(conn, 200) =~ ~r/error/ end @@ -173,7 +173,7 @@ defmodule ChangelogWeb.Admin.EpisodeControllerTest do assert redirected_to(conn) == Routes.admin_podcast_episode_path(conn, :index, p.slug) assert count(Episode.published()) == 1 - assert called(Github.Pusher.push(:_, e.notes)) + assert called(ObanWorkers.NotesPusher.queue(:_)) end @tag :as_inserted_admin @@ -186,7 +186,7 @@ defmodule ChangelogWeb.Admin.EpisodeControllerTest do assert redirected_to(conn) == Routes.admin_podcast_episode_path(conn, :index, p.slug) assert count(Episode.published()) == 0 assert count(Episode.scheduled()) == 1 - assert called(Github.Pusher.push(:_, e.notes)) + assert called(ObanWorkers.NotesPusher.queue(:_)) end @tag :as_inserted_admin @@ -207,7 +207,7 @@ defmodule ChangelogWeb.Admin.EpisodeControllerTest do assert count(Episode.published()) == 1 assert Repo.get(EpisodeGuest, eg1.id).thanks assert Repo.get(EpisodeGuest, eg2.id).thanks - assert called(Github.Pusher.push(:_, e.notes)) + assert called(ObanWorkers.NotesPusher.queue(:_)) end @tag :as_inserted_admin