Sha256: 50e5e9270bb5edf1e43065774fc0c849d0625b870d9c59c66a66cffc6fe840e1

Contents?: true

Size: 610 Bytes

Versions: 5

Compression:

Stored size: 610 Bytes

Contents

require 'mongoid'

module TransactionEventStoreMongoid
  class Lock
    include Mongoid::Document

    store_in collection: 'event_store_locks'

    field :_id, type: String, default: ->{ SecureRandom.uuid }, overwrite: true
    field :ts, type: Time

    def with_lock(stream)
      begin
        lock = self.class.create(_id: stream, ts: Time.now.utc)
      rescue ::Mongo::Error::OperationFailure
        raise ::TransactionEventStore::CannotObtainLock
      end
      yield if block_given?
    ensure
      lock.delete if lock.present?
    end

    index({ ts: 1 }, { expire_after_seconds: 30 })
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
transaction_event_store_mongoid-1.0.2 lib/transaction_event_store_mongoid/lock.rb
transaction_event_store_mongoid-1.0.1 lib/transaction_event_store_mongoid/lock.rb
transaction_event_store_mongoid-1.0.0 lib/transaction_event_store_mongoid/lock.rb
transaction_event_store_mongoid-1.0.0.pre.1 lib/transaction_event_store_mongoid/lock.rb
transaction_event_store_mongoid-0.0.1 lib/transaction_event_store_mongoid/lock.rb