Class: MultipleMan::AsyncModelPublisher

Inherits:
ModelPublisher
  • Object
show all
Defined in:
lib/multiple_man_sidekiq/async_model_publisher.rb

Instance Method Summary collapse

Instance Method Details

#publish(records, operation = :create) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/multiple_man_sidekiq/async_model_publisher.rb', line 4

def publish(records, operation=:create)
  return unless MultipleMan.configuration.enabled

  if records.respond_to?(:pluck)
    return unless records.any?
    ids = records.pluck(:id)
    klass = records.first.class.name
  else
    return if records.nil?
    ids = [records.id]
    klass = records.class.name
  end

  # Split jobs into parts so sidekiq doesn't need to deal with enormous seed jobs.
  ids.each_slice(1000) do |ids|
    queue = MultipleMan.configuration.sidekiq_queue[operation.to_sym] || 'default'

    Sidekiq::Client.push({
      'class' => ModelPublisherJob,
      'queue' => queue,
      'args'  => [ klass, ids, options, operation ]
    })
  end
end