Class: Rufus::Scheduler::FileLock
- Inherits:
-
Object
- Object
- Rufus::Scheduler::FileLock
- Defined in:
- lib/rufus/scheduler/locks.rb
Overview
The standard flock mecha, with its own class thanks to @ecin
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path) ⇒ FileLock
constructor
A new instance of FileLock.
-
#lock ⇒ Object
Locking is successful if this Ruby process can create and lock its lockfile (at the given path).
- #locked? ⇒ Boolean
- #unlock ⇒ Object
Constructor Details
#initialize(path) ⇒ FileLock
Returns a new instance of FileLock.
50 51 52 53 |
# File 'lib/rufus/scheduler/locks.rb', line 50 def initialize(path) @path = path.to_s end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
48 49 50 |
# File 'lib/rufus/scheduler/locks.rb', line 48 def path @path end |
Instance Method Details
#lock ⇒ Object
Locking is successful if this Ruby process can create and lock its lockfile (at the given path).
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# 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? ⇒ Boolean
89 90 91 92 |
# File 'lib/rufus/scheduler/locks.rb', line 89 def locked? !! (@lockfile && @lockfile.flock(File::LOCK_NB | File::LOCK_EX)) end |
#unlock ⇒ Object
84 85 86 87 |
# File 'lib/rufus/scheduler/locks.rb', line 84 def unlock !! (@lockfile && @lockfile.flock(File::LOCK_UN)) end |