Sha256: dadc86e4de9fbf0a76268383663f460653a9a5daedf6344d0536d04b99dc26d8

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

class Inbox
  include Mongoid::Document
  include Mongoid::Timestamps
  include SimpleEnum::Mongoid

  field :route_name, type: String
  field :postman_name, type: String
  field :payload, type: String
  field :event_id, type: String
  field :attempts, type: Integer, default: 0
  field :last_attempted_at, type: Time
  field :processor_class_name, type: String
  field :metadata, type: Hash, default: {}

  # Indexes
  index({ event_id: 1 }, unique: true)

  attr_accessor :allow_processing

  as_enum :status, %i[pending processed failed], field: { type: String, default: 'pending' }, map: :string

  validates :processor_class_name, presence: true

  after_create :process, if: proc { |resource| resource.allow_processing == true }

  def increment_attempt
    self.attempts = attempts + 1
    self.last_attempted_at = Time.zone.now
  end

  def process
    processor_class_name.constantize.perform_async(id.to_s)
  end

  def check_threshold_reach
    return if attempts < ENV.fetch('INBOXABLE__MAX_ATTEMPTS', 3)&.to_i

    self.retry_at = Time.zone.now + ENV.fetch('INBOXABLE__RETRY_DELAY_IN_SECONDS', 5)&.to_i&.seconds
    self.status = :failed
    self.allow_processing = false
  end

  def check_publishing
    self.allow_processing = false if processed?
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
inboxable-0.1.1 lib/templates/mongoid_inbox.rb
inboxable-0.1.0 lib/templates/mongoid_inbox.rb