Skip to content

Commit

Permalink
Move show notes push to background worker
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Jul 5, 2024
1 parent ea31cf9 commit ff8a043
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions lib/changelog/oban_workers/notes_pusher.ex
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions lib/changelog_web/controllers/admin/episode_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions test/changelog_web/controllers/admin/episode_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
],
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ff8a043

Please sign in to comment.