Last active
December 22, 2018 00:30
-
-
Save sbellware/0afc3f850c00ab9da8dc75b5c81ec9e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
handle Deposit do |deposit| | |
account_id = deposit.account_id | |
sequence = deposit.metadata.global_position | |
# Retry once if an expected version error is raised by the write | |
Retry.(MessageStore::ExpectedVersion::Error) do | |
account, version = store.fetch(account_id, include: :version) | |
# Idempotence protection using sequence numbers | |
unless sequence > account.sequence | |
logger.info(tag: :ignored) { "Command ignored (Command: #{deposit.message_type}, Account ID: #{account_id}, Account Sequence: #{account.sequence}, Deposit Sequence: #{sequence})" } | |
return | |
end | |
time = clock.iso8601 | |
deposited = Deposited.follow(deposit) | |
deposited.processed_time = time | |
deposited.sequence = sequence | |
stream_name = stream_name(account_id) | |
# Write with concurrency protection using expected version | |
write.(deposited, stream_name, expected_version: version) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment