Class: GoodJob::Adapter::InlineBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/good_job/adapter/inline_buffer.rb

Overview

The InlineBuffer is integrated into the Adapter and captures jobs that have been enqueued inline. The purpose is allow job records to be persisted, in a locked state, while within a transaction, and then execute the jobs after the transaction has been committed to ensure that the jobs do not run within a transaction.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInlineBuffer

Returns a new instance of InlineBuffer.



58
59
60
# File 'lib/good_job/adapter/inline_buffer.rb', line 58

def initialize
  @callables = []
end

Class Attribute Details

.current_bufferGoodJob::Adapter::InlineBuffer?

Current buffer of jobs to be enqueued.



18
# File 'lib/good_job/adapter/inline_buffer.rb', line 18

thread_mattr_accessor :current_buffer

Class Method Details

.capture { ... } ⇒ Proc

This block should be used to wrap the transaction that could enqueue jobs.

Examples:

Wrapping a transaction

buffer = GoodJob::Adapter::InlineBuffer.capture do
  ActiveRecord::Base.transaction do
    MyJob.perform_later
  end
end
buffer.call

Yields:

  • The block that may enqueue jobs.

Returns:

  • (Proc)

    A proc that will execute enqueued jobs after the transaction has been committed.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/good_job/adapter/inline_buffer.rb', line 30

def self.capture
  if current_buffer
    yield
    return proc {}
  end

  begin
    self.current_buffer = new
    yield
    current_buffer.to_proc
  ensure
    self.current_buffer = nil
  end
end

.defer?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/good_job/adapter/inline_buffer.rb', line 54

def self.defer?
  current_buffer.present?
end

.perform_now_or_defer(&block) ⇒ Object

Used within the adapter to wrap inline job execution



46
47
48
49
50
51
52
# File 'lib/good_job/adapter/inline_buffer.rb', line 46

def self.perform_now_or_defer(&block)
  if defer?
    current_buffer.defer(block)
  else
    yield
  end
end

Instance Method Details

#defer(callable) ⇒ Object



62
63
64
# File 'lib/good_job/adapter/inline_buffer.rb', line 62

def defer(callable)
  @callables << callable
end

#to_procObject



66
67
68
69
70
# File 'lib/good_job/adapter/inline_buffer.rb', line 66

def to_proc
  proc do
    @callables.map(&:call)
  end
end