class MultiBackgroundJob::UniqueJob

Constants

VALID_OPTIONS

Attributes

across[R]
lock[R]
timeout[R]
unlock_policy[R]

Public Class Methods

coerce(value) click to toggle source
# File lib/multi_background_job/unique_job.rb, line 42
def self.coerce(value)
  return unless value.is_a?(Hash)

  new(
    across: (value['across'] || value[:across] || :queue).to_sym,
    timeout: (value['timeout'] || value[:timeout] || nil),
    unlock_policy: (value['unlock_policy'] || value[:unlock_policy] || :success).to_sym,
  ).tap do |instance|
    instance.lock = value['lock'] || value[:lock]
  end
end
new(across: :queue, timeout: nil, unlock_policy: :success, lock: nil) click to toggle source

@options [Hash] Unique definitions @option [Symbol] :across Valid options are :queue and :systemwide. If jobs should not to be duplicated on

current queue or the entire system

@option [Integer] :timeout Amount of times in seconds. Timeout decides how long to wait for acquiring the lock.

A default timeout is defined to 1 week so unique locks won't last forever.

@option [Symbol] :unlock_policy Control when the unique lock is removed. The default value is `success`.

The job will not unlock until it executes successfully, it will remain locked even if it raises an error and
goes into the retry queue. The alternative value is `start` the job will unlock right before it starts executing
# File lib/multi_background_job/unique_job.rb, line 23
def initialize(across: :queue, timeout: nil, unlock_policy: :success, lock: nil)
  unless VALID_OPTIONS[:across].include?(across.to_sym)
    raise Error, format('Invalid `across: %<given>p` option. Only %<expected>p are allowed.',
      given: across,
      expected: VALID_OPTIONS[:across])
  end
  unless VALID_OPTIONS[:unlock_policy].include?(unlock_policy.to_sym)
    raise Error, format('Invalid `unlock_policy: %<given>p` option. Only %<expected>p are allowed.',
      given: unlock_policy,
      expected: VALID_OPTIONS[:unlock_policy])
  end
  timeout = VALID_OPTIONS[:timeout] if timeout.to_i <= 0

  @across = across.to_sym
  @timeout = timeout.to_i
  @unlock_policy = unlock_policy.to_sym
  @lock = lock if lock.is_a?(MultiBackgroundJob::Lock)
end

Public Instance Methods

==(other)
Alias for: eql?
eql?(other) click to toggle source
# File lib/multi_background_job/unique_job.rb, line 68
def eql?(other)
  return false unless other.is_a?(self.class)

  [across, timeout, unlock_policy] == [other.across, other.timeout, other.unlock_policy]
end
Also aliased as: ==
lock=(value) click to toggle source
# File lib/multi_background_job/unique_job.rb, line 75
def lock=(value)
  @lock = case value
  when MultiBackgroundJob::Lock then value
  when Hash then MultiBackgroundJob::Lock.coerce(value)
  else
    nil
  end
end
to_hash() click to toggle source
# File lib/multi_background_job/unique_job.rb, line 58
def to_hash
  {
    'across' => (across.to_s if across),
    'timeout' => timeout,
    'unlock_policy' => (unlock_policy.to_s if unlock_policy),
  }.tap do |hash|
    hash['lock'] = lock.to_hash if lock
  end
end
ttl() click to toggle source
# File lib/multi_background_job/unique_job.rb, line 54
def ttl
  Time.now.to_f + timeout
end