Sha256: d7b77713861e2275e686eaefc322ca9ff45fbfaec331c0632a380032927fe585
Contents?: true
Size: 951 Bytes
Versions: 2
Compression:
Stored size: 951 Bytes
Contents
require 'socket' 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
mongoid-semaphore-0.0.5 | lib/mongoid/semaphore/sync_methods.rb |
mongoid-semaphore-0.0.4 | lib/mongoid/semaphore/sync_methods.rb |