module SidekiqUtils::FindOptional

Public Instance Methods

find_optional(entity, id, scope: nil, updated_since: nil) click to toggle source

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.

# 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