diff --git a/lib/rufus/scheduler.rb b/lib/rufus/scheduler.rb index 565dbb7b..34bd2675 100644 --- a/lib/rufus/scheduler.rb +++ b/lib/rufus/scheduler.rb @@ -95,12 +95,12 @@ def initialize(opts={}) @scheduler_lock = if lockfile = opts[:lockfile] - Rufus::Scheduler::Lock::Flock.new(lockfile) + Rufus::Scheduler::FileLock.new(lockfile) else - opts[:scheduler_lock] || Rufus::Scheduler::Lock::Null.new + opts[:scheduler_lock] || Rufus::Scheduler::NullLock.new end - @job_lock = opts[:job_lock] || Rufus::Scheduler::Lock::Null.new + @job_lock = opts[:job_lock] || Rufus::Scheduler::NullLock.new # If we can't grab the @scheduler_lock, don't run. @scheduler_lock.lock || return @@ -353,7 +353,7 @@ def job(job_id) # to #lock # and #unlock, and both of these methods should be idempotent. # - # Look at rufus/lock/flock.rb for an example. + # Look at rufus/scheduler/locks.rb for an example. # def lock diff --git a/lib/rufus/scheduler/jobs.rb b/lib/rufus/scheduler/jobs.rb index 5133049a..fbaccba8 100644 --- a/lib/rufus/scheduler/jobs.rb +++ b/lib/rufus/scheduler/jobs.rb @@ -119,13 +119,14 @@ def trigger(time) set_next_time(time) - return if opts[:overlap] == false && running? - - r = + return if ( + opts[:overlap] == false && + running? + ) + return if ( callback(:confirm_lock, time) && callback(:on_pre_trigger, time) - - return if r == false + ) == false @count += 1 diff --git a/lib/rufus/scheduler/locks.rb b/lib/rufus/scheduler/locks.rb index 798a5d34..8ba1e6e1 100644 --- a/lib/rufus/scheduler/locks.rb +++ b/lib/rufus/scheduler/locks.rb @@ -27,65 +27,68 @@ class Rufus::Scheduler - module Lock + # + # A lock that can always be acquired + # + class NullLock + # Locking is always successful. # - # A lock that can always be acquired - # - class Null + def lock; true; end - 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 mecha, with its own class thanks to @ecin - # - class Flock + # + # The standard flock mecha, 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 - 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.flock(File::LOCK_UN) if @lockfile) - end + !! (@lockfile && @lockfile.flock(File::LOCK_UN)) + end - def locked? + def locked? - !!(@lockfile.flock(File::LOCK_NB | File::LOCK_EX) if @lockfile) - end + !! (@lockfile && @lockfile.flock(File::LOCK_NB | File::LOCK_EX)) end end end diff --git a/spec/lock_flock_spec.rb b/spec/lock_flock_spec.rb index 2477aaa7..91581727 100644 --- a/spec/lock_flock_spec.rb +++ b/spec/lock_flock_spec.rb @@ -9,12 +9,12 @@ require 'spec_helper' -describe Rufus::Scheduler::Lock::Flock do +describe Rufus::Scheduler::FileLock do before :each do @lock_path = '.rufus-scheduler.lock' - @lock = Rufus::Scheduler::Lock::Flock.new(@lock_path) + @lock = Rufus::Scheduler::FileLock.new(@lock_path) end after :each do @@ -23,7 +23,7 @@ FileUtils.rm_f('lock.txt') end - context ':lock => Rufus::Lock::File.new(path)' do + context ':scheduler_lock => Rufus::Scheduler::FileLock.new(path)' do it 'writes down a .rufus-scheduler.lock file' do