Class: Wal::LoggingWatcher

Inherits:
Object
  • Object
show all
Includes:
Watcher
Defined in:
lib/wal/logging_watcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(slot, watcher) ⇒ LoggingWatcher

Returns a new instance of LoggingWatcher.



5
6
7
8
9
10
# File 'lib/wal/logging_watcher.rb', line 5

def initialize(slot, watcher)
  @slot = slot
  @watcher = watcher
  @actions = Set.new
  @tables = Set.new
end

Instance Method Details

#on_event(event) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wal/logging_watcher.rb', line 20

def on_event(event)
  case event
  when Wal::BeginTransactionEvent
    @start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    @count = 0
    Wal.logger&.debug("[#{@slot}] Begin transaction=#{event.transaction_id} size=#{event.estimated_size}")
  when Wal::CommitTransactionEvent
    elapsed = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - @start) * 1000).round(3)
    message = "[#{@slot}] Commit transaction=#{event.transaction_id} elapsed=#{elapsed} events=#{@count}"
    message << " actions=#{@actions.sort.join(",")}" unless @actions.empty?
    message << " tables=#{@tables.sort.join(",")}" unless @tables.empty?
    if @count > 1
      Wal.logger&.info(message)
    else
      Wal.logger&.debug(message)
    end
    @actions.clear
    @tables.clear
  when Wal::InsertEvent
    Wal.logger&.debug("[#{@slot}] Insert transaction=#{event.transaction_id} table=#{event.table} primary_key=#{event.primary_key}")
    @count += 1
    @actions << :insert
    @tables << event.table
  when Wal::UpdateEvent
    if Wal.logger&.level >= Logger::DEBUG
      message = "[#{@slot}] Update transaction=#{event.transaction_id} table=#{event.table} primary_key=#{event.primary_key}"
      message << " changed=#{event.diff.keys.join(",")}" if event.diff && !event.diff.empty?
      Wal.logger&.debug(message)
    end
    @count += 1
    @actions << :update
    @tables << event.table
  when Wal::DeleteEvent
    Wal.logger&.debug("[#{@slot}] Delete transaction=#{event.transaction_id} table=#{event.table} primary_key=#{event.primary_key}")
    @count += 1
    @actions << :delete
    @tables << event.table
  else
    @count += 1
  end
  @watcher.on_event(event)
end

#should_watch_table?(table) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/wal/logging_watcher.rb', line 12

def should_watch_table?(table)
  @watcher.should_watch_table? table
end

#valid_context_prefix?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/wal/logging_watcher.rb', line 16

def valid_context_prefix?(prefix)
  @watcher.valid_context_prefix? prefix
end