Skip to content

Commit

Permalink
refactor: Extract retrieve_work patch
Browse files Browse the repository at this point in the history
This should help DRY-up ixti#175
  • Loading branch information
ixti committed Jan 16, 2024
1 parent 02704ab commit 6294b2b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
19 changes: 4 additions & 15 deletions lib/sidekiq/throttled/patches/basic_fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,14 @@
require "sidekiq"
require "sidekiq/fetch"

require_relative "./throttled_retriever"

module Sidekiq
module Throttled
module Patches
module BasicFetch
# Retrieves job from redis.
#
# @return [Sidekiq::Throttled::UnitOfWork, nil]
def retrieve_work
work = super

if work && Throttled.throttled?(work.job)
Throttled.cooldown&.notify_throttled(work.queue)
requeue_throttled(work)
return nil
end

Throttled.cooldown&.notify_admitted(work.queue) if work

work
def self.prepended(base)
base.prepend(ThrottledRetriever)
end

private
Expand Down
26 changes: 26 additions & 0 deletions lib/sidekiq/throttled/patches/throttled_retriever.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Sidekiq
module Throttled
module Patches
module ThrottledRetriever
# Retrieves job from redis.
#
# @return [Sidekiq::BasicFetch::UnitOfWork, nil]
def retrieve_work
work = super

if work && Throttled.throttled?(work.job)
Throttled.cooldown&.notify_throttled(work.queue)
requeue_throttled(work)
return nil
end

Throttled.cooldown&.notify_admitted(work.queue) if work

work
end
end
end
end
end

0 comments on commit 6294b2b

Please sign in to comment.