Module: PidLock
- Defined in:
- lib/pid_lock.rb,
lib/pid_lock/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"- @@pid_path =
nil
Class Method Summary collapse
- .lock(pid_name) ⇒ Object
- .locked?(pid_name) ⇒ Boolean
- .pid(pid_name) ⇒ Object
- .pid_path ⇒ Object
- .pid_path=(path) ⇒ Object
- .unlock(pid_name) ⇒ Object
Class Method Details
.lock(pid_name) ⇒ Object
16 17 18 19 |
# File 'lib/pid_lock.rb', line 16 def lock(pid_name) Dir.exists?(pid_path) or FileUtils.mkdir_p(pid_path) File.write(File.join(pid_path, "#{pid_name}.pid"), $$) end |
.locked?(pid_name) ⇒ Boolean
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pid_lock.rb', line 21 def locked?(pid_name) return false unless File.exists?(File.join(pid_path, "#{pid_name}.pid")) begin Process.kill(0, pid(pid_name)) return true rescue self.unlock(pid_name) return false end end |
.pid(pid_name) ⇒ Object
32 33 34 |
# File 'lib/pid_lock.rb', line 32 def pid(pid_name) File.read(File.join(pid_path, "#{pid_name}.pid")).to_i end |
.pid_path ⇒ Object
12 13 14 |
# File 'lib/pid_lock.rb', line 12 def pid_path @@pid_path || "tmp/pid_locks" end |
.pid_path=(path) ⇒ Object
8 9 10 |
# File 'lib/pid_lock.rb', line 8 def pid_path=(path) @@pid_path = path end |
.unlock(pid_name) ⇒ Object
36 37 38 39 40 |
# File 'lib/pid_lock.rb', line 36 def unlock(pid_name) if File.exists?(File.join(pid_path, "#{pid_name}.pid")) File.delete(File.join(pid_path, "#{pid_name}.pid")) end end |