Class: Async::Cron::Schedule::Periodic

Inherits:
Generic
  • Object
show all
Defined in:
lib/async/cron/schedule/periodic.rb

Instance Attribute Summary

Attributes inherited from Generic

#flags

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#invoke, #run, #run_once

Constructor Details

#initialize(seconds, minutes, hours, weekday, monthday, month, flags = Flags.new) ⇒ Periodic

Create a new schedule with the given parameters.



33
34
35
36
37
38
39
40
41
42
# File 'lib/async/cron/schedule/periodic.rb', line 33

def initialize(seconds, minutes, hours, weekday, monthday, month, flags = Flags.new)
  super(flags)
  
  @seconds = seconds
  @minutes = minutes
  @hours = hours
  @weekday = weekday
  @monthday = monthday
  @month = month
end

Class Method Details

.parse(string) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/async/cron/schedule/periodic.rb', line 13

def self.parse(string)
  parts = string.split(/\s+/)
  
  if parts.last =~ /[a-zA-Z]/
    flags = Flags.parse(parts.pop)
  else
    flags = Flags.new
  end
  
  seconds = Seconds.parse(parts[0])
  minutes = Minutes.parse(parts[1])
  hours = Hours.parse(parts[2])
  weekday = Weekday.parse(parts[3])
  monthday = Monthday.parse(parts[4])
  month = Month.parse(parts[5])
  
  return self.new(seconds, minutes, hours, weekday, monthday, month, flags)
end

Instance Method Details

#increment(time = nil) ⇒ Object

Compute the next execution time after the given time.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/async/cron/schedule/periodic.rb', line 45

def increment(time = nil)
  time = Time.from(time) || Time.now
  
  units = [@seconds, @minutes, @hours, @weekday, @monthday, @month]
  index = 1
  
  # Step the smallest unit first:
  units[0].increment(time)
  
  # Now try to fit the rest of the schedule:
  while index < units.length
    unit = units[index]
    
    # puts "Checking #{unit} at #{index}... -> #{time}"
    
    # If the unit is not already at the desired time, increment it:
    unless unit.include?(time)
      # puts "Incrementing #{unit} at #{index}..."
      unit.increment(time)
      # puts "-> #{time}"
      
      # Reset all smaller units:
      units[0...index].each do |unit|
        # puts "Resetting #{unit}..."
        unit.reset(time)
      end
    end
    
    index += 1
  end
  
  return time.normalize!
end