Module: SidekiqUtils::FindOptional

Extended by:
ActiveSupport::Concern
Defined in:
lib/sidekiq_utils/find_optional.rb

Defined Under Namespace

Classes: NotFoundError, NotUpdatedError

Instance Method Summary collapse

Instance Method Details

#find_optional(entity, id, scope: nil, updated_since: nil) ⇒ Object

try finding a record, but eventually give up retrying if we still cannot find it. use this if you are trying to load a record from a job, but don’t want the failed job to end up in the RetrySet if it keeps failing.

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sidekiq_utils/find_optional.rb', line 11

def find_optional(entity, id, scope: nil, updated_since: nil)
  entity = entity.public_send(scope) if scope
  if id.is_a?(Enumerable)
    instance = entity.where(id: id)
  else
    instance = entity.find_by(id: id)
  end

  raise(NotFoundError) unless instance.present?
  raise(NotUpdatedError) if updated_since && instance.updated_at < updated_since

  instance
end