Class: Akasha::CommandRouter::OptimisticTransactor

Inherits:
Object
  • Object
show all
Defined in:
lib/akasha/command_router/optimistic_transactor.rb

Overview

Default command transactor providing optional optimistic concurrency. Works by loading aggregate from the repo by id, having it handle the command, and saving changes to the aggregate to the repository.

Constant Summary collapse

MAX_CONFLICT_RETRIES =

The default maximum number of retries when conflict is detected.

2
MIN_CONFLICT_RETRY_INTERVAL =

A lower limit for a retry interval.

0
MAX_CONFLICT_RETRY_INTERVAL =

An upper limit for a retry interval.

1

Instance Method Summary collapse

Instance Method Details

#call(aggregate_class, command, aggregate_id, options, **data) ⇒ Object

Have an aggregate handle a command.

  • ‘aggregate_class` - aggregate class you want to handle the command,

  • ‘command` - command the aggregate will process, corresponding to a method of the aggregate class.

  • ‘aggregate_id` - id of the aggregate instance the command is for,

  • ‘options`:

    - concurrency - `:optimistic` or `:none` (default: `:optimistic`);
    - revision - set to aggregate revision to detect conflicts while saving
      aggregates (requires `concurrency == :optimistic`); `nil` to just save
      without concurrency control;
    - max_conflict_retries - how many times to retry processing a command if a conflict
      is detected (`ConflictError`); default: MAX_CONFLICT_RETRIES;
    - min_conflict_retry_interval - minimum time to sleep between retries; default MIN_CO_RETRY_INTERVAL;
    - max_conflict_retry_interval - maximum time to sleep between retries; default MIN_CO_RETRY_INTERVAL.
    
  • ‘data`- command payload.



30
31
32
33
34
35
36
37
38
# File 'lib/akasha/command_router/optimistic_transactor.rb', line 30

def call(aggregate_class, command, aggregate_id, options, **data)
  max_conflict_retries = options.fetch(:max_conflict_retries, MAX_CONFLICT_RETRIES)
  min_conflict_retry_interval = options.fetch(:min_conflict_retry_interval, MIN_CONFLICT_RETRY_INTERVAL)
  max_conflict_retry_interval = options.fetch(:max_conflict_retry_interval, MAX_CONFLICT_RETRY_INTERVAL)
  with_retries(base_sleep_seconds: min_conflict_retry_interval, max_sleep_seconds: max_conflict_retry_interval,
               max_tries: 1 + max_conflict_retries, rescue: [Akasha::ConflictError]) do
    handle_command(aggregate_class, command, aggregate_id, options, **data)
  end
end