diff --git a/app/controllers/admin/podcasts_controller.rb b/app/controllers/admin/podcasts_controller.rb index 178b33fc8..76cd1cf92 100644 --- a/app/controllers/admin/podcasts_controller.rb +++ b/app/controllers/admin/podcasts_controller.rb @@ -5,7 +5,7 @@ module Admin authorize_resource(resource) if resource.save - Podcasts::GetEpisodesJob.perform_later(podcast_id: resource.id) if resource.published + Podcasts::GetEpisodesWorker.perform_async(podcast_id: resource.id) if resource.published redirect_to( [namespace, resource], notice: translate_with_resource("create.success"), diff --git a/app/jobs/podcasts/get_episodes_job.rb b/app/jobs/podcasts/get_episodes_job.rb deleted file mode 100644 index 699e308eb..000000000 --- a/app/jobs/podcasts/get_episodes_job.rb +++ /dev/null @@ -1,12 +0,0 @@ -module Podcasts - class GetEpisodesJob < ApplicationJob - queue_as :podcasts_get_episodes - - def perform(podcast_id:, limit: 1000, force_update: false, feed: Podcasts::Feed) - podcast = Podcast.find_by(id: podcast_id) - return unless podcast - - feed.new(podcast).get_episodes(limit: limit, force_update: force_update) - end - end -end diff --git a/app/workers/podcasts/get_episodes_worker.rb b/app/workers/podcasts/get_episodes_worker.rb new file mode 100644 index 000000000..11d0928e3 --- /dev/null +++ b/app/workers/podcasts/get_episodes_worker.rb @@ -0,0 +1,24 @@ +module Podcasts + class GetEpisodesWorker + include Sidekiq::Worker + + sidekiq_options queue: :high_priority, retry: 10 + + # podcast_data should be a Hash with keys :podcast_id, :limit, and :force_update + # :limit and :force_update are both optional - there are default values + def perform(podcast_data = {}) + # Sidekiq turns arguments into Strings so the Ruby keyword argument sorcery doesn't work here + # prevent any mismatch between String keys and Symbol keys + podcast_data.symbolize_keys! + + podcast_id = podcast_data[:podcast_id] + limit = podcast_data[:limit] || 1_000 + force_update = podcast_data[:force_update] || false + + podcast = Podcast.find_by(id: podcast_id) + return unless podcast + + Podcasts::Feed.new(podcast).get_episodes(limit: limit, force_update: force_update) + end + end +end diff --git a/lib/tasks/fetch.rake b/lib/tasks/fetch.rake index 135d6bd05..cd1810551 100644 --- a/lib/tasks/fetch.rake +++ b/lib/tasks/fetch.rake @@ -2,7 +2,7 @@ desc "This task is called by the Heroku scheduler add-on" task get_podcast_episodes: :environment do Podcast.published.select(:id).find_each do |podcast| - Podcasts::GetEpisodesJob.perform_later(podcast_id: podcast.id, limit: 5) + Podcasts::GetEpisodesWorker.perform_async({ podcast_id: podcast.id, limit: 5 }) end end diff --git a/spec/requests/admin/podcasts_spec.rb b/spec/requests/admin/podcasts_spec.rb index 010fc1189..5fc069b07 100644 --- a/spec/requests/admin/podcasts_spec.rb +++ b/spec/requests/admin/podcasts_spec.rb @@ -28,16 +28,16 @@ RSpec.describe "Admin::Podcasts", type: :request do end it "enqueues a job after creating a podcast" do - expect do + sidekiq_assert_enqueued_jobs(1, only: Podcasts::GetEpisodesWorker) do post "/admin/podcasts", params: { podcast: valid_attributes } - end.to have_enqueued_job(Podcasts::GetEpisodesJob).exactly(:once) + end end it "doesn't enqueue a job when creating an unpublished podcast" do valid_attributes[:published] = false - expect do + sidekiq_assert_no_enqueued_jobs(only: Podcasts::GetEpisodesWorker) do post "/admin/podcasts", params: { podcast: valid_attributes } - end.not_to have_enqueued_job(Podcasts::GetEpisodesJob) + end end end end diff --git a/spec/jobs/podcasts/get_episodes_job_spec.rb b/spec/workers/podcasts/get_episodes_worker_spec.rb similarity index 50% rename from spec/jobs/podcasts/get_episodes_job_spec.rb rename to spec/workers/podcasts/get_episodes_worker_spec.rb index b2eed9616..1d06d341f 100644 --- a/spec/jobs/podcasts/get_episodes_job_spec.rb +++ b/spec/workers/podcasts/get_episodes_worker_spec.rb @@ -1,11 +1,13 @@ require "rails_helper" -RSpec.describe Podcasts::GetEpisodesJob, type: :job do - include_examples "#enqueues_job", "podcasts_get_episodes", podcast_id: 10 +RSpec.describe Podcasts::GetEpisodesWorker, type: :worker do + # Passing in a random podcast_data since the worker doesn't actually run + include_examples "#enqueues_on_correct_queue", "high_priority", [{ podcast_id: 456, limit: 999, force_update: false }] - describe "#perform_now" do + describe "#perform" do let(:podcast) { create(:podcast) } let(:feed) { instance_double(Podcasts::Feed) } + let(:worker) { subject } before do allow(Podcasts::Feed).to receive(:new).and_return(feed) @@ -13,12 +15,12 @@ RSpec.describe Podcasts::GetEpisodesJob, type: :job do end it "calls the service" do - described_class.perform_now(podcast_id: podcast.id, limit: 5, force_update: true) + worker.perform(podcast_id: podcast.id, limit: 5, force_update: true) expect(feed).to have_received(:get_episodes) end it "doesn't call the service when the podcast is not found" do - described_class.perform_now(podcast_id: Podcast.maximum(:id).to_i + 1, limit: 5) + worker.perform(podcast_id: Podcast.maximum(:id).to_i + 1, limit: 5) expect(feed).not_to have_received(:get_episodes) end end