Sha256: 9bfa8cce86f3a7e78f87d6c5721ac7ad81c1ef8a7478a54a63e5dc19bf008e34
Contents?: true
Size: 1.41 KB
Versions: 15
Compression:
Stored size: 1.41 KB
Contents
module Mimi module DB module Lock class MysqlLock attr_reader :name, :name_uint64, :options, :timeout # # Timeout semantics: # nil -- wait indefinitely # 0 -- do not wait # <s> -- wait <s> seconds (can be Float) # def initialize(name, opts = {}) @name = name @name_uint64 = Digest::SHA1.digest(name).unpack('q').first @options = opts @timeout = if opts[:timeout].nil? -1 elsif opts[:timeout] <= 0 0 else opts[:timeout].to_f.round end end def execute(&_block) ActiveRecord::Base.transaction(requires_new: true) do begin acquire_lock_with_timeout! yield if block_given? ensure release_lock! end end end private # def acquire_lock_with_timeout! result = Mimi::DB.execute('select get_lock(?, ?) as lock_acquired', name, timeout) lock_acquired = result.first[0] == 1 raise Mimi::DB::Lock::NotAvailable unless lock_acquired true end # def release_lock! Mimi::DB.execute('select release_lock(?)', name) true end end # class MysqlLock end # module Lock end # module DB end # module Mimi
Version data entries
15 entries across 15 versions & 1 rubygems