Module: CanvasSync::JobBatches::Compat::Sidekiq

Defined in:
lib/canvas_sync/job_batches/compat/sidekiq.rb,
lib/canvas_sync/job_batches/compat/sidekiq/web.rb,
lib/canvas_sync/job_batches/compat/sidekiq/web/helpers.rb

Defined Under Namespace

Modules: Web, WorkerExtension Classes: ClientMiddleware, ServerMiddleware, SidekiqCallbackWorker

Class Method Summary collapse

Class Method Details

.configureObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/canvas_sync/job_batches/compat/sidekiq.rb', line 109

def self.configure
  return if @already_configured
  @already_configured = true

  if defined?(::Sidekiq::Batch) && ::Sidekiq::Batch != CanvasSync::JobBatches::Batch
    print "WARNING: Detected Sidekiq Pro or sidekiq-batch. CanvasSync JobBatches may not be fully compatible!"
  end

  sidekiq_middleware(:client) do |chain|
    chain.remove ::Sidekiq::Batch::Middleware::ClientMiddleware if defined?(::Sidekiq::Batch::Middleware::ClientMiddleware)
    chain.add CanvasSync::JobBatches::Compat::Sidekiq::ClientMiddleware
  end

  sidekiq_middleware(:server) do |chain|
    chain.remove ::Sidekiq::Batch::Middleware::ServerMiddleware if defined?(::Sidekiq::Batch::Middleware::ServerMiddleware)
    chain.add CanvasSync::JobBatches::Compat::Sidekiq::ServerMiddleware
  end

  ::Sidekiq.configure_server do |config|
    config.death_handlers << ->(job, ex) do
      switch_tenant(job) do
        if is_activejob_job?(job)
          CanvasSync::JobBatches::Compat::ActiveJob.handle_job_death(job["args"][0], ex)
        elsif job['bid'].present?
          ::Sidekiq::Batch.process_dead_job(job['bid'], job['jid'])
        end
      end
    end
  end

  ::Sidekiq.const_set(:Batch, CanvasSync::JobBatches::Batch)
  # This alias helps apartment-sidekiq set itself up correctly
  ::Sidekiq::Batch.const_set(:Server, CanvasSync::JobBatches::Compat::Sidekiq::ServerMiddleware)
  ::Sidekiq::Worker.send(:include, CanvasSync::JobBatches::Compat::Sidekiq::WorkerExtension)
  Batch::Callback.worker_class = SidekiqCallbackWorker
end

.is_activejob_job?(msg) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

.sidekiq_middleware(placement, &blk) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/canvas_sync/job_batches/compat/sidekiq.rb', line 98

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

.switch_tenant(job) ⇒ Object



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

def self.switch_tenant(job)
  if defined?(::Apartment)
    ::Apartment::Tenant.switch(job['apartment'] || 'public') do
      yield
    end
  else
    yield
  end
end