Class: Clicker::LaunchLock

Inherits:
Object
  • Object
show all
Defined in:
lib/clicker/launch_lock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program_name) ⇒ LaunchLock

Returns a new instance of LaunchLock.



8
9
10
11
12
# File 'lib/clicker/launch_lock.rb', line 8

def initialize(program_name)
  @config_dir = "#{ENV['HOME']}/.config/#{program_name}"
  @lock_file_path = "#{@config_dir}/#{program_name}.lock"
  FileUtils.mkdir_p(@config_dir)
end

Instance Attribute Details

#lock_file_pathObject (readonly)

Returns the value of attribute lock_file_path.



6
7
8
# File 'lib/clicker/launch_lock.rb', line 6

def lock_file_path
  @lock_file_path
end

Instance Method Details

#locked?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/clicker/launch_lock.rb', line 35

def locked?
  File.exist?(@lock_file_path)
end

#ownerObject



39
40
41
42
43
44
45
46
47
# File 'lib/clicker/launch_lock.rb', line 39

def owner
  pid = nil
  File.open(@lock_file_path) do |f|
    pid = f.gets.to_i
  end
  return pid
rescue
  return nil
end

#try_lockObject



14
15
16
17
18
19
20
21
22
# File 'lib/clicker/launch_lock.rb', line 14

def try_lock
  File.open(@lock_file_path, 'w') do |f|
    f.puts Process.pid
  end
  yield
  return true
rescue
  return false
end

#unlockObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/clicker/launch_lock.rb', line 24

def unlock
  File.open(@lock_file_path) do |f|
    pid = f.gets.to_i
    Process.kill("TERM", pid)
  end
  FileUtils.rm(@lock_file_path)
  return true
rescue Errno::ENOENT
  return false
end