Class: NewRelic::Security::ParseCron::CronParser
- Inherits:
-
Object
- Object
- NewRelic::Security::ParseCron::CronParser
- Defined in:
- lib/newrelic_security/parse-cron/cron_parser.rb
Overview
Parses cron expressions and computes the next occurence of the “job”
Defined Under Namespace
Classes: InternalTime
Constant Summary collapse
- SYMBOLS =
{ "jan" => "1", "feb" => "2", "mar" => "3", "apr" => "4", "may" => "5", "jun" => "6", "jul" => "7", "aug" => "8", "sep" => "9", "oct" => "10", "nov" => "11", "dec" => "12", "sun" => "0", "mon" => "1", "tue" => "2", "wed" => "3", "thu" => "4", "fri" => "5", "sat" => "6" }
- SUBELEMENT_REGEX =
%r{^(\d+)(-(\d+)(/(\d+))?)?$}
Instance Method Summary collapse
-
#initialize(source, time_source = Time) ⇒ CronParser
constructor
A new instance of CronParser.
- #interpret_vixieisms(spec) ⇒ Object
-
#last(now = @time_source.now, num = 1) ⇒ Object
returns the last occurence before the given date.
-
#next(now = @time_source.now, num = 1) ⇒ Object
returns the next occurence after the given date.
- #parse_element(elem, allowed_range) ⇒ Object
Constructor Details
#initialize(source, time_source = Time) ⇒ CronParser
Returns a new instance of CronParser.
55 56 57 58 59 |
# File 'lib/newrelic_security/parse-cron/cron_parser.rb', line 55 def initialize(source, time_source = Time) @source = interpret_vixieisms(source) @time_source = time_source validate_source end |
Instance Method Details
#interpret_vixieisms(spec) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/newrelic_security/parse-cron/cron_parser.rb', line 61 def interpret_vixieisms(spec) case spec when '@reboot' raise ArgumentError, "Can't predict last/next run of @reboot" when '@yearly', '@annually' '0 0 1 1 *' when '@monthly' '0 0 1 * *' when '@weekly' '0 0 * * 0' when '@daily', '@midnight' '0 0 * * *' when '@hourly' '0 * * * *' else spec end end |
#last(now = @time_source.now, num = 1) ⇒ Object
returns the last occurence before the given date
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/newrelic_security/parse-cron/cron_parser.rb', line 110 def last(now = @time_source.now, num = 1) t = InternalTime.new(now, @time_source) unless time_specs[:month][0].include?(t.month) nudge_month(t, :last) t.day = 32 end if t.day == 32 || !interpolate_weekdays(t.year, t.month)[0].include?(t.day) nudge_date(t, :last) t.hour = 24 end unless time_specs[:hour][0].include?(t.hour) nudge_hour(t, :last) t.min = 60 end # always nudge the minute nudge_minute(t, :last) t = t.to_time if num > 1 recursive_calculate(:last, t, num) else t end end |
#next(now = @time_source.now, num = 1) ⇒ Object
returns the next occurence after the given date
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/newrelic_security/parse-cron/cron_parser.rb', line 81 def next(now = @time_source.now, num = 1) t = InternalTime.new(now, @time_source) unless time_specs[:month][0].include?(t.month) nudge_month(t) t.day = 0 end unless interpolate_weekdays(t.year, t.month)[0].include?(t.day) nudge_date(t) t.hour = -1 end unless time_specs[:hour][0].include?(t.hour) nudge_hour(t) t.min = -1 end # always nudge the minute nudge_minute(t) t = t.to_time if num > 1 recursive_calculate(:next, t, num) else t end end |
#parse_element(elem, allowed_range) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/newrelic_security/parse-cron/cron_parser.rb', line 139 def parse_element(elem, allowed_range) values = elem.split(',').map do |subel| if subel =~ /^\*/ step = subel.length > 1 ? subel[2..-1].to_i : 1 stepped_range(allowed_range, step) else raise ArgumentError, "Bad Vixie-style specification #{subel}" unless SUBELEMENT_REGEX === subel if ::Regexp.last_match(5) # with range stepped_range(::Regexp.last_match(1).to_i..::Regexp.last_match(3).to_i, ::Regexp.last_match(5).to_i) elsif ::Regexp.last_match(3) # range without step stepped_range(::Regexp.last_match(1).to_i..::Regexp.last_match(3).to_i, 1) else # just a numeric [::Regexp.last_match(1).to_i] end end end.flatten.sort [Set.new(values), values, elem] end |