Class: ForestAdminAgent::AuditTrail::Sql::AuditConnectionBase

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/forest_admin_agent/audit_trail/sql/audit_connection_base.rb

Overview

Dedicated abstract base so the audit storage keeps its own connection pool, isolated from the host application's ActiveRecord::Base connection. Also the level the attribute overrides in AuditLog need: declaring them straight on an ActiveRecord::Base child resolves the type eagerly and blows up before any connection is established.

Direct Known Subclasses

AuditLog

Class Method Summary collapse

Class Method Details

.connect_to(database) ⇒ Object

establish_connection is class-level, so two stores pointed at different databases would silently clobber each other's pool and both end up writing to whichever connected last. One audit database per agent is the supported shape; a second, different one is a configuration mistake worth hearing about at boot.

Under one mutex for all stores, not one each: the check, the connect and the assignment have to be one step, or two stores connecting at once both pass the check and the loser writes to the winner's database.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/forest_admin_agent/audit_trail/sql/audit_connection_base.rb', line 26

def connect_to(database)
  connection_mutex.synchronize do
    if @database && @database != database
      raise ForestAdminDatasourceToolkit::Exceptions::ForestException,
            'The audit trail is already connected to another database. One agent, one audit database.'
    end
    next if @database

    establish_connection(database)
    @database = database
  end
end

.connection_mutexObject



46
47
48
# File 'lib/forest_admin_agent/audit_trail/sql/audit_connection_base.rb', line 46

def connection_mutex
  @connection_mutex ||= Mutex.new
end

.disconnect!Object



39
40
41
42
43
44
# File 'lib/forest_admin_agent/audit_trail/sql/audit_connection_base.rb', line 39

def disconnect!
  connection_mutex.synchronize do
    @database = nil
    remove_connection
  end
end