Skip to content

Instantly share code, notes, and snippets.

@sbellware
Last active December 22, 2018 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbellware/0afc3f850c00ab9da8dc75b5c81ec9e9 to your computer and use it in GitHub Desktop.
Save sbellware/0afc3f850c00ab9da8dc75b5c81ec9e9 to your computer and use it in GitHub Desktop.
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