lib/rufus/scheduler/locks.rb in rufus-scheduler-3.6.0 vs lib/rufus/scheduler/locks.rb in rufus-scheduler-3.7.0
- old
+ new
@@ -1,72 +1,69 @@
require 'fileutils'
-class Rufus::Scheduler
+#
+# A lock that can always be acquired
+#
+class Rufus::Scheduler::NullLock
+ # Locking is always successful.
#
- # A lock that can always be acquired
- #
- class NullLock
+ def lock; true; end
- # Locking is always successful.
- #
- def lock; true; end
+ def locked?; true; end
+ def unlock; true; end
+end
- def locked?; true; end
- def unlock; true; end
- end
+#
+# The standard flock mechanism, with its own class thanks to @ecin
+#
+class Rufus::Scheduler::FileLock
- #
- # The standard flock mechanism, with its own class thanks to @ecin
- #
- class FileLock
+ attr_reader :path
- attr_reader :path
+ def initialize(path)
- def initialize(path)
+ @path = path.to_s
+ end
- @path = path.to_s
- end
+ # Locking is successful if this Ruby process can create and lock
+ # its lockfile (at the given path).
+ #
+ def lock
- # Locking is successful if this Ruby process can create and lock
- # its lockfile (at the given path).
- #
- def lock
+ return true if locked?
- return true if locked?
+ @lockfile = nil
- @lockfile = nil
+ FileUtils.mkdir_p(::File.dirname(@path))
- FileUtils.mkdir_p(::File.dirname(@path))
+ file = File.new(@path, File::RDWR | File::CREAT)
+ locked = file.flock(File::LOCK_NB | File::LOCK_EX)
- file = File.new(@path, File::RDWR | File::CREAT)
- locked = file.flock(File::LOCK_NB | File::LOCK_EX)
+ return false unless locked
- return false unless locked
+ now = Time.now
- 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
- 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
- @lockfile = file
+ true
+ end
- true
- end
+ def unlock
- def unlock
+ !! (@lockfile && @lockfile.flock(File::LOCK_UN))
+ end
- !! (@lockfile && @lockfile.flock(File::LOCK_UN))
- end
+ def locked?
- def locked?
-
- !! (@lockfile && @lockfile.flock(File::LOCK_NB | File::LOCK_EX))
- end
+ !! (@lockfile && @lockfile.flock(File::LOCK_NB | File::LOCK_EX))
end
end