Module: TCB

Defined in:
lib/tcb.rb,
lib/tcb/domain.rb,
lib/tcb/record.rb,
lib/tcb/publish.rb,
lib/tcb/version.rb,
lib/tcb/envelope.rb,
lib/tcb/event_bus.rb,
lib/tcb/stream_id.rb,
lib/tcb/command_bus.rb,
lib/tcb/event_query.rb,
lib/tcb/outbox_relay.rb,
lib/tcb/configuration.rb,
lib/tcb/rspec_helpers.rb,
lib/tcb/domain_context.rb,
lib/tcb/handles_events.rb,
lib/tcb/records_events.rb,
lib/tcb/handles_commands.rb,
lib/tcb/minitest_helpers.rb,
lib/tcb/correlation_query.rb,
lib/tcb/event_bus_shutdown.rb,
lib/tcb/test_helpers/shared.rb,
lib/tcb/event_store/in_memory.rb,
lib/tcb/outbox_store/in_memory.rb,
lib/tcb/event_bus_queue_pressure.rb,
lib/tcb/event_store/active_record.rb,
lib/tcb/event_bus/running_strategy.rb,
lib/tcb/outbox_store/active_record.rb,
lib/tcb/event_bus/shutdown_strategy.rb,
lib/tcb/subscriber_invocation_failed.rb,
lib/tcb/event_bus/subscriber_registry.rb,
lib/tcb/subscriber_metadata_extractor.rb,
lib/tcb/event_bus/queue_pressure_monitor.rb,
lib/generators/tcb/domain/domain_generator.rb,
lib/generators/tcb/outbox/outbox_generator.rb,
lib/generators/tcb/shared/command_argument.rb,
lib/generators/tcb/install/install_generator.rb,
lib/tcb/event_bus/termination_signal_handler.rb,
lib/generators/tcb/event_store/event_store_generator.rb

Defined Under Namespace

Modules: Domain, Generators, HandlesCommands, HandlesEvents, MinitestHelpers, OutboxStore, RSpecHelpers, RecordsEvents, TestHelpers Classes: Configuration, CorrelationQuery, DomainContext, Envelope, EventBus, EventBusQueuePressure, EventBusShutdown, EventQuery, EventStore, OutboxEntry, OutboxRelay, Record, StreamId, SubscriberInvocationFailed, SubscriberMetadataExtractor

Constant Summary collapse

VERSION =
"0.7.0"
CommandHandlerNotFound =
Class.new(StandardError)
ConfigurationError =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.configObject



89
90
91
# File 'lib/tcb.rb', line 89

def self.config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



41
42
43
44
45
46
# File 'lib/tcb.rb', line 41

def self.configure(&block)
  yield config
  config.domain_modules = @domain_modules || []
  config.permitted_serialization_classes
  config.freeze
end

.configured?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/tcb.rb', line 93

def self.configured?
  !!@config && @config.event_bus_configured?
end

.dispatch(command, correlation_id: SecureRandom.uuid) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/tcb/command_bus.rb', line 5

def self.dispatch(command, correlation_id: SecureRandom.uuid)
  validate!(command)
  handler = resolve_handler(command)
  Thread.current[:tcb_correlation_id] = correlation_id
  handler.new.call(command)
  correlation_id
ensure
  Thread.current[:tcb_correlation_id] = nil
end

.domain_modulesObject



37
38
39
# File 'lib/tcb.rb', line 37

def self.domain_modules
  @domain_modules || []
end

.domain_modules=(modules) ⇒ Object



33
34
35
# File 'lib/tcb.rb', line 33

def self.domain_modules=(modules)
  @domain_modules = modules
end

.publish(*events_or_envelopes) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/tcb/publish.rb', line 2

def self.publish(*events_or_envelopes)
  events_or_envelopes.each do |e|
    envelope = TCB::Envelope.coerce(e)
    config.event_bus.publish(envelope)
  end
  events_or_envelopes
end

.reactions_for(event_class) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/tcb.rb', line 67

def self.reactions_for(event_class)
  domain_modules.flat_map do |mod|
    next [] unless mod.respond_to?(:event_handler_registrations)

    mod.event_handler_registrations
      .select { |r| r.event_class == event_class }
      .flat_map(&:handlers)
  end
end

.read(domain_module) ⇒ Object



60
61
62
63
64
65
# File 'lib/tcb.rb', line 60

def self.read(domain_module)
  EventQuery.new(
    store: config.event_store,
    context: DomainContext.from_module(domain_module).to_s
  )
end

.read_correlation(correlation_id, across: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tcb.rb', line 77

def self.read_correlation(correlation_id, across: nil)
  domains = across || config.domain_modules.select do |m|
    m.respond_to?(:persist_registrations) && m.persist_registrations.any?
  end

  CorrelationQuery.new(
    store:          config.event_store,
    correlation_id: correlation_id,
    domains:        domains
  )
end

.record(events_from: [], events: [], within: nil, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tcb.rb', line 48

def self.record(events_from: [], events: [], within: nil, &block)
  Record.call(
    events_from:          events_from,
    events:               events,
    within:               within,
    store:                config.event_store,
    registrations:        config.persist_registrations,
    outbox_registrations: config.outbox_registrations,
    &block
  )
end

.reset!(graceful_shutdown_time: nil) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/tcb.rb', line 97

def self.reset!(graceful_shutdown_time: nil)
  if configured?
    graceful_shutdown_time ?
      @config.event_bus.shutdown(drain: true, timeout: graceful_shutdown_time) :
      @config.event_bus.force_shutdown
  end
  @config = nil
end