Module: CanvasSync::JobUniqueness::Compat::Sidekiq

Defined in:
lib/canvas_sync/job_uniqueness/compat/sidekiq.rb

Defined Under Namespace

Modules: WorkerExtension Classes: ClientMiddleware, CommonMiddleware, ServerMiddleware, SidekiqLockContext

Class Method Summary collapse

Class Method Details

.configureObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/canvas_sync/job_uniqueness/compat/sidekiq.rb', line 118

def self.configure
  sidekiq_middleware(:client) do |chain|
    chain.insert_before CanvasSync::JobBatches::Compat::Sidekiq::ClientMiddleware, Compat::Sidekiq::ClientMiddleware
  end

  sidekiq_middleware(:server) do |chain|
    chain.insert_after CanvasSync::JobBatches::Compat::Sidekiq::ServerMiddleware, Compat::Sidekiq::ServerMiddleware
  end

  ::Sidekiq::Worker.extend(ActiveSupport::Concern) unless ::Sidekiq::Worker < ActiveSupport::Concern

  ::Sidekiq::Worker.send(:include, Compat::Sidekiq::WorkerExtension)
end

.is_activejob_job?(msg) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/canvas_sync/job_uniqueness/compat/sidekiq.rb', line 76

def self.is_activejob_job?(msg)
  return false unless defined?(::ActiveJob)

  msg['class'] == 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper' && (msg['wrapped'].to_s).constantize < Compat::ActiveJob::UniqueJobExtension
end

.sidekiq_middleware(placement, &blk) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/canvas_sync/job_uniqueness/compat/sidekiq.rb', line 89

def self.sidekiq_middleware(placement, &blk)
  install_middleware = ->(config) do
    config.send("#{placement}_middleware") do |chain|
      blk.call(chain)
    end
  end

  ::Sidekiq.configure_client(&install_middleware) if placement == :client
  ::Sidekiq.configure_server(&install_middleware)
end

.validate_middleware_order(chain, order) ⇒ Object



82
83
84
85
86
87
# File 'lib/canvas_sync/job_uniqueness/compat/sidekiq.rb', line 82

def self.validate_middleware_order(chain, order)
  chain_classes = chain.entries.map(&:klass)
  filtered = chain_classes.select { |klass| order.include?(klass) }
  raise "Middleware chain does not contain all required middleware: #{order - filtered}" unless order.all? { |klass| filtered.include?(klass) }
  raise "Middleware must be in order: #{order.inspect}" if filtered != order
end

.validate_middleware_placement!Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/canvas_sync/job_uniqueness/compat/sidekiq.rb', line 100

def self.validate_middleware_placement!
  sidekiq_middleware(:client) do |chain|
    # Unique middleware must come _before_ the Batch middleware so that the uniqueness middleware can wrap the job in a batch
    validate_middleware_order(chain, [
      CanvasSync::JobUniqueness::Compat::Sidekiq::ClientMiddleware,
      CanvasSync::JobBatches::Compat::Sidekiq::ClientMiddleware,
    ])
  end

  sidekiq_middleware(:server) do |chain|
    # Unique middleware must com _after_ the Batch middleware so that the Batch is loaded before reaching the uniqueness middleware
    validate_middleware_order(chain, [
      CanvasSync::JobBatches::Compat::Sidekiq::ServerMiddleware,
      CanvasSync::JobUniqueness::Compat::Sidekiq::ServerMiddleware,
    ])
  end
end