Sha256: 796cac9a7e43864cff4ea02e7c1f72bf507b9aa6b360492110294070879a1fac

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

require 'unique_job/logging'
require 'unique_job/job_history'

module UniqueJob
  module Util
    include Logging

    def perform_if_unique(worker, args, &block)
      if worker.respond_to?(:unique_key)
        unique_key = worker.unique_key(*args)
        logger.debug { "[UniqueJob] Calculate unique key worker=#{worker.class} key=#{unique_key}" }

        if unique_key.nil? || unique_key.to_s.empty?
          logger.warn { "[UniqueJob] Don't check a job with a blank key worker=#{worker.class} key=#{unique_key}" }
          yield
        elsif check_uniqueness(worker, unique_key.to_s)
          yield
        else
          logger.debug { "[UniqueJob] Duplicate job skipped worker=#{worker.class} key=#{unique_key}" }
          perform_callback(worker, :after_skip, args)
          nil
        end
      else
        yield
      end
    end

    def check_uniqueness(worker, unique_key)
      history = job_history(worker)

      if history.exists?(unique_key)
        false
      else
        history.add(unique_key)
        true
      end
    end

    def job_history(worker)
      ttl = worker.respond_to?(:unique_in) ? worker.unique_in : 3600
      JobHistory.redis_options = @redis_options
      JobHistory.new(worker.class, self.class, ttl)
    end

    def truncate(text, length: 100)
      if text.length > length
        text.slice(0, length)
      else
        text
      end
    end

    def perform_callback(worker, callback_name, args)
      if worker.respond_to?(callback_name)
        parameters = worker.method(callback_name).parameters

        begin
          if parameters.empty?
            worker.send(callback_name)
          else
            worker.send(callback_name, *args)
          end
        rescue ArgumentError => e
          raise ArgumentError.new("[UniqueJob] Invalid parameters callback=#{callback_name}")
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unique_job-0.4.0 lib/unique_job/util.rb
unique_job-0.3.2.pre lib/unique_job/util.rb