diff --git a/app/services/podcasts/get_episode.rb b/app/services/podcasts/get_episode.rb index c3f67614d..030ec9944 100644 --- a/app/services/podcasts/get_episode.rb +++ b/app/services/podcasts/get_episode.rb @@ -12,7 +12,9 @@ module Podcasts if episode try_update_media_url(episode: episode, item_data: item_data, force_update: force_update) else - PodcastEpisodes::CreateWorker.perform_async(podcast.id, item_data.to_h) + episode_cache_key = cache_key(item_data) + cache_episode_data(episode_cache_key, item_data) + PodcastEpisodes::CreateWorker.perform_async(podcast.id, episode_cache_key) end end @@ -36,5 +38,13 @@ module Podcasts rescue ArgumentError, NoMethodError => e Rails.logger.error("not a valid date: #{e}") end + + def cache_episode_data(episode_cache_key, item_data) + Rails.cache.write(episode_cache_key, item_data.to_h) + end + + def cache_key(item_data) + Digest::SHA1.hexdigest(item_data.to_s) + end end end diff --git a/app/workers/podcast_episodes/create_worker.rb b/app/workers/podcast_episodes/create_worker.rb index 34a82e90e..103aacf16 100644 --- a/app/workers/podcast_episodes/create_worker.rb +++ b/app/workers/podcast_episodes/create_worker.rb @@ -4,7 +4,10 @@ module PodcastEpisodes sidekiq_options queue: :high_priority - def perform(podcast_id, item) + def perform(podcast_id, episode_cache_key) + item = Rails.cache.read(episode_cache_key) + return unless item + Podcasts::CreateEpisode.call(podcast_id, item.with_indifferent_access) end end diff --git a/spec/services/podcasts/feed_spec.rb b/spec/services/podcasts/feed_spec.rb index a405be114..33012cd96 100644 --- a/spec/services/podcasts/feed_spec.rb +++ b/spec/services/podcasts/feed_spec.rb @@ -96,7 +96,10 @@ RSpec.describe Podcasts::Feed, type: :service, vcr: vcr_option do end context "when creating" do + let(:cache_store) { ActiveSupport::Cache.lookup_store(:redis_cache_store) } + before do + allow(Rails).to receive(:cache).and_return(cache_store) stub_request(:head, "https://traffic.libsyn.com/sedaily/AnalyseAsia.mp3").to_return(status: 200) stub_request(:head, "https://traffic.libsyn.com/sedaily/IFTTT.mp3").to_return(status: 200) end diff --git a/spec/services/podcasts/get_episode_spec.rb b/spec/services/podcasts/get_episode_spec.rb index 04a13155c..9b6e26b64 100644 --- a/spec/services/podcasts/get_episode_spec.rb +++ b/spec/services/podcasts/get_episode_spec.rb @@ -82,9 +82,10 @@ RSpec.describe Podcasts::GetEpisode, type: :service do end it "enqueues a worker to create an episode when it doesn't exist" do + cache_key = Digest::SHA1.hexdigest(item.to_s) allow(podcast).to receive(:existing_episode).and_return(nil) - sidekiq_assert_enqueued_with(job: PodcastEpisodes::CreateWorker, args: [podcast.id, item.to_h]) do + sidekiq_assert_enqueued_with(job: PodcastEpisodes::CreateWorker, args: [podcast.id, cache_key]) do described_class.new(podcast).call(item: item) end end diff --git a/spec/workers/podcast_episodes/create_worker_spec.rb b/spec/workers/podcast_episodes/create_worker_spec.rb index 0254e17a5..30d2d7f51 100644 --- a/spec/workers/podcast_episodes/create_worker_spec.rb +++ b/spec/workers/podcast_episodes/create_worker_spec.rb @@ -1,30 +1,31 @@ require "rails_helper" RSpec.describe PodcastEpisodes::CreateWorker, type: :worker do - include_examples "#enqueues_on_correct_queue", "high_priority", [123, {}] + include_examples "#enqueues_on_correct_queue", "high_priority", [123, "cache_key"] describe "#perform" do let(:worker) { subject } let(:podcast_id) { 781 } - let(:item) { { foo: "bar" } } + let(:cache_key) { "ep_cache_key" } before do allow(Podcasts::CreateEpisode).to receive(:call) end it "creates a podcast episode" do - worker.perform(podcast_id, item) + allow(Rails.cache).to receive(:read).and_return({}) + worker.perform(podcast_id, cache_key) - expect(Podcasts::CreateEpisode).to have_received(:call).with(podcast_id, item).once + expect(Podcasts::CreateEpisode).to have_received(:call).with(podcast_id, {}).once + expect(Rails.cache).to have_received(:read).with(cache_key).once end - context "when item has string keys" do - it "creates a podcast episode regardless of whether item has string or symbol keys" do - worker.perform(podcast_id, item.stringify_keys) + it "does not create a podcast episode if item is not found" do + allow(Rails.cache).to receive(:read) + worker.perform(podcast_id, cache_key) - expect(Podcasts::CreateEpisode).to have_received(:call).with(podcast_id, item).once - end + expect(Podcasts::CreateEpisode).not_to have_received(:call) end end end