mirror of
https://github.com/ditkrg/outboxable.git
synced 2026-01-22 22:06:47 +00:00
Major improvements
This commit is contained in:
parent
5a927b7c6a
commit
92ee8d2eea
@ -3,14 +3,15 @@
|
|||||||
require_relative 'outboxable/version'
|
require_relative 'outboxable/version'
|
||||||
|
|
||||||
require 'outboxable/worker'
|
require 'outboxable/worker'
|
||||||
require 'outboxable/publishing_manager'
|
|
||||||
require 'outboxable/polling_publisher_worker'
|
|
||||||
require 'outboxable/connection'
|
require 'outboxable/connection'
|
||||||
require 'outboxable/configuration'
|
require 'outboxable/configuration'
|
||||||
require 'outboxable/rabbitmq/publisher'
|
require 'outboxable/rabbitmq/publisher'
|
||||||
|
|
||||||
require 'active_support/concern'
|
require 'active_support/concern'
|
||||||
|
|
||||||
|
require 'outboxable/publishing_manager'
|
||||||
|
require 'outboxable/polling_publisher_worker'
|
||||||
|
|
||||||
module Outboxable
|
module Outboxable
|
||||||
class Error < StandardError; end
|
class Error < StandardError; end
|
||||||
|
|
||||||
|
|||||||
@ -6,12 +6,6 @@ module Outboxable
|
|||||||
def self.configure
|
def self.configure
|
||||||
self.configuration ||= Configuration.new
|
self.configuration ||= Configuration.new
|
||||||
yield(configuration)
|
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
|
end
|
||||||
|
|
||||||
class Configuration
|
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 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 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')
|
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
|
end
|
||||||
|
|
||||||
def message_broker=(message_broker)
|
def message_broker=(message_broker)
|
||||||
|
|||||||
@ -1,27 +1,25 @@
|
|||||||
module Outboxable
|
module Outboxable
|
||||||
class PollingPublisherWorker
|
class PollingPublisherWorker
|
||||||
include Sidekiq::Job
|
include Sidekiq::Job
|
||||||
sidekiq_options queue: 'critical'
|
|
||||||
|
|
||||||
def perform(args)
|
def perform
|
||||||
orm = args['orm']
|
Outboxable.configuration.orm == :mongoid ? perform_mongoid : perform_activerecord
|
||||||
orm == 'mongoid' ? perform_mongoid(orm) : perform_activerecord(orm)
|
|
||||||
end
|
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|
|
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.
|
||||||
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)
|
outbox.update(last_attempted_at: 1.minute.from_now, status: :processing, allow_publish: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform_mongoid(orm)
|
def perform_mongoid
|
||||||
Outbox.pending.where(last_attempted_at: [..Time.zone.now, nil]).each do |outbox|
|
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.
|
# 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)
|
outbox.update(last_attempted_at: 1.minute.from_now, status: :processing, allow_publish: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,9 +4,9 @@ module Outboxable
|
|||||||
class Worker
|
class Worker
|
||||||
include ::Sidekiq::Job
|
include ::Sidekiq::Job
|
||||||
|
|
||||||
def perform(outbox_id, orm)
|
def perform(outbox_id)
|
||||||
Outboxable::PublishingManager.publish(resource: Outbox.find(outbox_id)) if orm == 'activerecord'
|
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 orm == 'mongoid'
|
Outboxable::PublishingManager.publish(resource: Outbox.find_by!(idempotency_key: outbox_id)) if Outboxable.configuration.orm == :mongoid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user