Sha256: 14095c5b16ad0f40700a5eb381f35d527bd9f015d42aafedddb0099cc9e119c8

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 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

  statuses.each_key do |key|
    scope key, -> { where(status_cd: key) }
  end

  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

1 entries across 1 versions & 1 rubygems

Version Path
inboxable-0.1.2 lib/templates/mongoid_inbox.rb