Sha256: b2db8eb9e4844b0895c9a6cab7bd2e8964e3d221b17eb2d4bb316bfd6be0e6ac
Contents?: true
Size: 1020 Bytes
Versions: 1
Compression:
Stored size: 1020 Bytes
Contents
require 'socket' require 'process' require 'mongoid/semaphore/unsynchronized_access' module Mongoid module Semaphore def reset_semaphore! self.update_attributes!({:sema_count => 1, :used_by => nil}) end def synchronized(&block) begin self.dec(:sema_count, 1) if (self.sema_count == 0) begin self.set(:used_by, "#{Socket.gethostname}:#{Process.pid}") block.call(self) ensure self.set(:used_by, nil) end else raise Mongoid::Semaphore::UnsynchronizedAccess.new(self.sema_count, "Semaphore in Use! (#{self.used_by})") end ensure self.inc(:sema_count, 1) end end def try_synchronized(&try_block) return false unless self.sema_count == 1 begin self.synchronized do try_block.call() end return true rescue Mongoid::Semaphore::UnsynchronizedAccess => ua return false end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mongoid-semaphore-0.0.1 | lib/mongoid/semaphore/sync_methods.rb |