Class: Whodunit::Chronicles::StreamAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/whodunit/chronicles/stream_adapter.rb

Overview

Abstract base class for database streaming adapters

Defines the interface that all database-specific adapters must implement for streaming database changes into audit events.

Direct Known Subclasses

Adapters::PostgreSQL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: Chronicles.logger) ⇒ StreamAdapter

Returns a new instance of StreamAdapter.



12
13
14
15
16
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 12

def initialize(logger: Chronicles.logger)
  @logger = logger
  @running = false
  @position = nil
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 10

def logger
  @logger
end

#position=(value) ⇒ Object (writeonly, protected)

Sets the attribute position

Parameters:

  • value

    the value to set the attribute position to.



76
77
78
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 76

def position=(value)
  @position = value
end

#running=(value) ⇒ Object (writeonly, protected)

Sets the attribute running

Parameters:

  • value

    the value to set the attribute running to.



76
77
78
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 76

def running=(value)
  @running = value
end

Instance Method Details

#current_positionString?

Get current replication position

Returns:

  • (String, nil)

    Current position or nil if not available

Raises:

  • (NotImplementedError)

    Must be implemented by subclasses



39
40
41
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 39

def current_position
  raise NotImplementedError, "#{self.class} must implement #current_position"
end

#log(level, message, context = {}) ⇒ Object (protected)

Log a message with context

Parameters:

  • level (Symbol)

    Log level (:info, :warn, :error, etc.)

  • message (String)

    Log message

  • context (Hash) (defaults to: {})

    Additional context



83
84
85
86
87
88
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 83

def log(level, message, context = {})
  logger.public_send(level, message,
    adapter: self.class.name.split('::').last,
    position: current_position,
    **context)
end

#setupvoid

This method returns an undefined value.

Set up the database for streaming (create publications, slots, etc.)

Raises:

  • (NotImplementedError)

    Must be implemented by subclasses



54
55
56
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 54

def setup
  raise NotImplementedError, "#{self.class} must implement #setup"
end

#start_streamingvoid

This method returns an undefined value.

Start streaming database changes

Parameters:

  • block (Proc)

    Block to call for each change event

Raises:

  • (NotImplementedError)

    Must be implemented by subclasses



23
24
25
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 23

def start_streaming(&)
  raise NotImplementedError, "#{self.class} must implement #start_streaming"
end

#stop_streamingvoid

This method returns an undefined value.

Stop streaming database changes

Raises:

  • (NotImplementedError)

    Must be implemented by subclasses



31
32
33
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 31

def stop_streaming
  raise NotImplementedError, "#{self.class} must implement #stop_streaming"
end

#streaming?Boolean

Check if adapter is currently streaming

Returns:

  • (Boolean)


46
47
48
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 46

def streaming?
  @running
end

#teardownvoid

This method returns an undefined value.

Tear down streaming setup (remove publications, slots, etc.)

Raises:

  • (NotImplementedError)

    Must be implemented by subclasses



62
63
64
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 62

def teardown
  raise NotImplementedError, "#{self.class} must implement #teardown"
end

#test_connectionBoolean

Test connection to the database

Returns:

  • (Boolean)

    true if connection is successful

Raises:

  • (NotImplementedError)

    Must be implemented by subclasses



70
71
72
# File 'lib/whodunit/chronicles/stream_adapter.rb', line 70

def test_connection
  raise NotImplementedError, "#{self.class} must implement #test_connection"
end