Skip to content

Commit

Permalink
switch to NullLock and FileLock
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Aug 20, 2014
1 parent c1744af commit 99d685d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 50 deletions.
8 changes: 4 additions & 4 deletions lib/rufus/scheduler.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
11 changes: 6 additions & 5 deletions lib/rufus/scheduler/jobs.rb
Expand Up @@ -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

Expand Down
79 changes: 41 additions & 38 deletions lib/rufus/scheduler/locks.rb
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/lock_flock_spec.rb
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 99d685d

Please sign in to comment.