Module: RaceGuard

Defined in:
lib/race_guard.rb,
lib/race_guard/rule.rb,
lib/race_guard/event.rb,
lib/race_guard/context.rb,
lib/race_guard/railtie.rb,
lib/race_guard/version.rb,
lib/race_guard/constants.rb,
lib/race_guard/protection.rb,
lib/race_guard/rule_engine.rb,
lib/race_guard/interceptors.rb,
lib/race_guard/method_watch.rb,
lib/race_guard/active_record.rb,
lib/race_guard/configuration.rb,
lib/race_guard/detector_runtime.rb,
lib/race_guard/method_resolution.rb,
lib/race_guard/interceptors/emitter.rb,
lib/race_guard/interceptors/faraday.rb,
lib/race_guard/commit_safety/watcher.rb,
lib/race_guard/interceptors/net_http.rb,
lib/race_guard/index_integrity/runner.rb,
lib/race_guard/reporters/log_reporter.rb,
lib/race_guard/interceptors/active_job.rb,
lib/race_guard/reporters/file_reporter.rb,
lib/race_guard/reporters/json_reporter.rb,
lib/race_guard/index_integrity/schema_ast.rb,
lib/race_guard/interceptors/action_mailer.rb,
lib/race_guard/reporters/webhook_reporter.rb,
lib/race_guard/index_integrity/model_scanner.rb,
lib/race_guard/index_integrity/schema_walker.rb,
lib/race_guard/index_integrity/validation_ast.rb,
lib/race_guard/index_integrity/schema_analyzer.rb,
lib/race_guard/index_integrity/table_inference.rb,
lib/race_guard/db_lock_auditor/read_modify_write.rb,
lib/race_guard/index_integrity/comparison_engine.rb,
lib/race_guard/index_integrity/schema_index_send.rb,
lib/race_guard/index_integrity/schema_connection_indexes.rb

Defined Under Namespace

Modules: ActiveRecord, CommitSafety, Context, DBLockAuditor, DetectorRuntime, IndexIntegrity, Interceptors, MethodResolution, MethodWatch, Reporters, RuleEngine Classes: Configuration, Event, Railtie, Rule

Constant Summary collapse

VERSION =
'0.1.0'
SEVERITY_LEVELS =
%i[info warn error raise].freeze

Class Method Summary collapse

Class Method Details

.after_commit(&block) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
# File 'lib/race_guard.rb', line 46

def after_commit(&block)
  raise ArgumentError, 'RaceGuard.after_commit requires a block' unless block

  if context.current.in_transaction?
    context.defer_after_commit(&block)
  else
    run_after_commit_immediate(block)
  end
  self
end

.configurationObject Also known as: config



20
21
22
# File 'lib/race_guard.rb', line 20

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



26
27
28
# File 'lib/race_guard.rb', line 26

def configure
  yield configuration
end

.contextObject



34
35
36
# File 'lib/race_guard.rb', line 34

def context
  @context ||= Context::Facade.new
end

.define_rule(name) ⇒ Object



42
43
44
# File 'lib/race_guard.rb', line 42

def define_rule(name, &)
  RuleEngine.define_rule(name, &)
end

.protect(name, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/race_guard/protection.rb', line 6

def self.protect(name, &block)
  raise ArgumentError, 'RaceGuard.protect requires a block' unless block

  sym = name.to_sym
  context.push_protected(sym)
  DetectorRuntime.enter(sym)
  yield
ensure
  safe_protect_exit(sym)
  context.pop_protected
end

.report(payload) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/race_guard.rb', line 57

def report(payload)
  cfg = configuration
  return nil unless cfg.active?

  event = Event.from_payload(payload)
  event = merge_protect_context(event)
  cfg.reporters.each do |reporter|
    reporter.report(event)
  rescue StandardError
    # isolate reporter failure
  end
  nil
end

.reset_configuration!Object



30
31
32
# File 'lib/race_guard.rb', line 30

def reset_configuration!
  @configuration = nil
end

.watch(klass, method_name, scope: :auto) ⇒ Object



38
39
40
# File 'lib/race_guard.rb', line 38

def watch(klass, method_name, scope: :auto)
  MethodWatch.watch(klass, method_name, scope: scope)
end