lib/services/modules/uniqueness_checker.rb in services-0.2.5 vs lib/services/modules/uniqueness_checker.rb in services-0.2.6

- old
+ new

@@ -1,36 +1,44 @@ module Services class Base module UniquenessChecker + KEY_PREFIX = %w( + services + uniqueness + ).join(':') + def self.prepended(mod) mod.const_set :NotUniqueError, Class.new(mod::Error) end def check_uniqueness!(*args) - raise 'A variable named @uniqueness_key is already defined. Have you called `check_uniqueness!` twice?' if defined?(@uniqueness_key) - raise 'Could not find @uniqueness_all_args' unless defined?(@uniqueness_all_args) - args = @uniqueness_all_args if args.empty? - @uniqueness_key = uniqueness_key(args) - if similar_service_id = Services.configuration.redis.get(@uniqueness_key) - raise self.class::NotUniqueError, "Service #{self.class} with args #{args} is not unique, a similar service is already running: #{similar_service_id}" + if args.empty? + raise 'Could not find uniqueness args' unless defined?(@uniqueness_args) + args = @uniqueness_args + end + @uniqueness_keys ||= [] + new_uniqueness_key = uniqueness_key(args) + raise "A uniqueness key with args #{args.inspect} already exists." if @uniqueness_keys.include?(new_uniqueness_key) + @uniqueness_keys << new_uniqueness_key + if similar_service_id = Services.configuration.redis.get(new_uniqueness_key) + raise self.class::NotUniqueError, "Service #{self.class} with uniqueness args #{args} is not unique, a similar service is already running: #{similar_service_id}" else - Services.configuration.redis.setex @uniqueness_key, 60 * 60, @id + Services.configuration.redis.setex new_uniqueness_key, 60 * 60, @id end end def call(*args) - @uniqueness_all_args = args + @uniqueness_args = args super ensure - Services.configuration.redis.del @uniqueness_key if defined?(@uniqueness_key) + Services.configuration.redis.del @uniqueness_keys if defined?(@uniqueness_keys) end private def uniqueness_key(args) [ - 'services', - 'uniqueness', + KEY_PREFIX, self.class.to_s ].tap do |key| key << Digest::MD5.hexdigest(args.to_s) unless args.empty? end.join(':') end