Major improvements

This commit is contained in:
Brusk Awat 2023-04-14 04:10:57 +03:00
parent 5a927b7c6a
commit 92ee8d2eea
Signed by: broosk1993
GPG Key ID: 5D20F7E02649F74E
4 changed files with 19 additions and 20 deletions

View File

@ -3,14 +3,15 @@
require_relative 'outboxable/version'
require 'outboxable/worker'
require 'outboxable/publishing_manager'
require 'outboxable/polling_publisher_worker'
require 'outboxable/connection'
require 'outboxable/configuration'
require 'outboxable/rabbitmq/publisher'
require 'active_support/concern'
require 'outboxable/publishing_manager'
require 'outboxable/polling_publisher_worker'
module Outboxable
class Error < StandardError; end

View File

@ -6,12 +6,6 @@ module Outboxable
def self.configure
self.configuration ||= Configuration.new
yield(configuration)
# In accordance to sidekiq-cron README: https://github.com/sidekiq-cron/sidekiq-cron#under-the-hood
Sidekiq::Options[:cron_poll_interval] = 5
# Create the cron job for the polling publisher
Sidekiq::Cron::Job.create(name: 'OutboxablePollingPublisher', cron: '*/5 * * * * *', class: 'Outboxable::PollingPublisherWorker', args: [{ orm: configuration.orm }])
end
class Configuration
@ -32,6 +26,12 @@ module Outboxable
raise Error, 'Outboxable Gem only supports Rails but you application does not seem to be a Rails app' unless Object.const_defined?('Rails')
raise Error, 'Outboxable Gem only support Rails version 7 and newer' if Rails::VERSION::MAJOR < 7
raise Error, 'Outboxable Gem uses the sidekiq-cron Gem. Make sure you add it to your project' unless Object.const_defined?('Sidekiq::Cron')
# In accordance to sidekiq-cron README: https://github.com/sidekiq-cron/sidekiq-cron#under-the-hood
Sidekiq::Options[:cron_poll_interval] = 5
# Create the cron job for the polling publisher
Sidekiq::Cron::Job.create(name: 'OutboxablePollingPublisher', cron: '*/5 * * * * *', class: 'Outboxable::PollingPublisherWorker')
end
def message_broker=(message_broker)

View File

@ -1,27 +1,25 @@
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)
def perform
Outboxable.configuration.orm == :mongoid ? perform_mongoid : perform_activerecord
end
def perform_activerecord(orm)
def perform_activerecord
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)
::Outboxable::Worker.perform_async(outbox.id)
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|
def perform_mongoid
Outbox.pending.any_of({ last_attempted_at: ..Time.zone.now }, { last_attempted_at: 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)
::Outboxable::Worker.perform_async(outbox.idempotency_key)
outbox.update(last_attempted_at: 1.minute.from_now, status: :processing, allow_publish: false)
end
end

View File

@ -4,9 +4,9 @@ module Outboxable
class Worker
include ::Sidekiq::Job
def perform(outbox_id, orm)
Outboxable::PublishingManager.publish(resource: Outbox.find(outbox_id)) if orm == 'activerecord'
Outboxable::PublishingManager.publish(resource: Outbox.find_by!(idempotency_key: outbox_id)) if orm == 'mongoid'
def perform(outbox_id)
Outboxable::PublishingManager.publish(resource: Outbox.find(outbox_id)) if Outboxable.configuration.orm == :activerecord
Outboxable::PublishingManager.publish(resource: Outbox.find_by!(idempotency_key: outbox_id)) if Outboxable.configuration.orm == :mongoid
end
end
end