Class: LockMethod::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/lock_method/lock.rb

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, method_id, args = nil, ttl = LockMethod.config.default_ttl, spin = false, &blk) ⇒ Lock

Returns a new instance of Lock.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lock_method/lock.rb', line 14

def initialize(obj, method_id, args = nil, ttl = LockMethod.config.default_ttl, spin = false, &blk)
  @mutex = ::Mutex.new
  @obj = obj
  @method_id = method_id
  @original_method_id = LockMethod.original_method_id method_id
  @method_signature = LockMethod.method_signature obj, method_id
  @args = args
  @args_digest = args.to_a.empty? ? 'empty' : LockMethod.digest(args)
  @ttl = ttl #!!!!!
  @spin = spin
  @blk = blk
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/lock_method/lock.rb', line 7

def args
  @args
end

#args_digestObject (readonly)

Returns the value of attribute args_digest.



12
13
14
# File 'lib/lock_method/lock.rb', line 12

def args_digest
  @args_digest
end

#blkObject (readonly)

Returns the value of attribute blk.



8
9
10
# File 'lib/lock_method/lock.rb', line 8

def blk
  @blk
end

#method_idObject (readonly)

Returns the value of attribute method_id.



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

def method_id
  @method_id
end

#method_signatureObject (readonly)

Returns the value of attribute method_signature.



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

def method_signature
  @method_signature
end

#objObject (readonly)

Returns the value of attribute obj.



5
6
7
# File 'lib/lock_method/lock.rb', line 5

def obj
  @obj
end

#original_method_idObject (readonly)

Returns the value of attribute original_method_id.



9
10
11
# File 'lib/lock_method/lock.rb', line 9

def original_method_id
  @original_method_id
end

#ttlObject (readonly)

Returns the value of attribute ttl.



11
12
13
# File 'lib/lock_method/lock.rb', line 11

def ttl
  @ttl
end

Instance Method Details

#call_and_lockObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lock_method/lock.rb', line 47

def call_and_lock
  while locked? and spin?
    ::Kernel.sleep 0.5
  end
  if locked?
    raise Locked, %{#{method_signature} is currently locked.}
  else
    begin
      save
      obj.send(*([original_method_id]+args), &blk)
    ensure
      delete
    end
  end
end

#deleteObject



27
28
29
# File 'lib/lock_method/lock.rb', line 27

def delete
  LockMethod.config.storage.delete cache_key
end

#locked?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/lock_method/lock.rb', line 35

def locked?
  !!LockMethod.config.storage.get(cache_key)
end

#marshal_dumpObject



39
40
41
# File 'lib/lock_method/lock.rb', line 39

def marshal_dump
  []
end

#marshal_load(source) ⇒ Object



43
44
45
# File 'lib/lock_method/lock.rb', line 43

def marshal_load(source)
  # nothing
end

#saveObject



31
32
33
# File 'lib/lock_method/lock.rb', line 31

def save
  LockMethod.config.storage.set cache_key, self, ttl
end