Module: Mongoid::Lock

Defined in:
lib/mongoid-lock.rb,
lib/mongoid/lock.rb,
lib/mongoid/lock/synch_methods.rb

Defined Under Namespace

Classes: UnsynchronizedAccess

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object




38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mongoid/lock.rb', line 38

def self.included(base)
  super if defined?(super)
  base.class_eval do
    # => Extend Document, If not Already Extended
    unless (base.included_modules.include?(Mongoid::Document))
      include Mongoid::Document
    end
    # => Add special field
    field :lock_waiter, :type => String, :default => nil
    field :lock_used_by, :type => String, :default => nil
    field :lock_acquired_at, :type => Time, :default => nil
    index [[:lock_acquired_at, -1]], :sparse => true
  end
end

.load_requirementsObject



3
4
5
# File 'lib/mongoid-lock.rb', line 3

def self.load_requirements
  require 'mongoid/lock'
end

Instance Method Details

#local_mutexObject



6
7
8
# File 'lib/mongoid/lock/synch_methods.rb', line 6

def local_mutex
  @__mutex ||= Mutex.new
end

#lock_acquireObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mongoid/lock/synch_methods.rb', line 14

def lock_acquire
  self.lock_acquire_begin
  local = "#{Socket.gethostname}:#{Process.pid}"
  ident = self.lock_used_by
  wait  = self.lock_waiter
  if (ident and ident != local)
    raise Mongoid::Lock::UnsynchronizedAccess.new(ident)
  elsif (wait != local)
    raise Mongoid::Lock::UnsynchronizedAccess.new(wait)
  else
    self.set(:lock_used_by, local)
    self.set(:lock_acquired_at, Time.now)
    self.set(:lock_waiter, nil)
  end
end

#lock_acquire_beginObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/mongoid/lock/synch_methods.rb', line 39

def lock_acquire_begin
  local = "#{Socket.gethostname}:#{Process.pid}"
  ident = self.lock_used_by
  wait  = self.lock_waiter
  if (ident or wait)
    raise Mongoid::Lock::UnsynchronizedAccess.new(ident)
  else
    self.set(:lock_waiter, local)
  end
end

#lock_acquired_atObject



30
31
32
33
34
# File 'lib/mongoid/lock.rb', line 30

def lock_acquired_at
  self.save
  self.reload
  return super
end

#lock_releaseObject



50
51
52
53
54
55
56
57
58
# File 'lib/mongoid/lock/synch_methods.rb', line 50

def lock_release
  local = "#{Socket.gethostname}:#{Process.pid}"
  ident = self.lock_used_by
  if (ident and ident == local)
    self.reset_lock!
  else
    raise Mongoid::Lock::UnsynchronizedAccess.new(ident)
  end
end

#lock_try_acquireObject



30
31
32
33
34
35
36
37
# File 'lib/mongoid/lock/synch_methods.rb', line 30

def lock_try_acquire
  begin
    self.lock_acquire
    return true
  rescue Mongoid::Lock::UnsynchronizedAccess => ua
    return false
  end
end

#lock_used_byObject



24
25
26
27
28
# File 'lib/mongoid/lock.rb', line 24

def lock_used_by
  self.save
  self.reload
  return super
end

#lock_waiterObject




18
19
20
21
22
# File 'lib/mongoid/lock.rb', line 18

def lock_waiter
  self.save
  self.reload
  return super
end

#reset_lock!Object



10
11
12
# File 'lib/mongoid/lock/synch_methods.rb', line 10

def reset_lock!
  self.update_attributes(:lock_used_by => nil, :lock_acquired_at => nil, :lock_waiter => nil)
end

#synchronized(&block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/mongoid/lock/synch_methods.rb', line 60

def synchronized(&block)
  self.local_mutex.synchronize {
    self.lock_acquire
    begin
      block.call()
    ensure
      self.lock_release
    end
  }
end

#try_synchronized(&try_block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/mongoid/lock/synch_methods.rb', line 71

def try_synchronized(&try_block)
  begin
    self.synchronized do
      try_block.call()
    end
    return true
  rescue Mongoid::Lock::UnsynchronizedAccess => ua
    return false
  end
end

#wait_synchronized(timeout, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mongoid/lock/synch_methods.rb', line 82

def wait_synchronized(timeout, &block)
  started = Time.now
  expires = started + timeout
  begin
    success = self.try_synchronized do
      block.call()
    end
    unless (success)
      sleep(2)
    end
  end while(not success and Time.now < expires)
  return success
end