Separates polling publisher for mongoid from activerecord

This commit is contained in:
Brusk Awat 2023-04-13 23:38:42 +03:00
parent 64c23f9796
commit a5153324d5
Signed by: broosk1993
GPG Key ID: 5D20F7E02649F74E
2 changed files with 13 additions and 1 deletions

View File

@ -10,7 +10,7 @@ module Outboxable
class Configuration class Configuration
ALLOWED_MESSAGE_BROKERS = %i[rabbitmq].freeze ALLOWED_MESSAGE_BROKERS = %i[rabbitmq].freeze
ALLOWED_ORMS = %i[activerecord].freeze ALLOWED_ORMS = %i[activerecord mongoid].freeze
attr_accessor :rabbitmq_host, attr_accessor :rabbitmq_host,
:rabbitmq_port, :rabbitmq_port,

View File

@ -4,6 +4,10 @@ module Outboxable
sidekiq_options queue: 'critical' sidekiq_options queue: 'critical'
def perform def perform
Outboxable.configuration.orm == :mongoid ? perform_mongoid : perform_activerecord
end
def perform_activerecord
Outbox.pending.where(last_attempted_at: [..Time.zone.now, nil]).find_in_batches(batch_size: 100).each do |batch| Outbox.pending.where(last_attempted_at: [..Time.zone.now, nil]).find_in_batches(batch_size: 100).each do |batch|
batch.each do |outbox| 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. # This is to prevent a job from being retried too many times. Worst-case scenario is 1 minute delay in jobs.
@ -12,5 +16,13 @@ module Outboxable
end end
end end
end end
def perform_mongoid
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.id)
outbox.update(last_attempted_at: 1.minute.from_now, status: :processing, allow_publish: false)
end
end
end end
end end