Class: Sequent::Core::AggregateSnapshotter

Inherits:
BaseCommandHandler show all
Defined in:
lib/sequent/core/aggregate_snapshotter.rb

Instance Attribute Summary

Attributes inherited from BaseCommandHandler

#repository

Instance Method Summary collapse

Methods inherited from BaseCommandHandler

#initialize

Methods included from Helpers::SelfApplier

#handle_message, included

Methods included from Helpers::UuidHelper

new_uuid

Constructor Details

This class inherits a constructor from Sequent::Core::BaseCommandHandler

Instance Method Details

#handles_message?(message) ⇒ Boolean

Returns:



9
10
11
# File 'lib/sequent/core/aggregate_snapshotter.rb', line 9

def handles_message?(message)
  message.is_a? SnapshotCommand
end

#SnapshotCommandObject

Take up to ‘limit` snapshots when needed. Throws `:done` when done.



16
17
18
19
20
21
22
23
# File 'lib/sequent/core/aggregate_snapshotter.rb', line 16

on SnapshotCommand do |command|
  aggregate_ids = repository.event_store.aggregates_that_need_snapshots(@last_aggregate_id, command.limit)
  aggregate_ids.each do |aggregate_id|
    take_snapshot!(aggregate_id)
  end
  @last_aggregate_id = aggregate_ids.last
  throw :done if @last_aggregate_id.nil?
end

#take_snapshot!(aggregate_id) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/sequent/core/aggregate_snapshotter.rb', line 25

def take_snapshot!(aggregate_id)
  aggregate = @repository.load_aggregate(aggregate_id)
  Sequent.logger.info "Taking snapshot for aggregate #{aggregate}"
  aggregate.take_snapshot!
rescue => e
  Sequent.logger.warn "Failed to take snapshot for aggregate #{aggregate_id}: #{e}", e.inspect
end