Module: Mongoid::Semaphore
- Included in:
- Lock
- Defined in:
- lib/mongoid-semaphore.rb,
lib/mongoid/semaphore.rb,
lib/mongoid/semaphore/sync_methods.rb,
lib/mongoid/semaphore/class_methods.rb
Defined Under Namespace
Modules: ClassMethods
Classes: MongoidSemaphoreRailtie, UnsynchronizedAccess
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/mongoid/semaphore.rb', line 29
def self.included(base)
super if defined?(super)
base.class_eval do
unless base.included_modules.include?(Mongoid::Document)
include Mongoid::Document
end
base.extend(Mongoid::Semaphore::ClassMethods)
field :sema_count, :type => Integer, :default => lambda { self.class.__semaphore_initial_count }
end
end
|
Instance Method Details
#reset_semaphore! ⇒ Object
3
4
5
|
# File 'lib/mongoid/semaphore/sync_methods.rb', line 3
def reset_semaphore!
self.update_attributes!({:sema_count => __semaphore_initial_count})
end
|
#sema_count ⇒ Object
> Volatile Value, Refresh Before Returning
21
22
23
24
25
|
# File 'lib/mongoid/semaphore.rb', line 21
def sema_count
self.save()
self.reload
return super
end
|
#sema_down ⇒ Object
11
12
13
|
# File 'lib/mongoid/semaphore/sync_methods.rb', line 11
def sema_down
self.dec(:sema_count, 1)
end
|
#sema_ready? ⇒ Boolean
15
16
17
18
19
|
# File 'lib/mongoid/semaphore/sync_methods.rb', line 15
def sema_ready?
self.save()
self.reload()
return (self.sema_count >= 0)
end
|
#sema_up ⇒ Object
7
8
9
|
# File 'lib/mongoid/semaphore/sync_methods.rb', line 7
def sema_up
self.inc(:sema_count, 1)
end
|
#synchronized(&block) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/mongoid/semaphore/sync_methods.rb', line 21
def synchronized(&block)
begin
self.sema_down
if (self.sema_ready?)
block.call()
else
raise Mongoid::Semaphore::UnsynchronizedAccess.new(self.sema_count)
end
ensure
self.sema_up
end
end
|
#try_synchronized(&try_block) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/mongoid/semaphore/sync_methods.rb', line 34
def try_synchronized(&try_block)
return false unless self.sema_ready?
begin
self.synchronized do
try_block.call()
end
return true
rescue Mongoid::Semaphore::UnsynchronizedAccess => ua
return false
end
end
|