Class: Videoreg::Lockfile

Inherits:
Object
  • Object
show all
Defined in:
lib/videoreg/lockfile.rb

Overview

Lockfile.rb – Implement a simple lock file method

Constant Summary collapse

KEY_LENGTH =
80

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lckf) ⇒ Lockfile

Returns a new instance of Lockfile.



11
12
13
# File 'lib/videoreg/lockfile.rb', line 11

def initialize(lckf)
  @lockfile = lckf
end

Instance Attribute Details

#lockfileObject

Returns the value of attribute lockfile.



6
7
8
# File 'lib/videoreg/lockfile.rb', line 6

def lockfile
  @lockfile
end

Instance Method Details

#create_keyObject



59
60
61
62
# File 'lib/videoreg/lockfile.rb', line 59

def create_key
  alpha = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
  return (0..KEY_LENGTH).map { alpha[rand(alpha.length)] }.join
end

#finalize(id) ⇒ Object



64
65
66
67
68
69
# File 'lib/videoreg/lockfile.rb', line 64

def finalize(id)
  #
  # Ensure lock file is erased when object dies before being released
  #
  File.delete(@lockfile)
end

#get_or_create_keyObject



55
56
57
# File 'lib/videoreg/lockfile.rb', line 55

def get_or_create_key
  @initial_lockcode || create_key
end

#lock(initial_lockcode = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/videoreg/lockfile.rb', line 15

def lock(initial_lockcode = nil)
  @initial_lockcode = initial_lockcode
  if File.exists?(@lockfile)
    return false
  else
    create
    ## verify that we indeed did get the lock
    verify
  end
end

#lockcodeObject



37
38
39
# File 'lib/videoreg/lockfile.rb', line 37

def lockcode
  readlock
end

#releaseObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/videoreg/lockfile.rb', line 41

def release
  if self.verify
    begin
      File.delete(@lockfile)
      @lockcode = ""
    rescue Exception => e
      return false
    end
    return true
  else
    return false
  end
end

#verifyObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/videoreg/lockfile.rb', line 26

def verify
  if not File.exists?(@lockfile)
    return false
  end
  if readlock == @lockcode.to_s
    return true
  else
    return false
  end
end