Module: CanvasSync::JobBatches::Sidekiq

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

Defined Under Namespace

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

Class Method Summary collapse

Class Method Details

.configureObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/canvas_sync/job_batches/sidekiq.rb', line 94

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

  ::Sidekiq.configure_client do |config|
    config.client_middleware do |chain|
      chain.remove ::Sidekiq::Batch::Middleware::ClientMiddleware if defined?(::Sidekiq::Batch::Middleware::ClientMiddleware)
      chain.add JobBatches::Sidekiq::ClientMiddleware
    end
  end
  ::Sidekiq.configure_server do |config|
    config.client_middleware do |chain|
      chain.remove ::Sidekiq::Batch::Middleware::ClientMiddleware if defined?(::Sidekiq::Batch::Middleware::ClientMiddleware)
      chain.add JobBatches::Sidekiq::ClientMiddleware
    end

    config.server_middleware do |chain|
      chain.remove ::Sidekiq::Batch::Middleware::ServerMiddleware if defined?(::Sidekiq::Batch::Middleware::ServerMiddleware)
      chain.add JobBatches::Sidekiq::ServerMiddleware
    end

    config.death_handlers << ->(job, ex) do
      switch_tenant(job) do
        if is_activejob_job?(job)
          JobBatches::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::Sidekiq::ServerMiddleware)
  ::Sidekiq::Worker.send(:include, JobBatches::Sidekiq::WorkerExtension)
  Batch::Callback.worker_class = SidekiqCallbackWorker
end

.is_activejob_job?(msg) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/canvas_sync/job_batches/sidekiq.rb', line 78

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

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

.switch_tenant(job) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/canvas_sync/job_batches/sidekiq.rb', line 84

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