Class: Flu::TransactionBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/flu-rails/transaction_buffer.rb

Overview

The entities that recorded events in the transaction currently open, so that its commit can publish them.

A record hands its events over from 'after_commit', and Rails skips every transactional callback still to run as soon as one of them raises: it re-commits the rest of the batch with 'should_run_callbacks: false' (ActiveRecord::ConnectionAdapters::Transaction#commit_records). One raising callback, in this gem or in the application, therefore drops the events of every record left in the queue. The commit of the transaction itself is the one moment nothing can skip, and it is where these are published instead.

One buffer per thread, one mark per open transaction: what a transaction recorded is what was pushed past its mark, which is what its rollback discards and what the outermost commit hands back. A thread holding transactions open on several databases at once shares that one stack, so the events of the first to commit wait for the last.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransactionBuffer

Returns a new instance of TransactionBuffer.



23
24
25
26
# File 'lib/flu-rails/transaction_buffer.rb', line 23

def initialize
  @entities = []
  @marks    = []
end

Class Method Details

.currentObject



19
20
21
# File 'lib/flu-rails/transaction_buffer.rb', line 19

def self.current
  Thread.current[:flu_transaction_buffer] ||= new
end

Instance Method Details

#record(entity) ⇒ Boolean

Returns false when no transaction is open, in which case nothing will ever drain the buffer and the entity is left to publish its own changes.

Returns:

  • (Boolean)

    false when no transaction is open, in which case nothing will ever drain the buffer and the entity is left to publish its own changes.



30
31
32
33
34
35
36
37
# File 'lib/flu-rails/transaction_buffer.rb', line 30

def record(entity)
  if @marks.empty?
    false
  else
    @entities.push(entity)
    true
  end
end

#transaction_committedArray

Returns the entities to publish: those of the outermost transaction, none otherwise, a nested transaction leaving what it recorded to the one it is nested in.

Returns:

  • (Array)

    the entities to publish: those of the outermost transaction, none otherwise, a nested transaction leaving what it recorded to the one it is nested in.



45
46
47
48
49
50
51
52
53
54
# File 'lib/flu-rails/transaction_buffer.rb', line 45

def transaction_committed
  @marks.pop
  if @marks.empty?
    committed = @entities
    @entities = []
    committed
  else
    []
  end
end

#transaction_rolled_backArray

Returns the entities whose events the rollback discards.

Returns:

  • (Array)

    the entities whose events the rollback discards.



57
58
59
# File 'lib/flu-rails/transaction_buffer.rb', line 57

def transaction_rolled_back
  @entities.slice!((@marks.pop || 0)..) || []
end

#transaction_startedObject



39
40
41
# File 'lib/flu-rails/transaction_buffer.rb', line 39

def transaction_started
  @marks.push(@entities.size)
end