Class: Arrow::Session::PosixLock

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

Overview

The Arrow::Session::PosixLock class, a derivative of Arrow::Session::Lock. This lock type uses the ‘posixlock’ library (raa.ruby-lang.org/project/posixlock/).

VCS Id

$Id$

Authors

  • Michael Granger

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

Constant Summary

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.plock"
FileMode =

The mode to open the lockfile in

File::CREAT|File::RDWR

Constants inherited from Lock

READ, UNLOCKED, WRITE

Instance Attribute Summary

Class Method Summary

Instance Method Summary

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 Arrow::Object

deprecate_class_method, deprecate_method, inherited

Methods included from Arrow::Loggable

#log

Constructor Details

- (PosixLock) initialize(uri, id)

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



73
74
75
76
77
78
79
80
81
# File 'lib/arrow/session/posixlock.rb', line 73

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

  File.mkpath( @lockDir )
  @filename = File.join( @lockDir, LockfileFormat % id.to_s.gsub(/\W/, '_') ).untaint
  self.log.debug "Filename is: #@filename"
  @lockfile = nil
end

Instance Attribute Details

- (Object) lockDir

The path to the directory where session lockfiles are kept.



89
90
91
# File 'lib/arrow/session/posixlock.rb', line 89

def lockDir
  @lockDir
end

Class Method Details

+ (Object) clean(directory = DefaultLockDir, threshold = 3600)

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/arrow/session/posixlock.rb', line 43

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.posixlock( File::LOCK_EX|File::LOCK_NB )
        File.delete( file )
        fh.posixlock( 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

- (Object) acquire_read_lock(blocking) (protected)

Acquire a read (shared) lock on the lockfile.



127
128
129
130
131
132
# File 'lib/arrow/session/posixlock.rb', line 127

def acquire_read_lock( blocking )
  flags = File::LOCK_SH
  flags |= File::LOCK_NB if !blocking

  self.lockfile.posixlock( flags )
end

- (Object) acquire_write_lock(blocking) (protected)

Acquire a write (exclusive) lock on the lockfile.



136
137
138
139
140
141
# File 'lib/arrow/session/posixlock.rb', line 136

def acquire_write_lock( blocking )
  flags = File::LOCK_EX
  flags |= File::LOCK_NB if !blocking

  self.lockfile.posixlock( flags )
end

- (Object) close_lock_file (protected)

Close the lockfile and destroy the File object belonging to this lock.



114
115
116
117
118
119
120
121
122
123
# File 'lib/arrow/session/posixlock.rb', line 114

def close_lock_file
  if @lockfile
    path = @lockfile.path
    @lockfile.close
    @lockfile = nil
    if File.exist?( path.untaint )
      File.delete( path.untaint )
    end
  end
end

- (Object) finish

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



94
95
96
97
# File 'lib/arrow/session/posixlock.rb', line 94

def finish
  super
  self.close_lock_file
end

- (Object) lockfile (protected)

Get the File object for the lockfile belonging to this lock, creating it if necessary.



107
108
109
# File 'lib/arrow/session/posixlock.rb', line 107

def lockfile
  @lockfile ||= File.open( @filename, FileMode )
end

- (Object) release_read_lock (protected)

Release a previously-acquired read lock.



145
146
147
148
149
150
# File 'lib/arrow/session/posixlock.rb', line 145

def release_read_lock
  if !self.write_locked?
    self.lockfile.posixlock( File::LOCK_UN )
    self.close_lock_file
  end
end

- (Object) release_write_lock (protected)

Release a previously-acquired write lock.



154
155
156
157
158
159
160
161
# File 'lib/arrow/session/posixlock.rb', line 154

def release_write_lock
  if self.read_locked?
    self.lockfile.posixlock( File::LOCK_SH )
  else
    self.lockfile.posixlock( File::LOCK_UN )
    self.close_lock_file
  end
end