forked from gzigzigzeo/sidekiq-grouping
-
Notifications
You must be signed in to change notification settings - Fork 1
Add singleton capabilities #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
xaibot
wants to merge
1
commit into
master
Choose a base branch
from
add-singleton-capability
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sidekiq | ||
module Grouping | ||
class SingletonBase | ||
attr_reader :queue, :worker_class | ||
|
||
def initialize(queue, worker_class) | ||
@queue = queue | ||
@worker_class = worker_class | ||
end | ||
|
||
def singleton_flusher_locked? | ||
redis_client.get(flusher_key).present? | ||
end | ||
|
||
def singleton_worker_locked? | ||
redis_client.get(worker_key).present? | ||
end | ||
|
||
def unlock_singleton_flusher | ||
redis_client.del(flusher_key) | ||
end | ||
|
||
private | ||
|
||
def flusher_key | ||
@flusher_key ||= "#{worker_key}:flusher" | ||
end | ||
|
||
def worker_key | ||
@worker_key ||= "#{worker_class.to_s.underscore}:#{queue}" | ||
end | ||
|
||
def redis_client | ||
@redis_client ||= Sidekiq.redis { _1 } | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sidekiq | ||
module Grouping | ||
class SingletonFlusher < SingletonBase | ||
attr_reader :flusher_lock_ttl | ||
|
||
def initialize(queue, worker_class, flusher_lock_ttl: 5) | ||
super(queue, worker_class) | ||
|
||
@flusher_lock_ttl = flusher_lock_ttl | ||
end | ||
|
||
def lock_singleton_flusher | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exclamation? |
||
raise "Singleton flusher lock for '#{flusher_key}' already exists" if singleton_flusher_locked? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why raise? |
||
|
||
redis_client.set(flusher_key, Time.current.to_s, ex: flusher_lock_ttl) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sidekiq | ||
module Grouping | ||
module SingletonFlusherConcern | ||
def could_flush_on_singleton_worker? | ||
return true unless singleton_worker? | ||
|
||
!(singleton_worker_running? || singleton_flusher_locked?) | ||
end | ||
|
||
def lock_singleton_flusher | ||
# This lock is released within Sidekiq::Grouping::SingletonWorker#with_singleton_worker_lock. | ||
# It prevents plucking again between the time when data is plucked and when the worker starts. | ||
singleton_flusher.lock_singleton_flusher if singleton_worker? | ||
end | ||
|
||
private | ||
|
||
def flusher_lock_ttl | ||
worker_class_options["singleton_flusher_lock_ttl"] | ||
end | ||
|
||
def singleton_flusher | ||
@singleton_flusher ||= | ||
Sidekiq::Grouping::SingletonFlusher.new(queue, worker_class, flusher_lock_ttl:) | ||
end | ||
|
||
def singleton_flusher_locked? | ||
@singleton_flusher.singleton_flusher_locked? | ||
end | ||
|
||
def singleton_worker? | ||
!!worker_class_options["singleton_worker"] | ||
end | ||
|
||
def singleton_worker_running? | ||
@singleton_flusher.singleton_worker_locked? | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sidekiq | ||
module Grouping | ||
class SingletonWorker < SingletonBase | ||
attr_reader :worker_lock_ttl | ||
|
||
def initialize(queue, worker_class, worker_lock_ttl: 3_600) | ||
super(queue, worker_class) | ||
|
||
@worker_lock_ttl = worker_lock_ttl | ||
end | ||
|
||
def with_singleton_worker_lock(&) | ||
lock_singleton_worker | ||
|
||
yield | ||
ensure | ||
unlock_singleton_worker | ||
end | ||
|
||
def lock_singleton_worker | ||
raise "Singleton lock for '#{worker_key}' already exists" if singleton_worker_locked? | ||
|
||
redis_client.set(worker_key, Time.current.to_s, ex: worker_lock_ttl) | ||
ensure | ||
unlock_singleton_flusher | ||
end | ||
|
||
def unlock_singleton_worker | ||
redis_client.del(worker_key) | ||
end | ||
|
||
# Method handy for unlocking from the console in case of need. | ||
def release_locks | ||
unlock_singleton_worker | ||
unlock_singleton_flusher | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sidekiq | ||
module Grouping | ||
module SingletonWorkerConcern | ||
def with_singleton_worker_lock(&) | ||
if singleton_worker? | ||
singleton_worker.with_singleton_worker_lock(&) | ||
else | ||
yield | ||
end | ||
end | ||
|
||
private | ||
|
||
def singleton_worker | ||
Sidekiq::Grouping::SingletonWorker.new( | ||
sidekiq_options_hash['queue'], | ||
self, | ||
worker_lock_ttl: sidekiq_options_hash['singleton_worker_lock_ttl'] | ||
) | ||
end | ||
|
||
def singleton_worker? | ||
sidekiq_options_hash['singleton_worker'] | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5 seconds?