Class: Lockfile
- Inherits:
-
Object
- Object
- Lockfile
- Defined in:
- lib/scout/open/lock/lockfile.rb
Defined Under Namespace
Classes: LockError, MaxTriesLockError, NFSLockError, SleepCycle, StackingLockError, StatLockError, StolenLockError, TimeoutLockError, UnLockError
Constant Summary collapse
- VERSION =
'2.1.8'
- HOSTNAME =
Socket.gethostname
- DEFAULT_RETRIES =
maximum number of attempts
nil
- DEFAULT_TIMEOUT =
the longest we will try
nil
- DEFAULT_MAX_AGE =
lockfiles older than this are stale
3600
- DEFAULT_SLEEP_INC =
sleep cycle is this much longer each time
2
- DEFAULT_MIN_SLEEP =
shortest sleep time
2
- DEFAULT_MAX_SLEEP =
longest sleep time
32
- DEFAULT_SUSPEND =
iff we steal a lock wait this long before we go on
1800
- DEFAULT_REFRESH =
how often we touch/validate the lock
8
- DEFAULT_DONT_CLEAN =
iff we leave lock files lying around
false
- DEFAULT_POLL_RETRIES =
this many polls makes one ‘try’
16
- DEFAULT_POLL_MAX_SLEEP =
the longest we’ll sleep between polls
0.08
- DEFAULT_DONT_SWEEP =
if we cleanup after other process on our host
false
- DEFAULT_DONT_USE_LOCK_ID =
if we dump lock info into lockfile
false
- DEFAULT_DEBUG =
ENV['LOCKFILE_DEBUG'] || false
Class Attribute Summary collapse
-
.debug ⇒ Object
Returns the value of attribute debug.
-
.dont_clean ⇒ Object
Returns the value of attribute dont_clean.
-
.dont_sweep ⇒ Object
Returns the value of attribute dont_sweep.
-
.dont_use_lock_id ⇒ Object
Returns the value of attribute dont_use_lock_id.
-
.max_age ⇒ Object
Returns the value of attribute max_age.
-
.max_sleep ⇒ Object
Returns the value of attribute max_sleep.
-
.min_sleep ⇒ Object
Returns the value of attribute min_sleep.
-
.poll_max_sleep ⇒ Object
Returns the value of attribute poll_max_sleep.
-
.poll_retries ⇒ Object
Returns the value of attribute poll_retries.
-
.refresh ⇒ Object
Returns the value of attribute refresh.
-
.retries ⇒ Object
Returns the value of attribute retries.
-
.sleep_inc ⇒ Object
Returns the value of attribute sleep_inc.
-
.suspend ⇒ Object
Returns the value of attribute suspend.
-
.timeout ⇒ Object
Returns the value of attribute timeout.
Instance Attribute Summary collapse
-
#basename ⇒ Object
readonly
Returns the value of attribute basename.
-
#clean ⇒ Object
readonly
Returns the value of attribute clean.
-
#debug ⇒ Object
(also: #debug?)
Returns the value of attribute debug.
-
#dirname ⇒ Object
readonly
Returns the value of attribute dirname.
-
#dont_clean ⇒ Object
readonly
Returns the value of attribute dont_clean.
-
#dont_sweep ⇒ Object
readonly
Returns the value of attribute dont_sweep.
-
#dont_use_lock_id ⇒ Object
readonly
Returns the value of attribute dont_use_lock_id.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#locked ⇒ Object
(also: #locked?)
readonly
Returns the value of attribute locked.
-
#max_age ⇒ Object
readonly
Returns the value of attribute max_age.
-
#max_sleep ⇒ Object
readonly
Returns the value of attribute max_sleep.
-
#min_sleep ⇒ Object
readonly
Returns the value of attribute min_sleep.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#poll_max_sleep ⇒ Object
readonly
Returns the value of attribute poll_max_sleep.
-
#poll_retries ⇒ Object
readonly
Returns the value of attribute poll_retries.
-
#refresh ⇒ Object
readonly
Returns the value of attribute refresh.
-
#refresher ⇒ Object
readonly
Returns the value of attribute refresher.
-
#retries ⇒ Object
readonly
Returns the value of attribute retries.
-
#sleep_inc ⇒ Object
readonly
Returns the value of attribute sleep_inc.
-
#suspend ⇒ Object
readonly
Returns the value of attribute suspend.
-
#thief ⇒ Object
(also: #thief?)
readonly
Returns the value of attribute thief.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
- .create(path, *a, &b) ⇒ Object
- .description ⇒ Object
- .finalizer_proc(file) ⇒ Object
- .init ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
- #alive?(pid) ⇒ Boolean
- #attempt ⇒ Object
- #create(path) ⇒ Object
- #create_tmplock ⇒ Object
- #dump_lock_id(lock_id = @lock_id) ⇒ Object
- #errmsg(e) ⇒ Object
- #gen_lock_id ⇒ Object
- #getopt(key, default = nil) ⇒ Object
- #give_up! ⇒ Object
-
#initialize(path, opts = {}, &block) ⇒ Lockfile
constructor
A new instance of Lockfile.
- #load_lock_id(buf) ⇒ Object
- #lock ⇒ Object
- #new_refresher ⇒ Object
- #sweep ⇒ Object
-
#synchronize ⇒ Object
Executes the given block after acquiring the lock and ensures that the lock is relinquished afterwards.
- #timestamp ⇒ Object
- #tmpnam(dir, seed = File.basename($0)) ⇒ Object
- #to_str ⇒ Object (also: #to_s)
- #touch(path) ⇒ Object
- #trace(s = nil) ⇒ Object
- #try_again! ⇒ Object (also: #again!)
- #uncache(file) ⇒ Object
- #unlock ⇒ Object
- #validlock? ⇒ Boolean
- #version ⇒ Object
Constructor Details
#initialize(path, opts = {}, &block) ⇒ Lockfile
Returns a new instance of Lockfile.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/scout/open/lock/lockfile.rb', line 179 def initialize(path, opts = {}, &block) @klass = self.class @path = path @opts = opts @retries = getopt 'retries' , @klass.retries @max_age = getopt 'max_age' , @klass.max_age @sleep_inc = getopt 'sleep_inc' , @klass.sleep_inc @min_sleep = getopt 'min_sleep' , @klass.min_sleep @max_sleep = getopt 'max_sleep' , @klass.max_sleep @suspend = getopt 'suspend' , @klass.suspend @timeout = getopt 'timeout' , @klass.timeout @refresh = getopt 'refresh' , @klass.refresh @dont_clean = getopt 'dont_clean' , @klass.dont_clean @poll_retries = getopt 'poll_retries' , @klass.poll_retries @poll_max_sleep = getopt 'poll_max_sleep' , @klass.poll_max_sleep @dont_sweep = getopt 'dont_sweep' , @klass.dont_sweep @dont_use_lock_id = getopt 'dont_use_lock_id' , @klass.dont_use_lock_id @debug = getopt 'debug' , @klass.debug @semaphore = Mutex.new @sleep_cycle = SleepCycle.new @min_sleep, @max_sleep, @sleep_inc @clean = @dont_clean ? nil : Lockfile.finalizer_proc(@path) @dirname = File.dirname @path @basename = File.basename @path @thief = false @locked = false @refrsher = nil lock(&block) if block end |
Class Attribute Details
.debug ⇒ Object
Returns the value of attribute debug.
87 88 89 |
# File 'lib/scout/open/lock/lockfile.rb', line 87 def debug @debug end |
.dont_clean ⇒ Object
Returns the value of attribute dont_clean.
88 89 90 |
# File 'lib/scout/open/lock/lockfile.rb', line 88 def dont_clean @dont_clean end |
.dont_sweep ⇒ Object
Returns the value of attribute dont_sweep.
91 92 93 |
# File 'lib/scout/open/lock/lockfile.rb', line 91 def dont_sweep @dont_sweep end |
.dont_use_lock_id ⇒ Object
Returns the value of attribute dont_use_lock_id.
92 93 94 |
# File 'lib/scout/open/lock/lockfile.rb', line 92 def dont_use_lock_id @dont_use_lock_id end |
.max_age ⇒ Object
Returns the value of attribute max_age.
80 81 82 |
# File 'lib/scout/open/lock/lockfile.rb', line 80 def max_age @max_age end |
.max_sleep ⇒ Object
Returns the value of attribute max_sleep.
83 84 85 |
# File 'lib/scout/open/lock/lockfile.rb', line 83 def max_sleep @max_sleep end |
.min_sleep ⇒ Object
Returns the value of attribute min_sleep.
82 83 84 |
# File 'lib/scout/open/lock/lockfile.rb', line 82 def min_sleep @min_sleep end |
.poll_max_sleep ⇒ Object
Returns the value of attribute poll_max_sleep.
90 91 92 |
# File 'lib/scout/open/lock/lockfile.rb', line 90 def poll_max_sleep @poll_max_sleep end |
.poll_retries ⇒ Object
Returns the value of attribute poll_retries.
89 90 91 |
# File 'lib/scout/open/lock/lockfile.rb', line 89 def poll_retries @poll_retries end |
.refresh ⇒ Object
Returns the value of attribute refresh.
86 87 88 |
# File 'lib/scout/open/lock/lockfile.rb', line 86 def refresh @refresh end |
.retries ⇒ Object
Returns the value of attribute retries.
79 80 81 |
# File 'lib/scout/open/lock/lockfile.rb', line 79 def retries @retries end |
.sleep_inc ⇒ Object
Returns the value of attribute sleep_inc.
81 82 83 |
# File 'lib/scout/open/lock/lockfile.rb', line 81 def sleep_inc @sleep_inc end |
.suspend ⇒ Object
Returns the value of attribute suspend.
84 85 86 |
# File 'lib/scout/open/lock/lockfile.rb', line 84 def suspend @suspend end |
.timeout ⇒ Object
Returns the value of attribute timeout.
85 86 87 |
# File 'lib/scout/open/lock/lockfile.rb', line 85 def timeout @timeout end |
Instance Attribute Details
#basename ⇒ Object (readonly)
Returns the value of attribute basename.
125 126 127 |
# File 'lib/scout/open/lock/lockfile.rb', line 125 def basename @basename end |
#clean ⇒ Object (readonly)
Returns the value of attribute clean.
126 127 128 |
# File 'lib/scout/open/lock/lockfile.rb', line 126 def clean @clean end |
#debug ⇒ Object Also known as: debug?
Returns the value of attribute debug.
141 142 143 |
# File 'lib/scout/open/lock/lockfile.rb', line 141 def debug @debug end |
#dirname ⇒ Object (readonly)
Returns the value of attribute dirname.
124 125 126 |
# File 'lib/scout/open/lock/lockfile.rb', line 124 def dirname @dirname end |
#dont_clean ⇒ Object (readonly)
Returns the value of attribute dont_clean.
135 136 137 |
# File 'lib/scout/open/lock/lockfile.rb', line 135 def dont_clean @dont_clean end |
#dont_sweep ⇒ Object (readonly)
Returns the value of attribute dont_sweep.
138 139 140 |
# File 'lib/scout/open/lock/lockfile.rb', line 138 def dont_sweep @dont_sweep end |
#dont_use_lock_id ⇒ Object (readonly)
Returns the value of attribute dont_use_lock_id.
139 140 141 |
# File 'lib/scout/open/lock/lockfile.rb', line 139 def dont_use_lock_id @dont_use_lock_id end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
118 119 120 |
# File 'lib/scout/open/lock/lockfile.rb', line 118 def klass @klass end |
#locked ⇒ Object (readonly) Also known as: locked?
Returns the value of attribute locked.
121 122 123 |
# File 'lib/scout/open/lock/lockfile.rb', line 121 def locked @locked end |
#max_age ⇒ Object (readonly)
Returns the value of attribute max_age.
128 129 130 |
# File 'lib/scout/open/lock/lockfile.rb', line 128 def max_age @max_age end |
#max_sleep ⇒ Object (readonly)
Returns the value of attribute max_sleep.
131 132 133 |
# File 'lib/scout/open/lock/lockfile.rb', line 131 def max_sleep @max_sleep end |
#min_sleep ⇒ Object (readonly)
Returns the value of attribute min_sleep.
130 131 132 |
# File 'lib/scout/open/lock/lockfile.rb', line 130 def min_sleep @min_sleep end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
120 121 122 |
# File 'lib/scout/open/lock/lockfile.rb', line 120 def opts @opts end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
119 120 121 |
# File 'lib/scout/open/lock/lockfile.rb', line 119 def path @path end |
#poll_max_sleep ⇒ Object (readonly)
Returns the value of attribute poll_max_sleep.
137 138 139 |
# File 'lib/scout/open/lock/lockfile.rb', line 137 def poll_max_sleep @poll_max_sleep end |
#poll_retries ⇒ Object (readonly)
Returns the value of attribute poll_retries.
136 137 138 |
# File 'lib/scout/open/lock/lockfile.rb', line 136 def poll_retries @poll_retries end |
#refresh ⇒ Object (readonly)
Returns the value of attribute refresh.
133 134 135 |
# File 'lib/scout/open/lock/lockfile.rb', line 133 def refresh @refresh end |
#refresher ⇒ Object (readonly)
Returns the value of attribute refresher.
123 124 125 |
# File 'lib/scout/open/lock/lockfile.rb', line 123 def refresher @refresher end |
#retries ⇒ Object (readonly)
Returns the value of attribute retries.
127 128 129 |
# File 'lib/scout/open/lock/lockfile.rb', line 127 def retries @retries end |
#sleep_inc ⇒ Object (readonly)
Returns the value of attribute sleep_inc.
129 130 131 |
# File 'lib/scout/open/lock/lockfile.rb', line 129 def sleep_inc @sleep_inc end |
#suspend ⇒ Object (readonly)
Returns the value of attribute suspend.
132 133 134 |
# File 'lib/scout/open/lock/lockfile.rb', line 132 def suspend @suspend end |
#thief ⇒ Object (readonly) Also known as: thief?
Returns the value of attribute thief.
122 123 124 |
# File 'lib/scout/open/lock/lockfile.rb', line 122 def thief @thief end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
134 135 136 |
# File 'lib/scout/open/lock/lockfile.rb', line 134 def timeout @timeout end |
Class Method Details
.create(path, *a, &b) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/scout/open/lock/lockfile.rb', line 147 def Lockfile.create(path, *a, &b) opts = { 'retries' => 0, 'min_sleep' => 0, 'max_sleep' => 1, 'sleep_inc' => 1, 'max_age' => nil, 'suspend' => 0, 'refresh' => nil, 'timeout' => nil, 'poll_retries' => 0, 'dont_clean' => true, 'dont_sweep' => false, 'dont_use_lock_id' => true, } begin new(path, opts).lock rescue LockError raise Errno::EEXIST, path end open(path, *a, &b) end |
.description ⇒ Object
17 18 19 |
# File 'lib/scout/open/lock/lockfile.rb', line 17 def Lockfile.description 'a ruby library for creating perfect and NFS safe lockfiles' end |
.finalizer_proc(file) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/scout/open/lock/lockfile.rb', line 170 def self.finalizer_proc(file) pid = Process.pid lambda do |id| File.unlink file if Process.pid == pid rescue nil end end |
.init ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/scout/open/lock/lockfile.rb', line 94 def init @retries = DEFAULT_RETRIES @max_age = DEFAULT_MAX_AGE @sleep_inc = DEFAULT_SLEEP_INC @min_sleep = DEFAULT_MIN_SLEEP @max_sleep = DEFAULT_MAX_SLEEP @suspend = DEFAULT_SUSPEND @timeout = DEFAULT_TIMEOUT @refresh = DEFAULT_REFRESH @dont_clean = DEFAULT_DONT_CLEAN @poll_retries = DEFAULT_POLL_RETRIES @poll_max_sleep = DEFAULT_POLL_MAX_SLEEP @dont_sweep = DEFAULT_DONT_SWEEP @dont_use_lock_id = DEFAULT_DONT_USE_LOCK_ID @debug = DEFAULT_DEBUG STDOUT.sync = true if @debug STDERR.sync = true if @debug end |
Instance Method Details
#alive?(pid) ⇒ Boolean
379 380 381 382 383 384 385 386 387 |
# File 'lib/scout/open/lock/lockfile.rb', line 379 def alive? pid pid = Integer("#{ pid }") begin Process.kill 0, pid true rescue Errno::ESRCH false end end |
#attempt ⇒ Object
566 567 568 569 570 |
# File 'lib/scout/open/lock/lockfile.rb', line 566 def attempt ret = nil loop{ break unless catch('attempt'){ ret = yield } == 'try_again' } ret end |
#create(path) ⇒ Object
530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/scout/open/lock/lockfile.rb', line 530 def create(path) umask = nil f = nil begin umask = File.umask 022 f = open path, File::WRONLY|File::CREAT|File::EXCL, 0644 ensure File.umask umask if umask end return(block_given? ? begin; yield f; ensure; f.close; end : f) end |
#create_tmplock ⇒ Object
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/scout/open/lock/lockfile.rb', line 470 def create_tmplock tmplock = tmpnam @dirname begin create(tmplock) do |f| unless dont_use_lock_id @lock_id = gen_lock_id dumped = dump_lock_id trace{"lock_id <\n#{ @lock_id.inspect }\n>"} f.write dumped f.flush end yield f end ensure begin; File.unlink tmplock; rescue Errno::ENOENT; end if tmplock end end |
#dump_lock_id(lock_id = @lock_id) ⇒ Object
504 505 506 507 |
# File 'lib/scout/open/lock/lockfile.rb', line 504 def dump_lock_id(lock_id = @lock_id) "host: %s\npid: %s\nppid: %s\ntime: %s\n" % lock_id.values_at('host','pid','ppid','time') end |
#errmsg(e) ⇒ Object
562 563 564 |
# File 'lib/scout/open/lock/lockfile.rb', line 562 def errmsg(e) "%s (%s)\n%s\n" % [e.class, e., e.backtrace.join("\n")] end |
#gen_lock_id ⇒ Object
488 489 490 491 492 493 494 495 |
# File 'lib/scout/open/lock/lockfile.rb', line 488 def gen_lock_id Hash[ 'host' => "#{ HOSTNAME }", 'pid' => "#{ Process.pid }", 'ppid' => "#{ Process.ppid }", 'time' => , ] end |
#getopt(key, default = nil) ⇒ Object
546 547 548 549 550 551 |
# File 'lib/scout/open/lock/lockfile.rb', line 546 def getopt(key, default = nil) [ key, key.to_s, key.to_s.intern ].each do |k| return @opts[k] if @opts.has_key?(k) end return default end |
#give_up! ⇒ Object
577 578 579 |
# File 'lib/scout/open/lock/lockfile.rb', line 577 def give_up! throw 'attempt', 'give_up' end |
#load_lock_id(buf) ⇒ Object
509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/scout/open/lock/lockfile.rb', line 509 def load_lock_id(buf) lock_id = {} kv = %r/([^:]+):(.*)/o buf.each_line do |line| m = kv.match line k, v = m[1], m[2] next unless m and k and v lock_id[k.strip] = v.strip end lock_id end |
#lock ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/scout/open/lock/lockfile.rb', line 228 def lock raise StackingLockError, "<#{ @path }> is locked!" if @locked sweep unless @dont_sweep ret = nil attempt do begin @sleep_cycle.reset create_tmplock do |f| begin Timeout.timeout(@timeout) do tmp_path = f.path tmp_stat = f.lstat n_retries = 0 trace{ "attempting to lock <#{ @path }>..." } begin i = 0 begin trace{ "polling attempt <#{ i }>..." } begin File.link tmp_path, @path rescue Errno::ENOENT try_again! end lock_stat = File.lstat @path raise StatLockError, "stat's do not agree" unless tmp_stat.rdev == lock_stat.rdev and tmp_stat.ino == lock_stat.ino trace{ "aquired lock <#{ @path }>" } @locked = true rescue => e i += 1 unless i >= @poll_retries t = [rand(@poll_max_sleep), @poll_max_sleep].min trace{ "poll sleep <#{ t }>..." } sleep t retry end raise end rescue => e n_retries += 1 trace{ "n_retries <#{ n_retries }>" } case validlock? when true raise MaxTriesLockError, "surpased retries <#{ @retries }>" if @retries and n_retries >= @retries trace{ "found valid lock" } sleeptime = @sleep_cycle.next trace{ "sleep <#{ sleeptime }>..." } sleep sleeptime when false trace{ "found invalid lock and removing" } begin File.unlink @path @thief = true warn "<#{ @path }> stolen by <#{ Process.pid }> at <#{ }>" trace{ "i am a thief!" } rescue Errno::ENOENT end trace{ "suspending <#{ @suspend }>" } sleep @suspend when nil raise MaxTriesLockError, "surpased retries <#{ @retries }>" if @retries and n_retries >= @retries end retry end # begin end # timeout rescue Timeout::Error raise TimeoutLockError, "surpassed timeout <#{ @timeout }>" end # begin end # create_tmplock if block_given? stolen = false @refresher = (@refresh ? new_refresher : nil) begin begin ret = yield @path rescue StolenLockError stolen = true raise end ensure begin begin @semaphore.synchronize do @refresher.kill end rescue @refresher.kill end if @refresher and @refresher.status @refresher = nil ensure unlock unless stolen end end else @refresher = (@refresh ? new_refresher : nil) ObjectSpace.define_finalizer self, @clean if @clean ret = self end rescue Errno::ESTALE, Errno::EIO => e raise(NFSLockError, errmsg(e)) end end return ret end |
#new_refresher ⇒ Object
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/scout/open/lock/lockfile.rb', line 413 def new_refresher Thread.new(Thread.current, @path, @refresh, @dont_use_lock_id) do |thread, path, refresh, dont_use_lock_id| loop do begin touch path trace{"touched <#{ path }> @ <#{ Time.now.to_f }>"} unless dont_use_lock_id txt = nil @semaphore.synchronize do txt = IO.read(path) end loaded = load_lock_id(txt) trace{"loaded <\n#{ loaded.inspect }\n>"} raise unless loaded == @lock_id end sleep refresh rescue Exception => e trace{errmsg e} thread.raise StolenLockError Thread.exit end end end end |
#sweep ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/scout/open/lock/lockfile.rb', line 341 def sweep begin glob = File.join(@dirname, ".*lck") paths = Dir[glob] paths.each do |path| begin basename = File.basename path pat = %r/^\s*\.([^_]+)_([^_]+)/o if pat.match(basename) host, pid = $1, $2 else next end host.gsub!(%r/^\.+|\.+$/,'') quad = host.split %r/\./ host = quad.first pat = %r/^\s*#{ host }/i if pat.match(HOSTNAME) and %r/^\s*\d+\s*$/.match(pid) unless alive?(pid) trace{ "process <#{ pid }> on <#{ host }> is no longer alive" } trace{ "sweeping <#{ path }>" } FileUtils.rm_f path else trace{ "process <#{ pid }> on <#{ host }> is still alive" } trace{ "ignoring <#{ path }>" } end else trace{ "ignoring <#{ path }> generated by <#{ host }>" } end rescue next end end rescue => e warn(errmsg(e)) end end |
#synchronize ⇒ Object
Executes the given block after acquiring the lock and ensures that the lock is relinquished afterwards.
218 219 220 221 222 223 224 225 226 |
# File 'lib/scout/open/lock/lockfile.rb', line 218 def synchronize raise ArgumentError, 'block must be given' unless block_given? begin lock yield ensure unlock end end |
#timestamp ⇒ Object
497 498 499 500 501 502 |
# File 'lib/scout/open/lock/lockfile.rb', line 497 def time = Time.now usec = time.usec.to_s usec << '0' while usec.size < 6 "#{ time.strftime('%Y-%m-%d %H:%M:%S') }.#{ usec }" end |
#tmpnam(dir, seed = File.basename($0)) ⇒ Object
521 522 523 524 525 526 527 528 |
# File 'lib/scout/open/lock/lockfile.rb', line 521 def tmpnam(dir, seed = File.basename($0)) pid = Process.pid time = Time.now sec = time.to_i usec = time.usec "%s%s.%s_%d_%s_%d_%d_%d.lck" % [dir, File::SEPARATOR, HOSTNAME, pid, seed, sec, usec, rand(sec)] end |
#to_str ⇒ Object Also known as: to_s
553 554 555 |
# File 'lib/scout/open/lock/lockfile.rb', line 553 def to_str @path end |
#touch(path) ⇒ Object
542 543 544 |
# File 'lib/scout/open/lock/lockfile.rb', line 542 def touch(path) FileUtils.touch path end |
#trace(s = nil) ⇒ Object
558 559 560 |
# File 'lib/scout/open/lock/lockfile.rb', line 558 def trace(s = nil) STDERR.puts((s ? s : yield)) if @debug end |
#try_again! ⇒ Object Also known as: again!
572 573 574 |
# File 'lib/scout/open/lock/lockfile.rb', line 572 def try_again! throw 'attempt', 'try_again' end |
#uncache(file) ⇒ Object
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/scout/open/lock/lockfile.rb', line 452 def uncache file refresh = nil begin is_a_file = File === file path = (is_a_file ? file.path : file.to_s) stat = (is_a_file ? file.stat : File.stat(file.to_s)) refresh = tmpnam(File.dirname(path)) File.link path, refresh File.chmod stat.mode, path File.utime stat.atime, stat.mtime, path ensure begin File.unlink refresh if refresh rescue Errno::ENOENT end end end |
#unlock ⇒ Object
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/scout/open/lock/lockfile.rb', line 389 def unlock raise UnLockError, "<#{ @path }> is not locked!" unless @locked begin @semaphore.synchronize do @refresher.kill end rescue @refresher.kill end if @refresher and @refresher.status @refresher = nil begin File.unlink @path rescue Errno::ENOENT raise StolenLockError, @path ensure @thief = false @locked = false ObjectSpace.undefine_finalizer self if @clean end end |
#validlock? ⇒ Boolean
438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/scout/open/lock/lockfile.rb', line 438 def validlock? if @max_age uncache @path rescue nil begin return((Time.now - File.stat(@path).mtime) < @max_age) rescue Errno::ENOENT return nil end else exist = File.exist?(@path) return(exist ? true : nil) end end |