Module: GoodJob::Bulk

Defined in:
lib/good_job/bulk.rb

Defined Under Namespace

Classes: Buffer

Constant Summary collapse

Error =
Class.new(StandardError)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_bufferGoodJob::Bulk::Buffer?

Current buffer of jobs to be enqueued.

Returns:



13
# File 'lib/good_job/bulk.rb', line 13

thread_mattr_accessor :current_buffer

Class Method Details

.capture(active_jobs = nil, queue_adapter: nil, &block) ⇒ nil, Array<ActiveJob::Base>

Capture jobs to a buffer. Pass either a block, or specific Active Jobs to be buffered.

Parameters:

  • active_jobs (Array<ActiveJob::Base>) (defaults to: nil)

    Active Jobs to be buffered.

  • queue_adapter (defaults to: nil)

    Override the jobs implicit queue adapter with an explicit one.

Returns:

  • (nil, Array<ActiveJob::Base>)

    The ActiveJob instances that have been buffered; nil if no active buffer

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/good_job/bulk.rb', line 19

def self.capture(active_jobs = nil, queue_adapter: nil, &block)
  raise(ArgumentError, "Use either the block form or the argument form, not both") if block && active_jobs

  if block
    buffer = Buffer.new
    buffer.capture(&block)
    buffer.active_jobs
  elsif current_buffer
    current_buffer&.add(active_jobs, queue_adapter: queue_adapter)
  end
end

.enqueue(active_jobs = nil, &block) ⇒ Array<ActiveJob::Base>

Capture jobs to a buffer and enqueue them all at once; or enqueue the current buffer.

Parameters:

  • active_jobs (Array<ActiveJob::Base>) (defaults to: nil)

    Active Jobs to be enqueued.

Returns:

  • (Array<ActiveJob::Base>)

    The ActiveJob instances that have been captured; check provider_job_id to confirm enqueued.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/good_job/bulk.rb', line 34

def self.enqueue(active_jobs = nil, &block)
  raise(ArgumentError, "Use either the block form or the argument form, not both") if block && active_jobs

  buffer = Buffer.new
  if block
    buffer.capture(&block)
    buffer.enqueue
    buffer.active_jobs
  elsif active_jobs.present?
    buffer.add(active_jobs)
    buffer.enqueue
    buffer.active_jobs
  elsif current_buffer.present?
    current_buffer.enqueue
    current_buffer.active_jobs
  end
end

.unbuffervoid

This method returns an undefined value.

Temporarily unset the current buffer; used to enqueue buffered jobs.



54
55
56
57
58
59
60
# File 'lib/good_job/bulk.rb', line 54

def self.unbuffer
  original_buffer = current_buffer
  self.current_buffer = nil
  yield
ensure
  self.current_buffer = original_buffer
end