Sha256: ed8d93d60b2b2c8903f71ebda56145e21877cf5a08ff5e94a01412222f0ad315

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module Outboxable
  class PollingPublisherWorker
    include Sidekiq::Job
    sidekiq_options queue: 'critical'

    def perform(args)
      orm = args['orm']
      orm == 'mongoid' ? perform_mongoid(orm) : perform_activerecord(orm)
    end

    def perform_activerecord(orm)
      Outbox.pending.where(last_attempted_at: [..Time.zone.now, nil]).find_in_batches(batch_size: 100).each do |batch|
        batch.each do |outbox|
          # This is to prevent a job from being retried too many times. Worst-case scenario is 1 minute delay in jobs.
          Outboxable::Worker.perform_async(outbox.id, orm)
          outbox.update(last_attempted_at: 1.minute.from_now, status: :processing, allow_publish: false)
        end
      end
    end

    def perform_mongoid(orm)
      Outbox.pending.where(last_attempted_at: [..Time.zone.now, nil]).each do |outbox|
        # This is to prevent a job from being retried too many times. Worst-case scenario is 1 minute delay in jobs.
        Outboxable::Worker.perform_async(outbox.idempotency_key, orm)
        outbox.update(last_attempted_at: 1.minute.from_now, status: :processing, allow_publish: false)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
outboxable-1.0.2 lib/outboxable/polling_publisher_worker.rb