Class: Arrow::Session::FileLock

Inherits:
Lock show all
Defined in:
lib/arrow/session/filelock.rb

Overview

The Arrow::Session::FileLock class, a derivative of Arrow::Session::Lock. Instances of this class provide file-based locking for Arrow sessions using the flock(2) system call. It (obviously) won’t work on platforms which don’t support flock(2), or on filesystems which don’t provide flock-based locking semantics (e.g., NFS).

Authors

Please see the file LICENSE in the top-level directory for licensing details.

Constant Summary collapse

DefaultLockDir =

The path to the default lockdir

'/tmp'
LockfileFormat =

The format string that will be used for the name of the lock file. The first ‘%s’ will be replaced with a sanitized version of the session id.

"arrow-session-%s.lock"
FileMode =

The mode to open the lockfile in

File::RDWR|File::CREAT

Constants inherited from Lock

Lock::READ, Lock::UNLOCKED, Lock::WRITE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Lock

create, derivativeDirs, #locked?, #read_lock, #read_locked?, #read_unlock, #release_all_locks, #with_read_lock, #with_write_lock, #write_lock, #write_locked?, #write_unlock

Methods inherited from Object

deprecate_class_method, deprecate_method, inherited

Constructor Details

#initialize(uri, id) ⇒ FileLock

Create a new Arrow::Session::FileLock object.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/arrow/session/filelock.rb', line 69

def initialize( uri, id )
	@lockDir = uri.path || DefaultLockDir
	super

	# 'foo de barg blag 0x1f2eca'.gsub( /\W/, '_' ) 
	#  => foo_de_barg_blag_0x1f2eca
	lockfilename = LockfileFormat % id.to_s.gsub( /\W/, '_' )
	File.mkpath( @lockDir )
	@filename = File.join( @lockDir, lockfilename ).untaint
	@lockfile = nil
end

Instance Attribute Details

#lockDirObject

The path to the directory where session lockfiles are kept.



87
88
89
# File 'lib/arrow/session/filelock.rb', line 87

def lockDir
  @lockDir
end

Class Method Details

.clean(directory = DefaultLockDir, threshold = 3600) ⇒ Object

Clean the specified directory of lock files older than threshold seconds.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/arrow/session/filelock.rb', line 39

def self::clean( directory=DefaultLockDir, threshold=3600 )
	pat = File.join( directory, LockfileFormat.gsub(/%s/, '*') )
	threshold = Time.now - threshold
	Dir[ pat ].each do |file|
		if File.mtime( file ) < threshold
			Arrow::Logger[self].info \
				"Removing stale lockfile '%s'" % file
			begin
				fh = File.open( file, FileMode )
				fh.flock( File::LOCK_EX|File::LOCK_NB )
				File.delete( file )
				fh.flock( File::LOCK_UN )
				fh.close
			rescue => err
				Arrow::Logger[self].warning \
					"Could not clean up '%s': %s" %
					[ file, err.message ]
				next
			end
		end
	end
end

Instance Method Details

#finishObject

Indicate to the lock that the caller will no longer be using it, and it may free any resources it had been using.



92
93
94
95
# File 'lib/arrow/session/filelock.rb', line 92

def finish
	super
	self.close_lock_file
end