class Rufus::Scheduler::FileLock
The standard flock mechanism, with its own class thanks to @ecin
Attributes
path[R]
Public Class Methods
new(path)
click to toggle source
# File lib/rufus/scheduler/locks.rb, line 50 def initialize(path) @path = path.to_s end
Public Instance Methods
lock()
click to toggle source
Locking is successful if this Ruby process can create and lock its lockfile (at the given path).
# File lib/rufus/scheduler/locks.rb, line 58 def lock return true if locked? @lockfile = nil FileUtils.mkdir_p(::File.dirname(@path)) file = File.new(@path, File::RDWR | File::CREAT) locked = file.flock(File::LOCK_NB | File::LOCK_EX) return false unless locked now = Time.now file.print("pid: #{$$}, ") file.print("scheduler.object_id: #{self.object_id}, ") file.print("time: #{now}, ") file.print("timestamp: #{now.to_f}") file.flush @lockfile = file true end
locked?()
click to toggle source
# File lib/rufus/scheduler/locks.rb, line 89 def locked? !! (@lockfile && @lockfile.flock(File::LOCK_NB | File::LOCK_EX)) end
unlock()
click to toggle source
# File lib/rufus/scheduler/locks.rb, line 84 def unlock !! (@lockfile && @lockfile.flock(File::LOCK_UN)) end