Move Podcast Episode Bust Cache Job to Sidekiq (#5416) [deploy]

This commit is contained in:
Maykon Menezes 2020-01-10 13:08:59 -03:00 committed by Molly Struve
parent 00fd2f4518
commit cf4dc80250
6 changed files with 46 additions and 42 deletions

View file

@ -1,12 +0,0 @@
module PodcastEpisodes
class BustCacheJob < ApplicationJob
queue_as :podcast_episodes_bust_cache
def perform(podcast_episode_id, path, podcast_slug, cache_buster = CacheBuster)
podcast_episode = PodcastEpisode.find_by(id: podcast_episode_id)
return unless podcast_episode
cache_buster.bust_podcast_episode(podcast_episode, path, podcast_slug)
end
end
end

View file

@ -140,7 +140,7 @@ class PodcastEpisode < ApplicationRecord
end
def bust_cache
PodcastEpisodes::BustCacheJob.perform_later(id, path, podcast_slug)
PodcastEpisodes::BustCacheWorker.perform_async(id, path, podcast_slug)
end
def process_html_and_prefix_all_images

View file

@ -0,0 +1,14 @@
module PodcastEpisodes
class BustCacheWorker
include Sidekiq::Worker
sidekiq_options queue: :high_priority, retry: 10
def perform(podcast_episode_id, path, podcast_slug)
podcast_episode = PodcastEpisode.find_by(id: podcast_episode_id)
return if podcast_episode.nil?
CacheBuster.bust_podcast_episode(podcast_episode, path, podcast_slug)
end
end
end

View file

@ -1,28 +0,0 @@
require "rails_helper"
RSpec.describe PodcastEpisodes::BustCacheJob do
include_examples "#enqueues_job", "podcast_episodes_bust_cache", 789, "/PodCAst/SlUg", "SlUg"
describe "#perform_now" do
let!(:podcast) { create(:podcast) }
let!(:podcast_episode) { FactoryBot.create(:podcast_episode, podcast_id: podcast.id) }
let(:cache_buster) { class_double(CacheBuster) }
before do
allow(cache_buster).to receive(:bust_podcast_episode)
end
describe "when no podcast episode is found" do
it "does not call the service" do
allow(PodcastEpisode).to receive(:find_by).and_return(nil)
described_class.perform_now(789, "/PodCAst/SlUg", "SlUg", cache_buster)
expect(cache_buster).not_to have_received(:bust_podcast_episode)
end
end
it "busts cache" do
described_class.perform_now(podcast_episode.id, "/PodCAst/SlUg", "SlUg", cache_buster)
expect(cache_buster).to have_received(:bust_podcast_episode).with(podcast_episode, "/PodCAst/SlUg", "SlUg")
end
end
end

View file

@ -117,7 +117,9 @@ RSpec.describe PodcastEpisode, type: :model do
context "when callbacks are triggered after save" do
it "triggers cache busting on save" do
expect { build(:podcast_episode).save }.to have_enqueued_job.on_queue("podcast_episodes_bust_cache")
sidekiq_assert_enqueued_with(job: PodcastEpisodes::BustCacheWorker, args: [podcast_episode.id, podcast_episode.path, podcast_episode.podcast_slug]) do
podcast_episode.save
end
end
end
end

View file

@ -0,0 +1,28 @@
require "rails_helper"
RSpec.describe PodcastEpisodes::BustCacheWorker, type: :worker do
describe "#perform" do
let(:worker) { subject }
before do
allow(CacheBuster).to receive(:bust_podcast_episode)
end
context "when no podcast episode is found" do
it "does not call the service" do
worker.perform(nil, "/PodCAst/SlUg", "SlUg")
expect(CacheBuster).not_to have_received(:bust_podcast_episode)
end
end
context "When podcast episode is found" do
let(:podcast) { create(:podcast) }
let(:podcast_episode) { FactoryBot.create(:podcast_episode, podcast_id: podcast.id) }
it "busts cache" do
worker.perform(podcast_episode.id, "/PodCAst/SlUg", "SlUg")
expect(CacheBuster).to have_received(:bust_podcast_episode).with(podcast_episode, "/PodCAst/SlUg", "SlUg")
end
end
end
end