Move Podcasts::GetEpisodesJob to Sidekiq (#5443) [deploy]
* Refactored Podcast::GetEpisodesJob to sidekiq * fixing specs * removing env keys * Change arguments to Hash * Update specs and add spec for queue * Update references for Hash argument * Add comment to explain parameters of #perform Co-authored-by: Alex <alexandersmith223@gmail.com>
This commit is contained in:
parent
79dcc185cd
commit
01dc81afa9
6 changed files with 37 additions and 23 deletions
|
|
@ -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"),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
24
app/workers/podcasts/get_episodes_worker.rb
Normal file
24
app/workers/podcasts/get_episodes_worker.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue