-
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow lock info to be configured from worker (#407)
- Loading branch information
Showing
9 changed files
with
182 additions
and
141 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module SidekiqUniqueJobs | ||
# | ||
# Gathers all configuration for a lock | ||
# which helps reduce the amount of instance variables | ||
# | ||
# @author Mikael Henriksson <[email protected]> | ||
# | ||
class LockConfig | ||
# | ||
# @!attribute [r] lock | ||
# @return [Symbol] the type of lock | ||
attr_reader :type | ||
# | ||
# @!attribute [r] limit | ||
# @return [Integer] the number of simultaneous locks | ||
attr_reader :limit | ||
# | ||
# @!attribute [r] timeout | ||
# @return [Integer, nil] the time to wait for a lock | ||
attr_reader :timeout | ||
# | ||
# @!attribute [r] ttl | ||
# @return [Integer, nil] the time (in seconds) to live after successful | ||
attr_reader :ttl | ||
# | ||
# @!attribute [r] ttl | ||
# @return [Integer, nil] the time (in milliseconds) to live after successful | ||
attr_reader :pttl | ||
# | ||
# @!attribute [r] lock_info | ||
# @return [Boolean] indicate wether to use lock_info or not | ||
attr_reader :lock_info | ||
# | ||
# @!attribute [r] on_conflict | ||
# @return [Symbol, Hash<Symbol, Symbol>] the strategies to use as conflict resolution | ||
attr_reader :on_conflict | ||
# | ||
# @!attribute [r] errors | ||
# @return [Array<Hash<Symbol, Array<String>] a collection of configuration errors | ||
attr_reader :errors | ||
|
||
def self.from_worker(options) | ||
new(options.stringify_keys) | ||
end | ||
|
||
def initialize(job_hash = {}) | ||
@type = job_hash[LOCK]&.to_sym | ||
@limit = job_hash.fetch(LOCK_LIMIT) { 1 } | ||
@timeout = job_hash.fetch(LOCK_TIMEOUT) { 0 } | ||
@ttl = job_hash.fetch(LOCK_TTL) { job_hash.fetch(LOCK_EXPIRATION) }.to_i | ||
@pttl = ttl * 1_000 | ||
@lock_info = job_hash.fetch(LOCK_INFO) { SidekiqUniqueJobs.config.lock_info } | ||
@on_conflict = job_hash[ON_CONFLICT] | ||
@errors = job_hash[ERRORS] | ||
end | ||
|
||
def wait_for_lock? | ||
timeout.nil? || timeout.positive? | ||
end | ||
end | ||
end |
This file contains 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,68 @@ | ||
# frozen_string_literal: true | ||
|
||
module SidekiqUniqueJobs | ||
# | ||
# Class Info provides information about a lock | ||
# | ||
# @author Mikael Henriksson <[email protected]> | ||
# | ||
class LockInfo < Redis::String | ||
# | ||
# Returns the value for this key as a hash | ||
# | ||
# | ||
# @return [Hash] | ||
# | ||
def value | ||
@value ||= load_json(super) | ||
end | ||
|
||
# | ||
# Check if this redis string is blank | ||
# | ||
# | ||
# @return [Boolean] | ||
# | ||
def none? | ||
value.nil? || value.empty? | ||
end | ||
|
||
# | ||
# Check if this redis string has a value | ||
# | ||
# | ||
# @return [Boolean] | ||
# | ||
def present? | ||
!none? | ||
end | ||
|
||
# | ||
# Quick access to the hash members for the value | ||
# | ||
# @param [String, Symbol] key the key who's value to retrieve | ||
# | ||
# @return [Object] | ||
# | ||
def [](key) | ||
value[key.to_s] if value.is_a?(Hash) | ||
end | ||
|
||
# | ||
# Writes the lock info to redis | ||
# | ||
# @param [Hash] obj the information to store at key | ||
# | ||
# @return [Hash] | ||
# | ||
def set(obj) | ||
return unless SidekiqUniqueJobs.config.lock_info | ||
raise InvalidArgument, "argument `obj` (#{obj}) needs to be a hash" unless obj.is_a?(Hash) | ||
|
||
json = dump_json(obj) | ||
@value = load_json(json) | ||
super(json) | ||
value | ||
end | ||
end | ||
end |
Oops, something went wrong.