Skip to content
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

Allow lock info to be configured from worker #407

Merged
merged 1 commit into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .reek.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ detectors:
- SidekiqUniqueJobs::Lock#del
- SidekiqUniqueJobs::Lock#lock
- SidekiqUniqueJobs::Lock::WhileExecuting#execute
- SidekiqUniqueJobs::Locksmith#enqueue
- SidekiqUniqueJobs::Locksmith#lock_sync
- SidekiqUniqueJobs::Locksmith#set_lock_info
- SidekiqUniqueJobs::Locksmith#wait_for_primed_token
Expand Down Expand Up @@ -112,7 +113,7 @@ detectors:
- SidekiqUniqueJobs::Digests#entries
- SidekiqUniqueJobs::Digests#page
- SidekiqUniqueJobs::Lock#lock
- SidekiqUniqueJobs::Lock::Info#set
- SidekiqUniqueJobs::LockInfo#set
- SidekiqUniqueJobs::Lock::WhileExecuting#execute
- SidekiqUniqueJobs::Locksmith#enqueue
- SidekiqUniqueJobs::Locksmith#lock
Expand Down Expand Up @@ -140,6 +141,7 @@ detectors:
TooManyInstanceVariables:
exclude:
- SidekiqUniqueJobs::Key
- SidekiqUniqueJobs::LockConfig
- SidekiqUniqueJobs::Locksmith
TooManyMethods:
exclude:
Expand Down
19 changes: 10 additions & 9 deletions lib/sidekiq_unique_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
require "sidekiq_unique_jobs/json"
require "sidekiq_unique_jobs/normalizer"
require "sidekiq_unique_jobs/job"
require "sidekiq_unique_jobs/redis"
require "sidekiq_unique_jobs/redis/entity"
require "sidekiq_unique_jobs/redis/hash"
require "sidekiq_unique_jobs/redis/list"
require "sidekiq_unique_jobs/redis/set"
require "sidekiq_unique_jobs/redis/sorted_set"
require "sidekiq_unique_jobs/redis/string"
require "sidekiq_unique_jobs/batch_delete"
require "sidekiq_unique_jobs/orphans/reaper"
require "sidekiq_unique_jobs/orphans/observer"
Expand All @@ -41,6 +48,9 @@
require "sidekiq_unique_jobs/key"
require "sidekiq_unique_jobs/locksmith"
require "sidekiq_unique_jobs/options_with_fallback"
require "sidekiq_unique_jobs/lock"
require "sidekiq_unique_jobs/lock_config"
require "sidekiq_unique_jobs/lock_info"
require "sidekiq_unique_jobs/lock/base_lock"
require "sidekiq_unique_jobs/lock/until_executed"
require "sidekiq_unique_jobs/lock/until_executing"
Expand All @@ -53,15 +63,6 @@
require "sidekiq_unique_jobs/middleware/server"
require "sidekiq_unique_jobs/sidekiq_unique_ext"
require "sidekiq_unique_jobs/on_conflict"
require "sidekiq_unique_jobs/redis"
require "sidekiq_unique_jobs/redis/entity"
require "sidekiq_unique_jobs/redis/hash"
require "sidekiq_unique_jobs/redis/list"
require "sidekiq_unique_jobs/redis/set"
require "sidekiq_unique_jobs/redis/sorted_set"
require "sidekiq_unique_jobs/redis/string"
require "sidekiq_unique_jobs/lock"
require "sidekiq_unique_jobs/lock/info"
require "sidekiq_unique_jobs/changelog"
require "sidekiq_unique_jobs/digests"

Expand Down
9 changes: 7 additions & 2 deletions lib/sidekiq_unique_jobs/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@ module SidekiqUniqueJobs
AT ||= "at"
CHANGELOGS ||= "uniquejobs:changelog"
CLASS ||= "class"
DEAD_VERSION ||= "uniquejobs:dead"
DIGESTS ||= "uniquejobs:digests"
ERRORS ||= "errors"
JID ||= "jid"
LIVE_VERSION ||= "uniquejobs:live"
DEAD_VERSION ||= "uniquejobs:dead"
LIMIT ||= "limit"
LIVE_VERSION ||= "uniquejobs:live"
LOCK ||= "lock"
LOCK_EXPIRATION ||= "lock_expiration"
LOCK_INFO ||= "lock_info"
LOCK_LIMIT ||= "lock_limit"
LOCK_PREFIX ||= "lock_prefix"
LOCK_TIMEOUT ||= "lock_timeout"
LOCK_TTL ||= "lock_ttl"
LOCK_TYPE ||= "lock_type"
LOG_DUPLICATE ||= "log_duplicate"
ON_CLIENT_CONFLICT ||= "on_client_conflict"
ON_CONFLICT ||= "on_conflict"
ON_SERVER_CONFLICT ||= "on_server_conflict"
QUEUE ||= "queue"
RETRY ||= "retry"
SCHEDULE ||= "schedule"
TIME ||= "time"
TIMEOUT ||= "timeout"
TTL ||= "ttl"
TYPE ||= "type"
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq_unique_jobs/lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def locked
# @return [Redis::Hash] with lock information
#
def info
@info ||= Lock::Info.new(key.info)
@info ||= LockInfo.new(key.info)
end

#
Expand Down
75 changes: 0 additions & 75 deletions lib/sidekiq_unique_jobs/lock/info.rb

This file was deleted.

63 changes: 63 additions & 0 deletions lib/sidekiq_unique_jobs/lock_config.rb
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
68 changes: 68 additions & 0 deletions lib/sidekiq_unique_jobs/lock_info.rb
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
Loading