Class: Synapse::EventSourcing::SnapshotTaker Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/event_sourcing/snapshot/taker.rb

Overview

This class is abstract.

Represents a mechanism for creating snapshot events for aggregates

Implementations can choose whether to snapshot the aggregate in the calling thread or asynchronously, though it is typically done asynchronously.

Direct Known Subclasses

AggregateSnapshotTaker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeundefined



17
18
19
# File 'lib/synapse/event_sourcing/snapshot/taker.rb', line 17

def initialize
  @executor = Contender::DirectExecutor.new
end

Instance Attribute Details

#event_storeSnapshotEventStore

Returns:

  • (SnapshotEventStore)


11
12
13
# File 'lib/synapse/event_sourcing/snapshot/taker.rb', line 11

def event_store
  @event_store
end

#executorContender::Executor

Returns:

  • (Contender::Executor)


14
15
16
# File 'lib/synapse/event_sourcing/snapshot/taker.rb', line 14

def executor
  @executor
end

Instance Method Details

#schedule_snapshot(type_identifier, aggregate_id) ⇒ undefined

Schedules a snapshot to be taken for an aggregate of the given type and with the given identifier

Parameters:

  • type_identifier (String)
  • aggregate_id (Object)

Returns:

  • (undefined)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/synapse/event_sourcing/snapshot/taker.rb', line 27

def schedule_snapshot(type_identifier, aggregate_id)
  @executor.execute do
    stream = @event_store.read_events type_identifier, aggregate_id
    first_sequence_number = stream.peek.sequence_number
    snapshot = create_snapshot type_identifier, aggregate_id, stream

    if snapshot and snapshot.sequence_number > first_sequence_number
      @event_store.append_snapshot_event type_identifier, snapshot
    end
  end
end