Class: DaemonController::LockFile

Inherits:
Object
  • Object
show all
Defined in:
lib/daemon_controller/lock_file.rb

Defined Under Namespace

Classes: AlreadyLocked

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ LockFile

Returns a new instance of LockFile.



27
28
29
# File 'lib/daemon_controller/lock_file.rb', line 27

def initialize(filename)
	@filename = filename
end

Instance Method Details

#exclusive_lockObject



31
32
33
34
35
36
37
38
39
# File 'lib/daemon_controller/lock_file.rb', line 31

def exclusive_lock
	File.open(@filename, 'w') do |f|
		if Fcntl.const_defined? :F_SETFD
			f.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
		end
		f.flock(File::LOCK_EX)
		yield
	end
end

#shared_lockObject



41
42
43
44
45
46
47
48
49
# File 'lib/daemon_controller/lock_file.rb', line 41

def shared_lock
	File.open(@filename, 'w') do |f|
		if Fcntl.const_defined? :F_SETFD
			f.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
		end
		f.flock(File::LOCK_SH)
		yield
	end
end

#try_exclusive_lockObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/daemon_controller/lock_file.rb', line 64

def try_exclusive_lock
	File.open(@filename, 'w') do |f|
		if Fcntl.const_defined? :F_SETFD
			f.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
		end
		if f.flock(File::LOCK_EX | File::LOCK_NB)
			yield
		else
			raise AlreadyLocked
		end
	end
end

#try_shared_lockObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/daemon_controller/lock_file.rb', line 51

def try_shared_lock
	File.open(@filename, 'w') do |f|
		if Fcntl.const_defined? :F_SETFD
			f.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
		end
		if f.flock(File::LOCK_SH | File::LOCK_NB)
			yield
		else
			raise AlreadyLocked
		end
	end
end