Class: PubSubModelSync::Transaction

Inherits:
Base
  • Object
show all
Defined in:
lib/pub_sub_model_sync/transaction.rb

Constant Summary collapse

PUBLISHER_KLASS =
PubSubModelSync::MessagePublisher

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

config, debug?, log

Constructor Details

#initialize(key, max_buffer: config.transactions_max_buffer, headers: {}) ⇒ Transaction

Returns a new instance of Transaction.

Parameters:

  • key (String, Nil)

    Transaction key, if empty will use the ordering_key from first payload

  • max_buffer (Integer) (defaults to: config.transactions_max_buffer)

    Once this quantity of notifications is reached, then all notifications will immediately be delivered. Note: There is no way to rollback delivered notifications if current transaction fails



12
13
14
15
16
17
18
# File 'lib/pub_sub_model_sync/transaction.rb', line 12

def initialize(key, max_buffer: config.transactions_max_buffer, headers: {})
  @key = key
  @max_buffer = max_buffer
  @children = []
  @payloads = []
  @headers = headers
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



6
7
8
# File 'lib/pub_sub_model_sync/transaction.rb', line 6

def children
  @children
end

#finishedObject

Returns the value of attribute finished.



6
7
8
# File 'lib/pub_sub_model_sync/transaction.rb', line 6

def finished
  @finished
end

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'lib/pub_sub_model_sync/transaction.rb', line 6

def headers
  @headers
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/pub_sub_model_sync/transaction.rb', line 6

def key
  @key
end

#max_bufferObject

Returns the value of attribute max_buffer.



6
7
8
# File 'lib/pub_sub_model_sync/transaction.rb', line 6

def max_buffer
  @max_buffer
end

#payloadsObject

Returns the value of attribute payloads.



6
7
8
# File 'lib/pub_sub_model_sync/transaction.rb', line 6

def payloads
  @payloads
end

#rootObject

Returns the value of attribute root.



6
7
8
# File 'lib/pub_sub_model_sync/transaction.rb', line 6

def root
  @root
end

Instance Method Details

#add_payload(payload) ⇒ Object

TODO: remove buffer (already managed by pubsub services when running with config.async = true)

Parameters:



22
23
24
25
# File 'lib/pub_sub_model_sync/transaction.rb', line 22

def add_payload(payload)
  payloads << payload
  deliver_payloads
end

#add_transaction(transaction) ⇒ Object



36
37
38
39
40
# File 'lib/pub_sub_model_sync/transaction.rb', line 36

def add_transaction(transaction)
  transaction.root = self
  children << transaction
  transaction
end

#clean_publisherObject



48
49
50
# File 'lib/pub_sub_model_sync/transaction.rb', line 48

def clean_publisher
  PUBLISHER_KLASS.current_transaction = nil if !root && children.empty?
end

#deliver_allObject



52
53
54
55
# File 'lib/pub_sub_model_sync/transaction.rb', line 52

def deliver_all
  deliver_payloads
  clean_publisher
end

#finishObject

rubocop:disable Metrics/AbcSize



27
28
29
30
31
32
33
34
# File 'lib/pub_sub_model_sync/transaction.rb', line 27

def finish # rubocop:disable Metrics/AbcSize
  if root
    root.children = root.children.reject { |t| t == self }
    root.deliver_all if root.finished && root.children.empty?
  end
  self.finished = true
  deliver_all if children.empty?
end

#rollbackObject



42
43
44
45
46
# File 'lib/pub_sub_model_sync/transaction.rb', line 42

def rollback
  self.children = []
  root&.rollback
  clean_publisher
end