Well, it's not the prettiest, though I made something that will split it up and parse it out. This isn't going to be touched very often so I'm not worried about speed, though it should handle everything that whenever cannot handle, such as ranges and comma separated dates, as well as reboot.
how it works?
there is a method called cron_wrong
which just adds a base error.
I am using 2 columns, interval
and interval_type
.
interval_type
can be cron
, @reboot
, minute
, hour
, day
, month
, year
. if it is minute
, hour
, day
, month
, or year
, I am just passing the interval
and interval_type
to whenever
and doing interval.send interval_type
or using @reboot
The tricky part is when the interval_type
is cron
-- I am validating that it is indeed a valid cron string
def cron_intervals
non_num_at_beg_or_end = /^[^\d]|[^\d]$/
has_non_nums = /[^\d]/
return if interval_type == '@reboot'
if interval_type != 'cron'
has_non_nums.match(interval) { |str| errors.add :interval, "#{str} must be number" }
errors.add :interval, "can't be blank" if interval.length == 0
return
end
cron = interval.split(' ')
if cron.length != 5
cron_wrong
errors.add :interval, 'Wrong number of arguments supplied, (must be 5 -- minute hour day month year)'
return
end
cron.each do |c|
# return if star or only numbers
if c == '*' || !has_non_nums.match(c)
next
end
non_num_at_beg_or_end.match(c) do |str|
cron_wrong
errors.add :interval, "Non-number '#{str}' found"
return
end
c.split(',').each do |spl_dash|
non_num_at_beg_or_end.match(spl_dash) do |str|
cron_wrong
errors.add :interval, "Non-number '#{str}' found"
return
end
range = spl_dash.split('-')
if range.length < 1 || range.length > 2
cron_wrong
errors.add :interval, "ranges must be between two numbers"
return
elsif range.length == 2 and not (range[0].to_i < range[1].to_i)
cron_wrong
errors.add :interval, "range numbers must be lower-higher"
return
end
range.each do |num|
# should be only numbers at this point
has_non_nums.match(num) do |str|
cron_wrong
errors.add :interval, "Non-number '#{str}' found"
return
end
if num.length.blank?
cron_wrong
errors.add :interval, "No number supplied"
end
end
end
end
end