Class: PeriodicJob::Checkpoint

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps::Short
Defined in:
lib/periodic_job_mongoid/checkpoint.rb

Constant Summary collapse

FUTURE_CUTOFF_TIME =

checkpoints reached this far in the future will be considered erroneous and will be treated as unreached

10.minutes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](name) ⇒ Object



34
35
36
37
# File 'lib/periodic_job_mongoid/checkpoint.rb', line 34

def self.[](name)
  return nil if name.to_s.strip.empty?
  Checkpoint.find_or_create_by name: name
end

Instance Method Details

#advance_if_older_than(max_age) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/periodic_job_mongoid/checkpoint.rb', line 20

def advance_if_older_than(max_age)
  time_now = Time.now.utc
  result = self.class.where(id: self.id)
    .and(self.class
      .or(
        { reached_at: nil },
        { :reached_at.lte => time_now - max_age },
        { :reached_at.gte => time_now + FUTURE_CUTOFF_TIME },
        ).selector
    ).find_one_and_update({'$set' => { reached_at: time_now }})

  !result.nil?
end

#ageObject



16
17
18
# File 'lib/periodic_job_mongoid/checkpoint.rb', line 16

def age
  Time.now - reached_at if reached_at
end