From cf4dc8025063158defd4fa6510bac5c601c1124d Mon Sep 17 00:00:00 2001 From: Maykon Menezes Date: Fri, 10 Jan 2020 13:08:59 -0300 Subject: [PATCH] Move Podcast Episode Bust Cache Job to Sidekiq (#5416) [deploy] --- app/jobs/podcast_episodes/bust_cache_job.rb | 12 -------- app/models/podcast_episode.rb | 2 +- .../podcast_episodes/bust_cache_worker.rb | 14 ++++++++++ .../podcast_episodes/bust_cache_job_spec.rb | 28 ------------------- spec/models/podcast_episode_spec.rb | 4 ++- .../bust_cache_worker_spec.rb | 28 +++++++++++++++++++ 6 files changed, 46 insertions(+), 42 deletions(-) delete mode 100644 app/jobs/podcast_episodes/bust_cache_job.rb create mode 100644 app/workers/podcast_episodes/bust_cache_worker.rb delete mode 100644 spec/jobs/podcast_episodes/bust_cache_job_spec.rb create mode 100644 spec/workers/podcast_episodes/bust_cache_worker_spec.rb diff --git a/app/jobs/podcast_episodes/bust_cache_job.rb b/app/jobs/podcast_episodes/bust_cache_job.rb deleted file mode 100644 index c6bdcafbe..000000000 --- a/app/jobs/podcast_episodes/bust_cache_job.rb +++ /dev/null @@ -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 diff --git a/app/models/podcast_episode.rb b/app/models/podcast_episode.rb index 6050a4008..c9829b9d5 100644 --- a/app/models/podcast_episode.rb +++ b/app/models/podcast_episode.rb @@ -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 diff --git a/app/workers/podcast_episodes/bust_cache_worker.rb b/app/workers/podcast_episodes/bust_cache_worker.rb new file mode 100644 index 000000000..e4350c2b0 --- /dev/null +++ b/app/workers/podcast_episodes/bust_cache_worker.rb @@ -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 diff --git a/spec/jobs/podcast_episodes/bust_cache_job_spec.rb b/spec/jobs/podcast_episodes/bust_cache_job_spec.rb deleted file mode 100644 index 44aeefd78..000000000 --- a/spec/jobs/podcast_episodes/bust_cache_job_spec.rb +++ /dev/null @@ -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 diff --git a/spec/models/podcast_episode_spec.rb b/spec/models/podcast_episode_spec.rb index 1af8f024a..a906c7bb8 100644 --- a/spec/models/podcast_episode_spec.rb +++ b/spec/models/podcast_episode_spec.rb @@ -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 diff --git a/spec/workers/podcast_episodes/bust_cache_worker_spec.rb b/spec/workers/podcast_episodes/bust_cache_worker_spec.rb new file mode 100644 index 000000000..86831d0ea --- /dev/null +++ b/spec/workers/podcast_episodes/bust_cache_worker_spec.rb @@ -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