Class: Sidekiq::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-multi-tenant/sidekiq.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.push_bulk_with_tenants(items) ⇒ Object



84
85
86
# File 'lib/activerecord-multi-tenant/sidekiq.rb', line 84

def push_bulk_with_tenants(items)
  new.push_bulk_with_tenants(items)
end

Instance Method Details

#push_bulk_with_tenants(items) ⇒ Object

Allows the caller to enqueue multiple Sidekiq jobs with tenant information in a single call. It ensures that each job is processed within the correct tenant context and returns an array of job IDs for the enqueued jobs



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/activerecord-multi-tenant/sidekiq.rb', line 62

def push_bulk_with_tenants(items)
  first_job = items['jobs'].first
  return [] unless first_job # no jobs to push
  unless first_job.is_a?(Hash)
    raise ArgumentError, "Bulk arguments must be an Array of Hashes: [{ 'args' => [1], 'tenant_id' => 1 }, ...]"
  end

  normed = normalize_item(items.except('jobs').merge('args' => []))
  payloads = items['jobs'].map do |job|
    MultiTenant.with(job['tenant_id']) do
      copy = normed.merge('args' => job['args'], 'jid' => SecureRandom.hex(12), 'enqueued_at' => Time.now.to_f)
      result = process_single(items['class'], copy)
      result || nil
    end
  end.compact

  raw_push(payloads) unless payloads.empty?
  payloads.collect { |payload| payload['jid'] }
end