Last active
August 29, 2015 13:56
-
-
Save simon-engledew/9010904 to your computer and use it in GitHub Desktop.
Set of useful methods for creating a singleton process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProcessLock | |
def initialize(filename) | |
FileUtils.touch(@filename = filename) | |
end | |
def acquire! | |
File.open(@filename, 'r+') do |f| | |
lock(f, false) do | |
f.truncate(f.write(Process.pid)) | |
return true | |
end | |
end | |
return false | |
end | |
def alive? | |
pid = read | |
return pid > 0 ? Process.kill(0, pid) > 0 : false | |
rescue | |
return false | |
end | |
def owner? | |
pid = read | |
pid and pid > 0 and pid == Process.pid | |
end | |
def read | |
File.open(@filename, 'r+'){|f|lock(f){f.read.to_i}} | |
end | |
private | |
def lock(file, blocking = true) | |
file.flock(blocking ? File::LOCK_EX : File::LOCK_EX | File::LOCK_NB) | |
return yield | |
ensure | |
file.flock(File::LOCK_UN) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment