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/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/shared_state.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/report_raised_error.rb,
lib/race_guard/interceptors/emitter.rb,
lib/race_guard/interceptors/faraday.rb,
lib/race_guard/shared_state/watcher.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/shared_state/mutex_stack.rb,
lib/race_guard/shared_state/trace_point.rb,
lib/race_guard/shared_state/access_event.rb,
lib/race_guard/shared_state/memo_scanner.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/shared_state/memo_registry.rb,
lib/race_guard/index_integrity/model_scanner.rb,
lib/race_guard/index_integrity/schema_walker.rb,
lib/race_guard/shared_state/conflict_tracker.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/generators/race_guard/install/install_generator.rb,
lib/race_guard/index_integrity/schema_connection_indexes.rb

Defined Under Namespace

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

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.after_commit(&block) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
# File 'lib/race_guard.rb', line 51

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



22
23
24
# File 'lib/race_guard.rb', line 22

def configuration
  @configuration ||= Configuration.new
end

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

Yields:



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

def configure
  yield configuration
  SharedState::MemoRegistry.sync_from_configuration!
  SharedState::TracePoint.sync_with_configuration!
end

.contextObject



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

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

.define_rule(name) ⇒ Object



47
48
49
# File 'lib/race_guard.rb', line 47

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/race_guard.rb', line 62

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

  raise ReportRaisedError, event if event.severity == :raise

  nil
end

.reset_configuration!Object



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

def reset_configuration!
  SharedState::TracePoint.uninstall!
  @configuration = nil
end

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



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

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