Class: EasySuite::Lock

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

Overview

– Lock ++ Class to lock easily.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lock_dir = nil) ⇒ Lock

– initialize ++

lock_dir

This class uses files in the directory.



17
18
19
# File 'lib/easysuite/lock.rb', line 17

def initialize(lock_dir = nil)
  @lock_dir = lock_dir
end

Instance Attribute Details

#lock_dirObject

Returns the value of attribute lock_dir.



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

def lock_dir
  @lock_dir
end

Instance Method Details

#lock(name) ⇒ Object

– lock ++ Make lock file in @lock_dir.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/easysuite/lock.rb', line 25

def lock(name)
  result = create_lock_file(name)

  if result && block_given?
    begin
      result = yield
    ensure
      delete_lock_file(name)
    end
  end

  result
end

#unlock(name) ⇒ Object

– unlock ++ Delete lock file in @lock_dir.



68
69
70
# File 'lib/easysuite/lock.rb', line 68

def unlock(name)
  delete_lock_file(name)
end

#wait(name, sec = nil) ⇒ Object

– wait ++ Make lock file in @lock_dir until success.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/easysuite/lock.rb', line 43

def wait(name, sec = nil)
  result = nil

  begin
    Timeout.timeout(sec) {
      sleep(1) until result = create_lock_file(name)
    }
  rescue Timeout::Error
  end

  if result && block_given?
    begin
      result = yield
    ensure
      delete_lock_file(name)
    end
  end

  result
end